Skip to content

Commit 0695d4d

Browse files
nordwindernordwinder
andauthored
DefiTuna Lending (#16142)
Co-authored-by: nordwinder <[email protected]>
1 parent edfa117 commit 0695d4d

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed

projects/defituna_lending.js

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
const { getProvider, getAssociatedTokenAddress, sumTokens2 } = require("./helper/solana");
2+
const { Program } = require("@project-serum/anchor");
3+
4+
async function getAccountsInBatches(connection, pubkeys, batchSize = 100) {
5+
const result = [];
6+
for (let i = 0; i < pubkeys.length; i += batchSize) {
7+
const batch = pubkeys.slice(i, i + batchSize);
8+
const infos = await connection.getMultipleAccountsInfo(batch);
9+
result.push(...infos);
10+
}
11+
return result;
12+
}
13+
14+
async function tvl(api) {
15+
const provider = getProvider(api.chain);
16+
const program = new Program(tunaIDL, tunaIDL.address, provider);
17+
const vaults = await program.account.vault.all();
18+
19+
// Available funds are held in token accounts whose ATA addresses are derived from the mint,
20+
// the vault’s public key, and the mint program’s public key
21+
const mints = vaults.map((vault) => vault.account.mint);
22+
const mintAccounts = await getAccountsInBatches(provider.connection, mints);
23+
const mintPrograms = mintAccounts.map((account) => account.owner);
24+
const tokenAccounts = vaults.map(
25+
(vault, i) =>
26+
getAssociatedTokenAddress(vault.account.mint, vault.publicKey, mintPrograms[i])
27+
);
28+
29+
return sumTokens2({ tokenAccounts, api });
30+
}
31+
32+
async function borrowed(api) {
33+
const provider = getProvider(api.chain);
34+
const program = new Program(tunaIDL, tunaIDL.address, provider);
35+
const vaults = await program.account.vault.all();
36+
vaults.forEach(vault => api.add(vault.account.mint.toBase58(), vault.account.borrowedFunds));
37+
}
38+
39+
module.exports = {
40+
timetravel: false,
41+
solana: { tvl, borrowed },
42+
methodology: "TVL is calculated based on the amount of deposited tokens.",
43+
start: "2024-11-29",
44+
hallmarks: [
45+
[1753297176, 'TUNA token launched']
46+
],
47+
};
48+
49+
const tunaIDL = {
50+
address: "tuna4uSQZncNeeiAMKbstuxA9CUkHH6HmC64wgmnogD",
51+
metadata: {
52+
name: "tuna",
53+
version: "2.0.11",
54+
spec: "0.1.0",
55+
description: "DefiTuna"
56+
},
57+
instructions: [],
58+
accounts: [
59+
{
60+
name: "Vault",
61+
type: {
62+
kind: "struct",
63+
fields: [
64+
{
65+
name: "version",
66+
docs: ["Struct version"],
67+
type: "u16",
68+
},
69+
{
70+
name: "bump",
71+
docs: ["Bump seed for the vault account"],
72+
type: {
73+
array: ["u8", 1],
74+
},
75+
},
76+
{
77+
name: "mint",
78+
docs: ["The mint of the token that this vault holds"],
79+
type: "publicKey",
80+
},
81+
{
82+
name: "deposited_funds",
83+
docs: [
84+
"The amount of funds deposited in the vault - takes into account accrued interest",
85+
],
86+
type: "u64",
87+
},
88+
{
89+
name: "deposited_shares",
90+
docs: ["The amount of shares deposited in the vault"],
91+
type: "u64",
92+
},
93+
{
94+
name: "borrowed_funds",
95+
docs: [
96+
"The amount of funds borrowed from the vault - takes into account accrued interest",
97+
],
98+
type: "u64",
99+
},
100+
{
101+
name: "borrowed_shares",
102+
docs: ["The amount of shares borrowed from the vault"],
103+
type: "u64",
104+
},
105+
{
106+
name: "unpaid_debt_shares",
107+
docs: [
108+
"Bad dept may appear on a position liquidation if not enough funds to repay the debt to a lending pool.",
109+
],
110+
type: "u64",
111+
},
112+
{
113+
name: "interest_rate",
114+
docs: [
115+
"The interest rate of the vault per seconds. (1<<60) / 31536000 = 1152921504606846976 / 31536000 = 100% annually.",
116+
],
117+
type: "u64",
118+
},
119+
{
120+
name: "last_update_timestamp",
121+
docs: ["The last time the vault was updated."],
122+
type: "u64",
123+
},
124+
{
125+
name: "supply_limit",
126+
docs: [
127+
"The maximum allowed supply for this pool. The default value 0 is unlimited supply.",
128+
],
129+
type: "u64",
130+
},
131+
{
132+
name: "pyth_oracle_price_update",
133+
docs: ["Pyth oracle price update account."],
134+
type: "publicKey",
135+
},
136+
{
137+
name: "pyth_oracle_feed_id",
138+
docs: ["Pyth oracle price feed id."],
139+
type: "publicKey",
140+
},
141+
{
142+
name: "reserved",
143+
docs: ["Reserved"],
144+
type: {
145+
array: ["u8", 184],
146+
},
147+
},
148+
],
149+
},
150+
},
151+
],
152+
errors: [],
153+
types: [],
154+
};

0 commit comments

Comments
 (0)