fix near helper

This commit is contained in:
g1nt0ki
2022-08-12 18:36:33 +02:00
parent 0a779c3b8b
commit 57c07278a4
3 changed files with 5 additions and 21 deletions

View File

@@ -90,16 +90,13 @@ function sumSingleBalance(balances, token, balance) {
if (name) {
if (decimals)
balance = BigNumber(balance).shiftedBy(-1 * decimals)
balance = balance / (10 ** decimals)
if (!balances[name])
balances[name] = BigNumber(0)
balances[name] = balances[name].plus(balance)
balances[name] = BigNumber(+(balances[name] || 0) + balance).toFixed(0)
return
}
sdk.util.sumSingleBalance(balances, transformAddress(token), balance)
sdk.util.sumSingleBalance(balances, transformAddress(token), BigNumber(balance).toFixed(0))
return balances
}

View File

@@ -1,5 +1,5 @@
const { BigNumber } = require('bignumber.js');
const { call, addTokenBalances, sumSingleBalance } = require('./helper/near');
const { call, addTokenBalances, sumSingleBalance } = require('../helper/near');
const PEMBROCK_CONTRACT = "v1.pembrock.near";
const REF_FINANCE_CONTRACT = "v2.ref-finance.near";

15
test.js
View File

@@ -321,19 +321,6 @@ const ethereumAddress = "0x0000000000000000000000000000000000000000";
const weth = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
const DAY = 24 * 3600;
function sumSingleBalance(balances, token, balance) {
if (typeof balance === 'number') {
const prevBalance = balances[token] ?? 0
if (typeof prevBalance !== 'number') {
throw new Error(`Trying to merge token balance and coingecko amount for ${token}`)
}
balances[token] = prevBalance + balance;
} else {
const prevBalance = BigNumber(balances[token] ?? "0");
balances[token] = prevBalance.plus(balance);
}
}
function fixBalances(balances) {
Object.entries(balances).forEach(([token, value]) => {
let newKey
@@ -341,7 +328,7 @@ function fixBalances(balances) {
else if (!token.includes(':')) newKey = `coingecko:${token}`
if (newKey) {
delete balances[token]
sumSingleBalance(balances, newKey, value)
sdk.util.sumSingleBalance(balances, newKey, value)
}
})
}