Skip to content

Commit

Permalink
Sake-Finance on Soneium (#13098)
Browse files Browse the repository at this point in the history
  • Loading branch information
waynebruce0x authored Jan 15, 2025
2 parents e2ebeef + c9d1cef commit 13748cd
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions projects/sake-finance/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const abi = {
getReserveTokensAddresses: "function getReserveTokensAddresses(address asset) view returns (address aTokenAddress, address stableDebtTokenAddress, address variableDebtTokenAddress)",
getAllReservesTokens: "function getAllReservesTokens() view returns ((string symbol, address tokenAddress)[])",
getReserveData: "function getReserveData(address asset) view returns (uint256 unbacked, uint256 accruedToTreasuryScaled, uint256 totalAToken, uint256 totalStableDebt, uint256 totalVariableDebt, uint256 liquidityRate, uint256 variableBorrowRate, uint256 stableBorrowRate, uint256 averageStableBorrowRate, uint256 liquidityIndex, uint256 variableBorrowIndex, uint40 lastUpdateTimestamp)",
};

const CONFIG = {
soneium: ['0x2BECa16DAa6Decf9C6F85eBA8F0B35696A3200b3']
};

const fetchReserveData = async (api, poolDatas, isBorrowed) => {
const reserveTokens = await api.multiCall({ calls: poolDatas, abi: abi.getAllReservesTokens });
console.log("reserveTokens:", reserveTokens) // 檢查獲取的代幣列表

const calls = []

poolDatas.map((pool, i) => {
reserveTokens[i].forEach(({ tokenAddress }) => calls.push({ target: pool, params: tokenAddress }));
});
const reserveData = await api.multiCall({ abi: isBorrowed ? abi.getReserveData : abi.getReserveTokensAddresses, calls, })
const tokensAndOwners = []
reserveData.forEach((data, i) => {
const token = calls[i].params
if (isBorrowed) {
api.add(token, data.totalVariableDebt)
api.add(token, data.totalStableDebt)
} else
tokensAndOwners.push([token, data.aTokenAddress])
})

if (isBorrowed) return api.getBalances()
return api.sumTokens({ tokensAndOwners })
}

module.exports.methodology = "Counts the tokens locked in the contracts to be used as collateral to borrow or to earn yield. Borrowed coins are not counted towards the TVL, so only the coins actually locked in the contracts are counted. There's multiple reasons behind this but one of the main ones is to avoid inflating the TVL through cycled lending."

Object.keys(CONFIG).forEach((chain) => {
const poolDatas = CONFIG[chain];
module.exports[chain] = {
tvl: (api) => fetchReserveData(api, poolDatas),
borrowed: (api) => fetchReserveData(api, poolDatas, true),
};
});

0 comments on commit 13748cd

Please sign in to comment.