|
| 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