Skip to content

Commit 9dee8d4

Browse files
committed
[echelon xlpt] include price conversion susde_usdc_xlpt
1 parent 4308c56 commit 9dee8d4

3 files changed

Lines changed: 95 additions & 2 deletions

File tree

constants/echelon.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
LENDING_CONTRACT_ADDRESS = "0xc6bc659f1649553c1a3fa05d9727433dc03843baac29473c817d06d39e7621ba"
22

3+
XLPT_ORACLE_CONTRACT_ADDRESS = "0xff9c14f0ab345b7804388d93401f5f0651f29dff7082bf386eea9b7a695c769e"
4+
35
SUSDE_MARKET_ADDRESS = "0x778362f04f7904ba0b76913ec7c0c5cc04e469b0b96929c6998b34910690a740"
46

57
SUSDE_TOKEN_ADDRESS = "0xb30a694a344edee467d9f82330bbe7c3b89f440a1ecd2da1f3bca266560fce69"

integrations/echelon_xlpt_integration.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from constants.example_integrations import (
1010
ECHELON_SUSDE_USDC_XLPT_COLLATERAL_START_BLOCK
1111
)
12-
from constants.echelon import LENDING_CONTRACT_ADDRESS, SUSDE_USDC_XLPT_MARKET_ADDRESS, SUSDE_USDC_TOKEN_ADDRESS, ETHENA_SUSDE_USDC_XLPT_ADDRESS_API_URL
12+
from constants.echelon import LENDING_CONTRACT_ADDRESS, XLPT_ORACLE_CONTRACT_ADDRESS, SUSDE_USDC_XLPT_MARKET_ADDRESS, SUSDE_USDC_TOKEN_ADDRESS, ETHENA_SUSDE_USDC_XLPT_ADDRESS_API_URL
1313
from constants.chains import Chain
1414
from integrations.integration_ids import IntegrationID as IntID
1515
from integrations.l2_delegation_integration import L2DelegationIntegration
@@ -37,7 +37,7 @@ def __init__(
3737
self.token_address = token_address
3838
self.market_address = market_address
3939
self.decimals = str(decimals)
40-
self.echelon_ts_location = "ts/echelon_balances.ts"
40+
self.echelon_ts_location = "ts/echelon_xlpt_balances.ts"
4141

4242
def get_l2_block_balances(
4343
self, cached_data: Dict[int, Dict[str, float]], blocks: List[int]
@@ -69,6 +69,8 @@ def get_participants_data(self, block, user_addresses=[]):
6969
str(self.decimals),
7070
str(block),
7171
json.dumps(user_addresses),
72+
XLPT_ORACLE_CONTRACT_ADDRESS,
73+
SUSDE_USDC_TOKEN_ADDRESS,
7274
],
7375
capture_output=True,
7476
text=True,

ts/echelon_xlpt_balances.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import * as dotenv from "dotenv";
2+
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
3+
4+
dotenv.config();
5+
6+
const config = new AptosConfig({ network: Network.MAINNET });
7+
// Aptos is the main entrypoint for all functions
8+
const client = new Aptos(config);
9+
10+
const args = process.argv.slice(2);
11+
const LENDING_CONTRACT_ADDRESS = args[0];
12+
const market_address = args[1];
13+
const decimals = Number(args[2]);
14+
const block = Number(args[3]);
15+
const user_addresses: string[] = JSON.parse(args[4]);
16+
const XLPT_ORACLE_ADDRESS = args[5];
17+
const SUSDE_USDC_TOKEN_ADDRESS = args[6];
18+
19+
async function getStrategy() {
20+
// iterate over all users and get their susde balance
21+
const user_balances: Record<string, number> = {};
22+
for (const address of user_addresses) {
23+
const susde_usdc_xlpt_balance = await client.view({
24+
payload: {
25+
function: `${LENDING_CONTRACT_ADDRESS}::lending::account_coins`,
26+
functionArguments: [address, market_address],
27+
},
28+
options: { ledgerVersion: block },
29+
});
30+
31+
const susde_usdc_xlpt_price = await client.view({
32+
payload: {
33+
function: `${XLPT_ORACLE_ADDRESS}::oracle::get_price`,
34+
functionArguments: [SUSDE_USDC_TOKEN_ADDRESS],
35+
},
36+
options: { ledgerVersion: block },
37+
});
38+
39+
const susde_usdc_value = Number(susde_usdc_xlpt_balance) * fp64ToFloat(BigInt((susde_usdc_xlpt_price[0] as { v: string }).v));;
40+
41+
user_balances[address] = scaleDownByDecimals(
42+
susde_usdc_value,
43+
decimals
44+
);
45+
}
46+
47+
console.log(JSON.stringify(user_balances));
48+
}
49+
50+
function scaleDownByDecimals(value: number, decimals: number) {
51+
return value / 10 ** decimals;
52+
}
53+
54+
const ZERO = BigInt(0);
55+
const ONE = BigInt(1);
56+
57+
export const fp64ToFloat = (a: bigint): number => {
58+
// avoid large number
59+
let mask = BigInt("0xffffffff000000000000000000000000");
60+
if ((a & mask) != ZERO) {
61+
throw new Error("too large");
62+
}
63+
64+
// integer part
65+
mask = BigInt("0x10000000000000000");
66+
let base = 1;
67+
let result = 0;
68+
for (let i = 0; i < 32; ++i) {
69+
if ((a & mask) != ZERO) {
70+
result += base;
71+
}
72+
base *= 2;
73+
mask = mask << ONE;
74+
}
75+
76+
// fractional part
77+
mask = BigInt("0x8000000000000000");
78+
base = 0.5;
79+
for (let i = 0; i < 32; ++i) {
80+
if ((a & mask) != ZERO) {
81+
result += base;
82+
}
83+
base /= 2;
84+
mask = mask >> ONE;
85+
}
86+
return result;
87+
};
88+
89+
const strategy = getStrategy().catch(console.error);

0 commit comments

Comments
 (0)