Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AYIN #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions projects/ayin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const alephium = require('../helper/chain/alephium')

const Addresses = {
alphAyinPool: '25ywM8iGxKpZWuGA5z6DXKGcZCXtPBmnbQyJEsjvjjWTy',
alphUsdtPool: '2A5R8KZQ3rhKYrW7bAS4JTjY9FCFLJg6HjQpqSFZBqACX',
alphWethPool: 'yXMFxdoKcE86W9NAyajc8Z3T3k2f5FGiHqHtuA69DYT1',
ayinUsdtPool: '21NEBCk8nj5JBKpS7eN8kX6xGJoLHNqTS3WBFnZ7q8L9m',
usdt: 'zSRgc7goAYUgYsEBYdAzogyyeKv3ne3uvWb3VDtxnaEK',
weth: 'vP6XSUyjmgWCB2B9tD5Rqun56WJqDdExWnfwZVEqzhQb',
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
].map(poolAddress => alephium.getAlphBalance(poolAddress)))
const alphTvl = alphTvls.reduce((tvl, res) => tvl + Number(res.balance), 0)
const tokensTvls = await Promise.all([
Addresses.alphAyinPool, Addresses.alphUsdtPool, Addresses.alphWethPool, Addresses.ayinUsdtPool
].map(poolAddress => alephium.getTokensBalance(poolAddress)))
const tokensTvl = tokensTvls.reduce((res, tokenTvls) => {
tokenTvls.forEach(tokenTvl => {
if (res[tokenTvl.tokenId] !== undefined) {
res[tokenTvl.tokenId] = Number(res[tokenTvl.tokenId]) + Number(tokenTvl.balance)
}
});
return res
}, {[TokenIds.ayin]: 0, [TokenIds.usdt]: 0, [TokenIds.weth]: 0})
const xAyinTvl = await ayinTvlForXAyin()
return {
alephium: alphTvl / 1e18,
ayin: tokensTvl[TokenIds.ayin] / 1e18 + xAyinTvl,
weth: tokensTvl[TokenIds.weth] / 1e18,
tether: tokensTvl[TokenIds.usdt] / 1e6
}
}

module.exports = {
timetravel: false,
methodology: 'TVL locked in the Ayin pools on Alephium',
alephium: { tvl }
}
49 changes: 49 additions & 0 deletions projects/helper/chain/alephium.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const axios = require("axios")
const basex = require('base-x')

const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
const bs58 = basex(ALPHABET)

const EXPLORER_API_HOST = 'https://backend-v115.mainnet.alephium.org'
const NODE_API_HOST = 'https://chadnode.ayin.app'

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

async function getTokensBalance(address) {
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) {
const decoded = bs58.decode(address)

if (decoded.length == 0) throw new Error('Address string is empty')
const addressType = decoded[0]
const addressBody = decoded.slice(1)

if (addressType == 0x03) {
return Buffer.from(addressBody).toString('hex')
} else {
throw new Error(`Invalid contract address type: ${addressType}`)
}
}

module.exports = {
getAlphBalance,
getTokensBalance,
contractIdFromAddress,
contractMultiCall
}