Skip to content

Commit c0f9a47

Browse files
Ryang-21llama
andauthored
Blend v2 (#14471)
Co-authored-by: llama <llama@llama.fi> Co-authored-by: Ryang-21 <Ryang-21@users.noreply.github.com>
1 parent 6137f28 commit c0f9a47

5 files changed

Lines changed: 166 additions & 4 deletions

File tree

projects/blend-backstop-v2/api.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const { BackstopConfig, BackstopToken, BackstopPool, BackstopPoolV2, } = require("@blend-capital/blend-sdk");
2+
const { nativeToScVal, rpc, Account, TransactionBuilder, Contract, scValToNative } = require("@stellar/stellar-sdk");
3+
const BACKSTOP_ID = "CAQQR5SWBXKIGZKPBZDH3KM5GQ5GUTPKB7JAFCINLZBC5WXPJKRG3IM7";
4+
5+
const network = {
6+
rpc: "https://soroban-rpc.creit.tech/",
7+
passphrase: "Public Global Stellar Network ; September 2015",
8+
};
9+
async function getTokenBalance(
10+
token_id,
11+
address
12+
) {
13+
// account does not get validated during simulateTx
14+
const account = new Account('GANXGJV2RNOFMOSQ2DTI3RKDBAVERXUVFC27KW3RLVQCLB3RYNO3AAI4', '123');
15+
const tx_builder = new TransactionBuilder(account, {
16+
fee: '1000',
17+
timebounds: { minTime: 0, maxTime: 0 },
18+
networkPassphrase: network.passphrase,
19+
});
20+
tx_builder.addOperation(new Contract(token_id).call('balance', nativeToScVal(address, {type: 'address'})));
21+
const stellarRpc = new rpc.Server(network.rpc, network.opts);
22+
const scval_result = await stellarRpc.simulateTransaction(
23+
tx_builder.build()
24+
);
25+
if (scval_result == undefined) {
26+
throw Error(`unable to fetch balance for token: ${token_id}`);
27+
}
28+
if (rpc.Api.isSimulationSuccess(scval_result)) {
29+
const val = scValToNative(scval_result.result.retval);
30+
return val;
31+
} else {
32+
throw Error(`unable to fetch balance for token: ${token_id}`);
33+
}
34+
}
35+
async function tvl(api) {
36+
let backstop = await BackstopConfig.load(network, BACKSTOP_ID);
37+
let backstopTokeData = await BackstopToken.load(
38+
network,
39+
backstop.backstopTkn,
40+
backstop.blndTkn,
41+
backstop.usdcTkn
42+
);
43+
44+
let totalBackstopTokens = await getTokenBalance(
45+
backstop.backstopTkn,
46+
BACKSTOP_ID
47+
);
48+
let totalBLND = Number(totalBackstopTokens) * backstopTokeData.blndPerLpToken / 1e7;
49+
let totalUSDC = Number(totalBackstopTokens) * backstopTokeData.usdcPerLpToken / 1e7;
50+
// backstop token is a 80% BLND 20% USDC Comet pool (Balancer v1 fork)
51+
api.addCGToken("usd-coin", totalUSDC);
52+
api.addCGToken('blend', totalBLND);
53+
return api.getBalances();
54+
}
55+
56+
module.exports = {
57+
stellar: {
58+
tvl: () => ({}),
59+
pool2: tvl
60+
},
61+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { getExports } = require("../helper/heroku-api");
2+
3+
module.exports = {
4+
timetravel: false,
5+
misrepresentedTokens: false,
6+
methodology: `Counts the total amount of BLND-USDC LP shares held by the Blend backstop V2 contract.`,
7+
...getExports("blend-backstop-v2", ['stellar'])
8+
};

projects/blend-backstop/api.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { BackstopConfig, BackstopToken } = require("@blend-capital/blend-sdk");
2+
const { nativeToScVal, rpc, Account, TransactionBuilder, Contract, scValToNative } = require("@stellar/stellar-sdk");
23

34
const BACKSTOP_ID = "CAO3AGAMZVRMHITL36EJ2VZQWKYRPWMQAPDQD5YEOF3GIF7T44U4JAL3";
45

@@ -7,24 +8,58 @@ const network = {
78
passphrase: "Public Global Stellar Network ; September 2015",
89
};
910

11+
async function getTokenBalance(
12+
token_id,
13+
address
14+
) {
15+
// account does not get validated during simulateTx
16+
const account = new Account('GANXGJV2RNOFMOSQ2DTI3RKDBAVERXUVFC27KW3RLVQCLB3RYNO3AAI4', '123');
17+
const tx_builder = new TransactionBuilder(account, {
18+
fee: '1000',
19+
timebounds: { minTime: 0, maxTime: 0 },
20+
networkPassphrase: network.passphrase,
21+
});
22+
tx_builder.addOperation(new Contract(token_id).call('balance', nativeToScVal(address, {type: 'address'})));
23+
const stellarRpc = new rpc.Server(network.rpc, network.opts);
24+
const scval_result = await stellarRpc.simulateTransaction(
25+
tx_builder.build()
26+
);
27+
if (scval_result == undefined) {
28+
throw Error(`unable to fetch balance for token: ${token_id}`);
29+
}
30+
if (rpc.Api.isSimulationSuccess(scval_result)) {
31+
const val = scValToNative(scval_result.result.retval);
32+
return val;
33+
} else {
34+
throw Error(`unable to fetch balance for token: ${token_id}`);
35+
}
36+
}
37+
1038
async function tvl(api) {
1139
let backstop = await BackstopConfig.load(network, BACKSTOP_ID);
12-
let backstop_token = await BackstopToken.load(
40+
let backstopTokeData = await BackstopToken.load(
1341
network,
1442
backstop.backstopTkn,
1543
backstop.blndTkn,
1644
backstop.usdcTkn
1745
);
18-
46+
47+
let totalBackstopTokens = await getTokenBalance(
48+
backstop.backstopTkn,
49+
BACKSTOP_ID
50+
);
51+
let totalBLND = Number(totalBackstopTokens) * backstopTokeData.blndPerLpToken / 1e7;
52+
let totalUSDC = Number(totalBackstopTokens) * backstopTokeData.usdcPerLpToken / 1e7;
1953
// backstop token is a 80% BLND 20% USDC Comet pool (Balancer v1 fork)
20-
api.addCGToken("usd-coin", Number(backstop_token.usdc) / 1e7);
21-
api.addCGToken('blend', Number(backstop_token.blnd) / 1e7);
54+
api.addCGToken("usd-coin", totalUSDC);
55+
api.addCGToken('blend', totalBLND);
2256
return api.getBalances();
2357
}
2458

2559
module.exports = {
2660
hallmarks: [
2761
[1745478927, "Calculate TVL using BLND Coin Gecko price instead of approximation via pool weights"],
62+
[1745858101, "Only account for lp tokens held by the backstop contract"],
2863
],
2964
stellar: {
3065
tvl: () => ({}),

projects/blend-pools-v2/api.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const {
2+
BackstopConfig,
3+
PoolV2,
4+
} = require("@blend-capital/blend-sdk");
5+
const { PromisePool } = require("@supercharge/promise-pool");
6+
const { sumTokens2 } = require('../helper/unwrapLPs')
7+
8+
const BACKSTOP_ID = "CAQQR5SWBXKIGZKPBZDH3KM5GQ5GUTPKB7JAFCINLZBC5WXPJKRG3IM7";
9+
10+
const network = {
11+
rpc: "https://soroban-rpc.creit.tech/",
12+
passphrase: "Public Global Stellar Network ; September 2015",
13+
};
14+
15+
async function addPoolTVL(poolId, api, isBorrowed = false) {
16+
let pool = await PoolV2.load(network, poolId);
17+
18+
for (const [reserveId, reserve] of Array.from(pool.reserves)) {
19+
const supply = reserve.totalSupply();
20+
const borrowed = reserve.totalLiabilities();
21+
if (isBorrowed) {
22+
api.add(reserveId, borrowed);
23+
} else {
24+
api.add(reserveId, supply - borrowed);
25+
}
26+
}
27+
28+
}
29+
30+
async function tvl(api) {
31+
let backstop = await BackstopConfig.load(network, BACKSTOP_ID);
32+
for (const pool of backstop.rewardZone){
33+
await addPoolTVL(pool, api);
34+
}
35+
return sumTokens2({ api })
36+
}
37+
38+
async function borrowed(api) {
39+
let backstop = await BackstopConfig.load(network, BACKSTOP_ID);
40+
for (const pool of backstop.rewardZone)
41+
await addPoolTVL(pool, api, true);
42+
43+
return sumTokens2({ api })
44+
}
45+
46+
module.exports = {
47+
stellar: {
48+
tvl, borrowed,
49+
},
50+
};

projects/blend-pools-v2/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { getExports } = require("../helper/heroku-api");
2+
const methodologies = require("../helper/methodologies");
3+
4+
module.exports = {
5+
timetravel: false,
6+
methodology: `${methodologies.lendingMarket}. TVL is calculated and totaled for all Blend V2 pools in the Blend reward zone.`,
7+
...getExports("blend-pools-v2", ['stellar'], ['borrowed'])
8+
};

0 commit comments

Comments
 (0)