From 5c1a562e73ddf80b21d957ab955f198df80b1889 Mon Sep 17 00:00:00 2001 From: okorieebube Date: Tue, 22 Oct 2024 05:46:48 +0100 Subject: [PATCH 1/3] feat: add new chain assetchain --- .gitignore | 3 +- fees/assetchain.ts | 79 ++++++++++++++++++++++++++++++++++++++++++++++ helpers/chains.ts | 1 + 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 fees/assetchain.ts diff --git a/.gitignore b/.gitignore index 6aae246f22..fb2ebfc7d0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ node_modules .vscode .DS_store .env -.idea \ No newline at end of file +.idea +yarn.lock \ No newline at end of file diff --git a/fees/assetchain.ts b/fees/assetchain.ts new file mode 100644 index 0000000000..2a8342e04f --- /dev/null +++ b/fees/assetchain.ts @@ -0,0 +1,79 @@ +import { Adapter, ProtocolType } from "../adapters/types"; +import { CHAIN } from "../helpers/chains"; +import { BigNumberish, ethers } from 'ethers'; +import { httpGet } from "../utils/fetchURL"; + +const provider = new ethers.JsonRpcProvider('https://mainnet-rpc.assetchain.org'); +const contractAddress = '0xFC00FACE00000000000000000000000000000000'; + + +async function getFees24Hr() { + try { + const abi = [ + 'function currentSealedEpoch() view returns (uint256)', + 'function getEpochSnapshot(uint256 epoch) view returns (uint256 endTime, uint256 epochFee, uint256 totalBaseRewardWeight, uint256 totalTxRewardWeight, uint256 baseRewardPerSecond, uint256 totalStake, uint256 totalSupply)' + ]; + const contract = new ethers.Contract(contractAddress, abi, provider); + const currentEpoch = await contract.currentSealedEpoch(); + + // Calculate how many 4-hour epochs are in 24 hours (6 epochs) + const epochsToFetch = 6; + const epochs: any = []; + + // Create array of epoch numbers to fetch (current and previous 5) + for (let i = 0; i < epochsToFetch; i++) epochs.push(currentEpoch.toString() - i); + + // Fetch all epoch data in parallel + const epochData = await Promise.all(await epochs.map((epoch: any) => contract.getEpochSnapshot(epoch))); + + // Calculate total fees and volume + let totalFees: BigInt = BigInt(0); + epochData.forEach((data: any) => { + const { epochFee } = data; + totalFees = totalFees += epochFee; + }); + + return ethers.formatEther(totalFees as unknown as BigNumberish); + + } catch (error) { + console.error('Error fetching fees:', error); + throw error; + } +} + +export async function convertRwaToUsd(amountInRwa: number) { + const url = 'https://price.assetchain.org/api/v1/price'; + const result = await httpGet(url, { + responseType: 'blob', headers: { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36", + "Content-Type": "text/csv; charset=utf-8", + "Accept": "text/csv; charset=utf-8", + "origin": url, + } + }); + const data = JSON.parse(result); + return data.data.price * amountInRwa; +} + +convertRwaToUsd(820); + + +const adapter: Adapter = { + version: 2, + adapter: { + [CHAIN.ASSETCHAIN]: { + fetch: async () => { + const feesInRwa = await getFees24Hr(); + const feesInUsd = await convertRwaToUsd(Number(feesInRwa)) + return { + dailyFees: feesInUsd, + dailyRevenue: feesInUsd + } + }, + start: 1598671449, + }, + }, + protocolType: ProtocolType.CHAIN +} + +export default adapter; diff --git a/helpers/chains.ts b/helpers/chains.ts index 4b3cb16762..358a53ff0e 100644 --- a/helpers/chains.ts +++ b/helpers/chains.ts @@ -1,6 +1,7 @@ // Use export enum CHAIN { ARBITRUM = "arbitrum", + ASSETCHAIN = "assetchain", AVAX = "avax", BLAST = "blast", BAHAMUT = "ftn", From b702dee936793e4a418b9d7e7541586a0dc179ac Mon Sep 17 00:00:00 2001 From: g1nt0ki <99907941+g1nt0ki@users.noreply.github.com> Date: Thu, 24 Oct 2024 09:34:15 +0200 Subject: [PATCH 2/3] code refactor --- .gitignore | 3 +- fees/assetchain.ts | 90 +++++++++++++++------------------------------- package-lock.json | 7 ++-- 3 files changed, 33 insertions(+), 67 deletions(-) diff --git a/.gitignore b/.gitignore index fb2ebfc7d0..6aae246f22 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ node_modules .vscode .DS_store .env -.idea -yarn.lock \ No newline at end of file +.idea \ No newline at end of file diff --git a/fees/assetchain.ts b/fees/assetchain.ts index 2a8342e04f..2387e32bb9 100644 --- a/fees/assetchain.ts +++ b/fees/assetchain.ts @@ -1,79 +1,45 @@ -import { Adapter, ProtocolType } from "../adapters/types"; +import { Adapter, FetchOptions, ProtocolType } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; -import { BigNumberish, ethers } from 'ethers'; import { httpGet } from "../utils/fetchURL"; -const provider = new ethers.JsonRpcProvider('https://mainnet-rpc.assetchain.org'); const contractAddress = '0xFC00FACE00000000000000000000000000000000'; -async function getFees24Hr() { - try { - const abi = [ - 'function currentSealedEpoch() view returns (uint256)', - 'function getEpochSnapshot(uint256 epoch) view returns (uint256 endTime, uint256 epochFee, uint256 totalBaseRewardWeight, uint256 totalTxRewardWeight, uint256 baseRewardPerSecond, uint256 totalStake, uint256 totalSupply)' - ]; - const contract = new ethers.Contract(contractAddress, abi, provider); - const currentEpoch = await contract.currentSealedEpoch(); +async function getFees24Hr(api: any) { + const currentEpoch = await api.call({ abi: 'uint256:currentSealedEpoch', target: contractAddress }) + const snapshotABI = 'function getEpochSnapshot(uint256 epoch) view returns (uint256 endTime, uint256 epochFee, uint256 totalBaseRewardWeight, uint256 totalTxRewardWeight, uint256 baseRewardPerSecond, uint256 totalStake, uint256 totalSupply)' - // Calculate how many 4-hour epochs are in 24 hours (6 epochs) - const epochsToFetch = 6; - const epochs: any = []; + // Calculate how many 4-hour epochs are in 24 hours (6 epochs) + const epochsToFetch = 6; + const epochs: any = []; - // Create array of epoch numbers to fetch (current and previous 5) - for (let i = 0; i < epochsToFetch; i++) epochs.push(currentEpoch.toString() - i); - - // Fetch all epoch data in parallel - const epochData = await Promise.all(await epochs.map((epoch: any) => contract.getEpochSnapshot(epoch))); - - // Calculate total fees and volume - let totalFees: BigInt = BigInt(0); - epochData.forEach((data: any) => { - const { epochFee } = data; - totalFees = totalFees += epochFee; - }); - - return ethers.formatEther(totalFees as unknown as BigNumberish); - - } catch (error) { - console.error('Error fetching fees:', error); - throw error; - } + // Create array of epoch numbers to fetch (current and previous 5) + for (let i = 0; i < epochsToFetch; i++) epochs.push(+currentEpoch - i); + const epochData = await api.multiCall({ abi: snapshotABI, calls: epochs, target: contractAddress }) + return epochData.reduce((acc: any, data: any) => acc + +data.epochFee, 0) } -export async function convertRwaToUsd(amountInRwa: number) { - const url = 'https://price.assetchain.org/api/v1/price'; - const result = await httpGet(url, { - responseType: 'blob', headers: { - "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36", - "Content-Type": "text/csv; charset=utf-8", - "Accept": "text/csv; charset=utf-8", - "origin": url, - } - }); - const data = JSON.parse(result); - return data.data.price * amountInRwa; +export async function getPrice() { + const url = 'https://price.assetchain.org/api/v1/price'; + const { data: { price }} = await httpGet(url) + return price } -convertRwaToUsd(820); - - const adapter: Adapter = { - version: 2, - adapter: { - [CHAIN.ASSETCHAIN]: { - fetch: async () => { - const feesInRwa = await getFees24Hr(); - const feesInUsd = await convertRwaToUsd(Number(feesInRwa)) - return { - dailyFees: feesInUsd, - dailyRevenue: feesInUsd - } - }, - start: 1598671449, - }, + version: 2, + adapter: { + [CHAIN.ASSETCHAIN]: { + fetch: async ({ api, createBalances }: FetchOptions) => { + const feesInRwa = await getFees24Hr(api) + const dailyFees = createBalances() + const price = await getPrice() + dailyFees.addUSDValue(price * feesInRwa/1e18) + return { dailyFees, dailyRevenue: dailyFees } + }, + start: 1598671449, }, - protocolType: ProtocolType.CHAIN + }, + protocolType: ProtocolType.CHAIN } export default adapter; diff --git a/package-lock.json b/package-lock.json index 3e87d23b18..37575548ea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -949,9 +949,10 @@ } }, "node_modules/@defillama/sdk": { - "version": "5.0.92", - "resolved": "https://registry.npmjs.org/@defillama/sdk/-/sdk-5.0.92.tgz", - "integrity": "sha512-t6A4qSX9y1VPIaiVE7/O1UPrkDmljjiGv8DrtKZuzuCpwMsL2uMDNIn5a8WLnQizft8mUV3YY4aojx6WC7L7Sw==", + "version": "5.0.94", + "resolved": "https://registry.npmjs.org/@defillama/sdk/-/sdk-5.0.94.tgz", + "integrity": "sha512-XlR5Gawx/uQXvkWZN962DBGXz2+6W4L8NaDIJ3b04vkD2QRhBYAftTKZfHgsYqBh/dFCOGQxh5P0jM49sf8dFw==", + "license": "ISC", "dependencies": { "@aws-sdk/client-s3": "^3.400.0", "@elastic/elasticsearch": "^8.13.1", From f35ebb56a0af4bb8884370d6643d7afb8d3167b8 Mon Sep 17 00:00:00 2001 From: g1nt0ki <99907941+g1nt0ki@users.noreply.github.com> Date: Fri, 25 Oct 2024 10:09:35 +0200 Subject: [PATCH 3/3] use cg price --- fees/assetchain.ts | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/fees/assetchain.ts b/fees/assetchain.ts index 2387e32bb9..861ac88d68 100644 --- a/fees/assetchain.ts +++ b/fees/assetchain.ts @@ -1,6 +1,5 @@ import { Adapter, FetchOptions, ProtocolType } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; -import { httpGet } from "../utils/fetchURL"; const contractAddress = '0xFC00FACE00000000000000000000000000000000'; @@ -19,12 +18,6 @@ async function getFees24Hr(api: any) { return epochData.reduce((acc: any, data: any) => acc + +data.epochFee, 0) } -export async function getPrice() { - const url = 'https://price.assetchain.org/api/v1/price'; - const { data: { price }} = await httpGet(url) - return price -} - const adapter: Adapter = { version: 2, adapter: { @@ -32,8 +25,7 @@ const adapter: Adapter = { fetch: async ({ api, createBalances }: FetchOptions) => { const feesInRwa = await getFees24Hr(api) const dailyFees = createBalances() - const price = await getPrice() - dailyFees.addUSDValue(price * feesInRwa/1e18) + dailyFees.addCGToken('xend-finance', feesInRwa/1e18) return { dailyFees, dailyRevenue: dailyFees } }, start: 1598671449,