Skip to content

Commit

Permalink
Count xAyin
Browse files Browse the repository at this point in the history
  • Loading branch information
h0ngcha0 committed Nov 21, 2023
1 parent 6f87ed6 commit ba1b501
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
16 changes: 15 additions & 1 deletion projects/ayin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,25 @@ const Addresses = {
ayin: 'vT49PY8ksoUL6NcXiZ1t2wAmC7tTPRfFfER8n3UCLvXy'
}

const XAyinAddress = 'zst5zMzizEeFYFis6DNSknY5GCYTpM85D3yXeRLe2ug3'

const TokenIds = {
usdt: alephium.contractIdFromAddress(Addresses.usdt),
weth: alephium.contractIdFromAddress(Addresses.weth),
ayin: alephium.contractIdFromAddress(Addresses.ayin)
}

async function ayinTvlForXAyin() {
const results = await alephium.contractMultiCall([
{ group: 0, address: XAyinAddress, methodIndex: 3 },
{ group: 0, address: XAyinAddress, methodIndex: 11}
])

const totalSupply = results[0].returns[0].value
const currentPrice = results[1].returns[0].value
return (Number(totalSupply) / 1e18) * (Number(currentPrice) / 1e18)
}

async function tvl() {
const alphTvls = await Promise.all([
Addresses.alphAyinPool, Addresses.alphUsdtPool, Addresses.alphWethPool
Expand All @@ -32,9 +45,10 @@ async function tvl() {
});
return res
}, {[TokenIds.ayin]: 0, [TokenIds.usdt]: 0, [TokenIds.weth]: 0})
const xAyinTvl = await ayinTvlForXAyin()
return {
alephium: alphTvl / 1e18,
ayin: tokensTvl[TokenIds.ayin] / 1e18,
ayin: tokensTvl[TokenIds.ayin] / 1e18 + xAyinTvl,
weth: tokensTvl[TokenIds.weth] / 1e18,
tether: tokensTvl[TokenIds.usdt] / 1e6
}
Expand Down
22 changes: 18 additions & 4 deletions projects/helper/chain/alephium.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,27 @@ const basex = require('base-x')
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
const bs58 = basex(ALPHABET)

const API_HOST = 'https://backend-v115.mainnet.alephium.org'
const EXPLORER_API_HOST = 'https://backend-v115.mainnet.alephium.org'
const NODE_API_HOST = ''

async function getAlphBalance(address) {
return (await axios.get(`${API_HOST}/addresses/${address}/balance`)).data
return (await axios.get(`${EXPLORER_API_HOST}/addresses/${address}/balance`)).data
}

async function getTokensBalance(address) {
return (await axios.get(`${API_HOST}/addresses/${address}/tokens-balance`)).data
return (await axios.get(`${EXPLORER_API_HOST}/addresses/${address}/tokens-balance`)).data
}

async function contractMultiCall(payload) {
const result = (await axios.post(`${NODE_API_HOST}/contracts/multicall-contract`, {calls: payload})).data
return result.results.map((r) => tryGetCallResult(r))
}

function tryGetCallResult(result) {
if (result.type === 'CallContractFailed') {
throw new Error(`Failed to call contract, error: ${result.error}`)
}
return result
}

function contractIdFromAddress(address) {
Expand All @@ -31,5 +44,6 @@ function contractIdFromAddress(address) {
module.exports = {
getAlphBalance,
getTokensBalance,
contractIdFromAddress
contractIdFromAddress,
contractMultiCall
}

0 comments on commit ba1b501

Please sign in to comment.