From 4082b9286fea9862ff4a849378347e2959921ba7 Mon Sep 17 00:00:00 2001 From: zengzengzenghuy Date: Fri, 6 Oct 2023 23:18:54 +0800 Subject: [PATCH 1/6] Add sDAI adapter --- src/adaptors/sdai/bridgeinterestreceiver.js | 216 +++++++++++++++++++ src/adaptors/sdai/erc20.js | 222 ++++++++++++++++++++ src/adaptors/sdai/index.js | 72 +++++++ 3 files changed, 510 insertions(+) create mode 100644 src/adaptors/sdai/bridgeinterestreceiver.js create mode 100644 src/adaptors/sdai/erc20.js create mode 100644 src/adaptors/sdai/index.js diff --git a/src/adaptors/sdai/bridgeinterestreceiver.js b/src/adaptors/sdai/bridgeinterestreceiver.js new file mode 100644 index 0000000000..159104362e --- /dev/null +++ b/src/adaptors/sdai/bridgeinterestreceiver.js @@ -0,0 +1,216 @@ +module.exports=[ + { + "inputs": [ + { + "internalType": "address", + "name": "_vault", + "type": "address" + } + ], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "inputs": [], + "name": "InvalidInitialization", + "type": "error" + }, + { + "inputs": [], + "name": "NotInitializing", + "type": "error" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "Claimed", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint64", + "name": "version", + "type": "uint64" + } + ], + "name": "Initialized", + "type": "event" + }, + { + "inputs": [], + "name": "claim", + "outputs": [ + { + "internalType": "uint256", + "name": "claimed", + "type": "uint256" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "claimer", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "currentEpochBalance", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "dripRate", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "epochLength", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "initialize", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [], + "name": "lastClaimTimestamp", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "nextClaimEpoch", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "previewClaimable", + "outputs": [ + { + "internalType": "uint256", + "name": "claimable", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "newClaimer", + "type": "address" + } + ], + "name": "setClaimer", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "vault", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "vaultAPY", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "wxdai", + "outputs": [ + { + "internalType": "contract IWXDAI", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "stateMutability": "payable", + "type": "receive" + } + ] \ No newline at end of file diff --git a/src/adaptors/sdai/erc20.js b/src/adaptors/sdai/erc20.js new file mode 100644 index 0000000000..5365f21bc7 --- /dev/null +++ b/src/adaptors/sdai/erc20.js @@ -0,0 +1,222 @@ +module.exports= [ + { + "constant": true, + "inputs": [], + "name": "name", + "outputs": [ + { + "name": "", + "type": "string" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "_spender", + "type": "address" + }, + { + "name": "_value", + "type": "uint256" + } + ], + "name": "approve", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "totalSupply", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "_from", + "type": "address" + }, + { + "name": "_to", + "type": "address" + }, + { + "name": "_value", + "type": "uint256" + } + ], + "name": "transferFrom", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "decimals", + "outputs": [ + { + "name": "", + "type": "uint8" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [ + { + "name": "_owner", + "type": "address" + } + ], + "name": "balanceOf", + "outputs": [ + { + "name": "balance", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "symbol", + "outputs": [ + { + "name": "", + "type": "string" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "_to", + "type": "address" + }, + { + "name": "_value", + "type": "uint256" + } + ], + "name": "transfer", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": true, + "inputs": [ + { + "name": "_owner", + "type": "address" + }, + { + "name": "_spender", + "type": "address" + } + ], + "name": "allowance", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "payable": true, + "stateMutability": "payable", + "type": "fallback" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "name": "spender", + "type": "address" + }, + { + "indexed": false, + "name": "value", + "type": "uint256" + } + ], + "name": "Approval", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "name": "from", + "type": "address" + }, + { + "indexed": true, + "name": "to", + "type": "address" + }, + { + "indexed": false, + "name": "value", + "type": "uint256" + } + ], + "name": "Transfer", + "type": "event" + } +] \ No newline at end of file diff --git a/src/adaptors/sdai/index.js b/src/adaptors/sdai/index.js new file mode 100644 index 0000000000..bba8eb3385 --- /dev/null +++ b/src/adaptors/sdai/index.js @@ -0,0 +1,72 @@ +const utils = require('../utils'); +const bridgeInterestReceiverABI = require('./bridgeinterestreceiver'); +const erc20ABI = require('./erc20'); +const Web3 = require('web3'); + +const chains = { + xdai: { + sDAI: '0xaf204776c7245bF4147c2612BF6e5972Ee483701', + wxDAI: '0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d', + interestreceiver: '0x670daeaF0F1a5e336090504C68179670B5059088', + }, +}; +let web3; +let bridgeInterestReceiver; +let wxDAI; + +async function init() { + try { + web3 = new Web3( + new Web3.providers.HttpProvider('https://rpc.gnosischain.com') + ); + bridgeInterestReceiver = new web3.eth.Contract( + bridgeInterestReceiverABI, + chains.xdai.interestreceiver + ); + wxDAI = new web3.eth.Contract(erc20ABI, chains.xdai.wxDAI); + } catch (e) { + console.log(e); + } +} +async function getApy() { + try { + // call vaultAPY() from bridge interest receiver contract + let vaultAPY = await bridgeInterestReceiver.methods.vaultAPY().call(); + // remain 8 decimals + return Math.round(web3.utils.fromWei(vaultAPY) * 1e8) / 1e8; + } catch (e) { + console.log(e); + } +} +async function getTVL() { + try { + // wxDAI balance of sDAI vault + let tvl = await wxDAI.methods.balanceOf(chains.xdai.sDAI).call(); + // get wxDAI price from coingecko + let wxDAIPrice = await utils.getData( + 'https://api.coingecko.com/api/v3/simple/price?ids=wrapped-xdai&vs_currencies=usd' + ); + // convert into USD + let tvlUSD = (tvl * wxDAIPrice['wrapped-xdai'].usd) / 1e18; + return tvlUSD; + } catch (e) { + console.log(e); + } +} +async function sDAIPool() { + await init(); + const sDAIPoolData = { + pool: chains.xdai.interestreceiver, + chain: utils.formatChain('xdai'), + project: 'sdai', + symbol: 'sDAI', + apy: await getApy(), + tvlUsd: await getTVL(), + }; + return [sDAIPoolData]; +} +module.exports = { + timetravel: false, + apy: sDAIPool, + url: 'https://agave.finance/sdai/', +}; From 0bf525f7bac1358a0f4c4e491d8fdb4294b03b9f Mon Sep 17 00:00:00 2001 From: zengzengzenghuy Date: Fri, 6 Oct 2023 23:57:37 +0800 Subject: [PATCH 2/6] fix: APY decimal error --- src/adaptors/sdai/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adaptors/sdai/index.js b/src/adaptors/sdai/index.js index bba8eb3385..b56c7a2ad6 100644 --- a/src/adaptors/sdai/index.js +++ b/src/adaptors/sdai/index.js @@ -33,7 +33,7 @@ async function getApy() { // call vaultAPY() from bridge interest receiver contract let vaultAPY = await bridgeInterestReceiver.methods.vaultAPY().call(); // remain 8 decimals - return Math.round(web3.utils.fromWei(vaultAPY) * 1e8) / 1e8; + return (Math.round(web3.utils.fromWei(vaultAPY) * 1e8) / 1e8)*100; } catch (e) { console.log(e); } From 1a5b33a9239775c7902983e6054440e556eec16d Mon Sep 17 00:00:00 2001 From: zengzengzenghuy Date: Sat, 7 Oct 2023 00:09:43 +0800 Subject: [PATCH 3/6] fix: tvlusd error --- src/adaptors/sdai/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adaptors/sdai/index.js b/src/adaptors/sdai/index.js index b56c7a2ad6..8dc1637b50 100644 --- a/src/adaptors/sdai/index.js +++ b/src/adaptors/sdai/index.js @@ -48,7 +48,7 @@ async function getTVL() { ); // convert into USD let tvlUSD = (tvl * wxDAIPrice['wrapped-xdai'].usd) / 1e18; - return tvlUSD; + return Math.round(tvlUSD*1e6)/1e6; } catch (e) { console.log(e); } From 0ab09b67a9bc3fd04b997c48db7e14e894c0e9ed Mon Sep 17 00:00:00 2001 From: zengzengzenghuy Date: Sat, 7 Oct 2023 00:18:38 +0800 Subject: [PATCH 4/6] fix: pool address --- src/adaptors/sdai/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adaptors/sdai/index.js b/src/adaptors/sdai/index.js index 8dc1637b50..a918334428 100644 --- a/src/adaptors/sdai/index.js +++ b/src/adaptors/sdai/index.js @@ -56,7 +56,7 @@ async function getTVL() { async function sDAIPool() { await init(); const sDAIPoolData = { - pool: chains.xdai.interestreceiver, + pool: chains.xdai.sDAI, chain: utils.formatChain('xdai'), project: 'sdai', symbol: 'sDAI', From a32a8857a5a42cc9b0053568b16b1ed61a019e8c Mon Sep 17 00:00:00 2001 From: zengzengzenghuy Date: Sat, 7 Oct 2023 12:02:15 +0800 Subject: [PATCH 5/6] use defillama sdk --- src/adaptors/sdai/index.js | 55 +++++++++++++++----------------------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/src/adaptors/sdai/index.js b/src/adaptors/sdai/index.js index a918334428..3f80b1487c 100644 --- a/src/adaptors/sdai/index.js +++ b/src/adaptors/sdai/index.js @@ -1,39 +1,25 @@ const utils = require('../utils'); const bridgeInterestReceiverABI = require('./bridgeinterestreceiver'); -const erc20ABI = require('./erc20'); -const Web3 = require('web3'); +const sdk = require('@defillama/sdk'); const chains = { xdai: { sDAI: '0xaf204776c7245bF4147c2612BF6e5972Ee483701', wxDAI: '0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d', interestreceiver: '0x670daeaF0F1a5e336090504C68179670B5059088', - }, + } }; -let web3; -let bridgeInterestReceiver; -let wxDAI; -async function init() { - try { - web3 = new Web3( - new Web3.providers.HttpProvider('https://rpc.gnosischain.com') - ); - bridgeInterestReceiver = new web3.eth.Contract( - bridgeInterestReceiverABI, - chains.xdai.interestreceiver - ); - wxDAI = new web3.eth.Contract(erc20ABI, chains.xdai.wxDAI); - } catch (e) { - console.log(e); - } -} async function getApy() { try { - // call vaultAPY() from bridge interest receiver contract - let vaultAPY = await bridgeInterestReceiver.methods.vaultAPY().call(); - // remain 8 decimals - return (Math.round(web3.utils.fromWei(vaultAPY) * 1e8) / 1e8)*100; + const vaultAPY = ( + await sdk.api.abi.call({ + chain: 'xdai', + abi: bridgeInterestReceiverABI.find((m) => m.name === 'vaultAPY'), + target: chains.xdai.interestreceiver, + }) + ).output; + return vaultAPY / 1e16; } catch (e) { console.log(e); } @@ -41,20 +27,23 @@ async function getApy() { async function getTVL() { try { // wxDAI balance of sDAI vault - let tvl = await wxDAI.methods.balanceOf(chains.xdai.sDAI).call(); - // get wxDAI price from coingecko - let wxDAIPrice = await utils.getData( - 'https://api.coingecko.com/api/v3/simple/price?ids=wrapped-xdai&vs_currencies=usd' - ); - // convert into USD - let tvlUSD = (tvl * wxDAIPrice['wrapped-xdai'].usd) / 1e18; - return Math.round(tvlUSD*1e6)/1e6; + const tvl = ( + await sdk.api.abi.call({ + chain: 'xdai', + abi: 'erc20:balanceOf', + target: chains.xdai.wxDAI, + params: chains.xdai.sDAI, + }) + ).output; + // get wxDAI price + const wxDAIPrice = await utils.getPrices([chains.xdai.wxDAI], 'xdai'); + const tvlUSD = (tvl * wxDAIPrice.pricesBySymbol.wxdai) / 1e18; + return tvlUSD; } catch (e) { console.log(e); } } async function sDAIPool() { - await init(); const sDAIPoolData = { pool: chains.xdai.sDAI, chain: utils.formatChain('xdai'), From 9d3388497c7d950be30fd1227cc51dc83f19f533 Mon Sep 17 00:00:00 2001 From: 0xngmi <80541789+0xngmi@users.noreply.github.com> Date: Sat, 7 Oct 2023 05:09:28 +0100 Subject: [PATCH 6/6] remove try/catch --- src/adaptors/sdai/index.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/adaptors/sdai/index.js b/src/adaptors/sdai/index.js index 3f80b1487c..ad9f947c71 100644 --- a/src/adaptors/sdai/index.js +++ b/src/adaptors/sdai/index.js @@ -11,7 +11,6 @@ const chains = { }; async function getApy() { - try { const vaultAPY = ( await sdk.api.abi.call({ chain: 'xdai', @@ -20,12 +19,8 @@ async function getApy() { }) ).output; return vaultAPY / 1e16; - } catch (e) { - console.log(e); - } } async function getTVL() { - try { // wxDAI balance of sDAI vault const tvl = ( await sdk.api.abi.call({ @@ -39,9 +34,6 @@ async function getTVL() { const wxDAIPrice = await utils.getPrices([chains.xdai.wxDAI], 'xdai'); const tvlUSD = (tvl * wxDAIPrice.pricesBySymbol.wxdai) / 1e18; return tvlUSD; - } catch (e) { - console.log(e); - } } async function sDAIPool() { const sDAIPoolData = {