Skip to content

Commit

Permalink
REFACTORING Included borrow apr and fixed url [DONE]
Browse files Browse the repository at this point in the history
  • Loading branch information
Sahitya777 committed Jul 3, 2024
1 parent 8ff1248 commit 8498cd3
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 4 deletions.
27 changes: 27 additions & 0 deletions src/adaptors/hashstack/abis/erc20abi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const erc20 = [
{
inputs: [
{
name: "account",
type: "felt"
}
],
name: "balanceOf",
outputs: [
{
name: "balance",
type: "Uint256"
}
],
stateMutability: "view",
type: "function"
}
]

const erc20abi = {};
erc20.forEach((i) => (erc20abi[i.name] = i));

module.exports = {
erc20abi,
};

98 changes: 94 additions & 4 deletions src/adaptors/hashstack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { uint256 } = require('starknet');
const { call } = require('../../helper/starknet');
const { metricsAbi } = require('./abis/metricsAbi');
const { default: BigNumber } = require('bignumber.js');

const { erc20abi } = require('./abis/erc20abi');
const oracle =
'0x07b05e8dc9c770b72befcf09599132093cf9e57becb2d1b3e89514e1f9bdf0ab';

Expand Down Expand Up @@ -160,14 +160,102 @@ const getTokenPrice = async (token) => {

const market= '0x548f38cb45720a101a1ec2edfaf608b47d2b39d137d0d3134087315f1b5f4a5'

async function getSpendBalances() {
const marketStats=[];

try {
const promises = [];
for (let i = 0; i < TOKENS?.length; ++i) {
const token = TOKENS[i];

const res=call({
abi:erc20abi?.balanceOf,
target:token?.address,
params:[token?.dToken],
allAbi:[],
})

promises.push(res);
}

return new Promise((resolve, _) => {
Promise.allSettled([...promises]).then((val) => {
const results = val.map((stat, idx) => {
if (
stat?.status == "fulfilled" &&
stat?.value
) {
return {
token: TOKENS[idx]?.name,
balance:
BigNumber(stat?.value).div(BigNumber(`1e${TOKENS[idx]?.decimals}`)).toNumber(),
};
} else return marketStats;
});
resolve(results);
});
});
} catch (e) {
return marketStats;
}
}

async function netStrkBorrow(){
let netstrk=0;
let strkData=await getStarknetFoundationIncentives();
if (strkData != null) {
let netallocation = 0
for (let token in strkData) {
if (strkData.hasOwnProperty(token)) {
const array = strkData[token]
const lastObject = array[array.length - 1]
netallocation += 0.3 * (lastObject?.allocation ?lastObject?.allocation:0)
}
}
netstrk=netallocation
} else {
netstrk=0
}
return netstrk;
}

async function getStarknetFoundationIncentives() {
const { data } = await axios.get(starknetFoundationIncentivesEndpoint);
return data['Hashstack'];
}

async function netSpendbalance(){
let netbalance = 0;
const spendBalances=await getSpendBalances();
for(var i=0;i<spendBalances.length;i++){
const priceInUsd = await getTokenPrice(TOKENS[i]?.address);
const res = await call({
abi: metricsAbi?.get_protocol_stats,
target:
market,
params: [TOKENS[i]?.address],
allAbi: [],
});
if(res && priceInUsd ){
if(TOKENS[i]?.name==="BTC" || TOKENS[i]?.name==="DAI"){
let value = 0
netbalance += value
}else{
let value=(BigNumber(res?.total_borrow.toString()).div(BigNumber(`1e${TOKENS[i]?.decimals}`))-spendBalances[i].balance)*priceInUsd
netbalance += value
}
}
}
TOKENS.map(async(token,i)=>{
})
return netbalance
}

async function apy() {
const incentives = await getStarknetFoundationIncentives();
const strkPrice=await getTokenPrice('0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d')
const netSpendBalance=await netSpendbalance();
const netsrtkborrow=await netStrkBorrow();
const promises =
TOKENS.map(async (token, i) => {
const priceInUsd = await getTokenPrice(token?.address);
Expand Down Expand Up @@ -198,17 +286,19 @@ async function apy() {
100 *
tokenIncentive[tokenIncentive.length - 1]['allocation']*0.7*strkPrice/ tokenIncentive[tokenIncentive.length - 1]['supply_usd']
: 0,
rewardTokens:tokenIncentive ? ['0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d']:[],
rewardTokens:tokenIncentive ? ['0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d']:[],
apyBaseBorrow:borrow_rate,
apyRewardBorrow:tokenIncentive && tokenIncentive.length > 0
?netsrtkborrow*365*100*strkPrice/netSpendBalance:0,
underlyingTokens:[token?.address],
totalSupplyUsd:totalSupplyUsd.toNumber(),
totalBorrowUsd:totalBorrowUsd.toNumber(),
url:`https://app.hashstack.finance/market`
url:`https://app.hashstack.finance/v1/market`
}
})
return Promise.all(promises)
}

apy()
module.exports = {
apy,
url: 'https://app.hashstack.finance',
Expand Down

0 comments on commit 8498cd3

Please sign in to comment.