Skip to content

Instantly share code, notes, and snippets.

@ElliotFriend
Created January 16, 2024 15:34
Show Gist options
  • Save ElliotFriend/3d58164b03563aefeea2e115c7fe6bfa to your computer and use it in GitHub Desktop.
Save ElliotFriend/3d58164b03563aefeea2e115c7fe6bfa to your computer and use it in GitHub Desktop.
defillama-helpers
const ADDRESSES = require('../coreAssets.json')
const { Account, Keypair, TransactionBuilder, Contract, SorobanRpc, Horizon, xdr } = require("@stellar/stellar-sdk")
const SOROBAN_ENDPOINT = "https://soroban-testnet.stellar.org"
const soroban_server = new SorobanRpc.Server(SOROBAN_ENDPOINT)
const HORIZON_ENDPOINT = "https://horizon.stellar.org"
const horizon_server = new Horizon.Server(HORIZON_ENDPOINT)
const tokenMapping = {
'native': { name: 'stellar' },
'USDC:GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN': { name: 'usd-coin' },
'EURC:GDHU6WRG4IEQXM5NZ4BMPKOXHW76MZM4Y2IEMFDVXBSDP6SJY4ITNPP2': { name: 'eurc' },
}
/**
* Convert from a number into its stroops value.
* @param {number|string} n Number to calculate the stroops value for.
* @returns {bigint} A BigInt representing the number of stroops in `n`.
*/
function toStroops(n) {
return BigInt(parseFloat(n) * 1e7)
}
/**
* Convert from integer stroops to whole units value.
* @param {number} n Number of stroops to calculate whole units for.
* @returns {string} A stringified number with 7 decimal places.
*/
function fromStroops(n) {
return (parseInt(n) / 1e7).toFixed(7)
}
async function getAssetSupply(assetCode, issuerAddress) {
let assetResp = await horizon_server.assets().forCode(assetCode).forIssuer(issuerAddress).call()
if (assetResp.records.length) {
return assetResp.records[0].amount
}
return 0
}
async function readContractData({ contractAddress, key, durability = 'persistent'}) {
let lkDurability
let ledgerKey = typeof key === 'string' ? xdr.ScVal.scvSymbol(key) : key
switch (durability) {
case 'temporary':
lkDurability = SorobanRpc.Durability.Temporary
case 'instance':
ledgerKey = xdr.ScVal.scvLedgerKeyContractInstance()
lkDurability = SorobanRpc.Durability.Persistent
default:
lkDurability = SorobanRpc.Durability.Persistent
}
const data = await soroban_server.getContractData(contractAddress, ledgerKey, lkDurability)
// console.log('data: ', data.val.value().val().value().storage())
if (durability !== 'instance') {
return data.val.contractData().val().value()
}
let returnValue
data.val.contractData().val().value().storage().some((child) => {
if (Buffer.from(child.key().value()).toString('utf-8') === key) {
returnValue = child.val().value()
return true
}
})
return returnValue
}
async function invokeReadOnlyContractFunction(contractAddress, functionName, args = []) {
const dummyAccount = new Account(Keypair.random().publicKey(), "1")
const tx = new TransactionBuilder(
dummyAccount, {
fee: '100',
networkPassphrase: 'Test SDF Network ; September 2015'
})
.addOperation(new Contract(contractAddress).call(
functionName,
...args
))
.setTimeout(0)
.build()
const simResponse = await soroban_server.simulateTransaction(tx)
if (simResponse.error) {
throw new Error(simResponse.error)
}
return simResponse.result.retval.value()
}
async function getBuiltinLPReserves(lpId) {
const resp = await horizon_server.liquidityPools().liquidityPoolId(lpId).call()
return resp.reserves
}
module.exports = {
toStroops,
fromStroops,
invokeReadOnlyContractFunction,
getBuiltinLPReserves,
getAssetSupply,
readContractData,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment