@defillama/sdk 5.0.1 → 5.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -109,11 +109,41 @@ function sumSingleBalance(balances, token, balance, chain) {
109
109
  balances[token] = value;
110
110
  }
111
111
  else {
112
- var prevBalance = BigInt(balances.hasOwnProperty(token) ? balances[token] : '0');
113
- var value = (prevBalance + BigInt(balance));
112
+ var prevBalance = getBigInt(balances[token]);
113
+ var value = (prevBalance + getBigInt(balance));
114
114
  isValidNumber(Number(value));
115
115
  balances[token] = Number(value).toString();
116
116
  }
117
+ function getBigInt(value) {
118
+ var balance = value;
119
+ if (!value)
120
+ return BigInt(0);
121
+ if (typeof value === 'bigint')
122
+ return value;
123
+ if (typeof value === 'number')
124
+ value = value.toString();
125
+ if (typeof value === 'string') {
126
+ if (value.includes('e')) { // Convert scientific notation to standard notation
127
+ var _a = value.split('e'), base = _a[0], e = _a[1];
128
+ var _b = base.split('.'), lead = _b[0], decimal = _b[1];
129
+ var final = void 0;
130
+ var exponent = +e;
131
+ if (exponent >= 0) {
132
+ final = lead + (decimal || '') + '0'.repeat(exponent - (decimal || '').length);
133
+ }
134
+ else {
135
+ // NOTE: unforutenately BigInt doesn't support negative exponents, so this ends up being 0
136
+ exponent = exponent * -1;
137
+ final = BigInt(lead) / BigInt('1' + Array(exponent).fill(0).join(''));
138
+ }
139
+ return BigInt(final);
140
+ }
141
+ return BigInt(value);
142
+ }
143
+ if (typeof value === 'string')
144
+ return BigInt(value);
145
+ throw new Error("Invalid balance: ".concat(balance));
146
+ }
117
147
  function isValidNumber(value) {
118
148
  if (typeof value === 'bigint')
119
149
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defillama/sdk",
3
- "version": "5.0.1",
3
+ "version": "5.0.2",
4
4
  "description": "SDK for calculating TVL",
5
5
  "main": "build/index.js",
6
6
  "scripts": {