@bancor/carbon-sdk
Advanced tools
Comparing version 0.0.16-DEV to 0.0.17-DEV
@@ -5,3 +5,3 @@ { | ||
"source": "src/index.ts", | ||
"version": "0.0.16-DEV", | ||
"version": "0.0.17-DEV", | ||
"description": "The SDK is a READ-ONLY tool, intended to facilitate working with Carbon contracts. It's a convenient wrapper around our matching algorithm, allowing programs and users get a ready to use transaction data that will allow them to manage strategies and fulfill trades", | ||
@@ -8,0 +8,0 @@ "main": "./dist/sdk.umd.js", |
@@ -1,2 +0,2 @@ | ||
import { BigNumber, Decimal } from './numerics'; | ||
import { BigNumber, Decimal, tenPow } from './numerics'; | ||
import { DecodedOrder, DecodedStrategy, FriendlyStrategy } from './types'; | ||
@@ -101,4 +101,2 @@ import { | ||
const TEN = new Decimal(10); | ||
const normalizeRate = ( | ||
@@ -109,5 +107,5 @@ amount: string, | ||
) => { | ||
const decimalDiff = amountTokenDecimals - otherTokenDecimals; | ||
return new Decimal(amount).times(TEN.pow(decimalDiff)).toFixed(); | ||
return new Decimal(amount) | ||
.times(tenPow(amountTokenDecimals, otherTokenDecimals)) | ||
.toFixed(); | ||
}; | ||
@@ -122,4 +120,6 @@ | ||
const decimalDiff = otherTokenDecimals - amountTokenDecimals; | ||
return new Decimal(1).div(amount).times(TEN.pow(decimalDiff)).toFixed(); | ||
return new Decimal(1) | ||
.div(amount) | ||
.times(tenPow(otherTokenDecimals, amountTokenDecimals)) | ||
.toFixed(); | ||
}; | ||
@@ -126,0 +126,0 @@ |
@@ -16,3 +16,3 @@ import { PopulatedTransaction } from '@ethersproject/contracts'; | ||
} from './types'; | ||
import { BigNumber } from './numerics'; | ||
import { BigNumber, Decimal, tenPow } from './numerics'; | ||
import { buildStrategyObject, formatStrategy } from './friendlyUtils'; | ||
@@ -122,2 +122,25 @@ import { decodeStrategy, encodeStrategy } from './encoders'; | ||
/** | ||
* Returns liquidity for a given pair. | ||
* | ||
* @param {string} token0 the first token of the pair | ||
* @param {string} token1 the second token of the pair | ||
* @returns {Promise<String>} liquidity value as string | ||
* @throws {Error} If `startDataSync` has not been called. | ||
*/ | ||
public async getLiquidityByPair(token0: string, token1: string) { | ||
if (!this._cachedOrders) { | ||
throw new Error(`initDataSync must be called prior to this action`); | ||
} | ||
const orders = this._cachedOrders.getOrders(token0, token1); | ||
const liquidityWei = Object.entries(orders).reduce( | ||
(acc, [_, { y }]) => acc.add(y), | ||
BigNumber.from(0) | ||
); | ||
const decimals = await Decimals.getInstance().fetchDecimals(token0); | ||
return formatUnits(liquidityWei, decimals); | ||
} | ||
/** | ||
* Gets the strategies that are owned by the user. | ||
@@ -186,3 +209,3 @@ * It does so by reading the voucher token and | ||
totalOutput: string; | ||
effectiveRate: BigNumber; | ||
effectiveRate: string; | ||
}> { | ||
@@ -233,6 +256,11 @@ if (!this._cachedOrders) { | ||
totalOutput: '0', | ||
effectiveRate: BigNumber.from(0), | ||
effectiveRate: '0', | ||
}; | ||
} | ||
const effectiveRate = new Decimal(totalInput.toString()) | ||
.div(totalOutput.toString()) | ||
.times(tenPow(targetDecimals, sourceDecimals)) | ||
.toString(); | ||
return { | ||
@@ -243,3 +271,3 @@ tradeActions, | ||
totalOutput: formatUnits(totalOutput, targetDecimals), | ||
effectiveRate: totalOutput.div(totalInput), | ||
effectiveRate, | ||
}; | ||
@@ -246,0 +274,0 @@ } |
@@ -12,3 +12,9 @@ import { BigNumber } from '@ethersproject/bignumber'; | ||
const ONE = 2 ** 32; | ||
const TEN = new Decimal(10); | ||
export { Decimal, BigNumber, ONE }; | ||
const tenPow = (dec0: number, dec1: number) => { | ||
const diff = dec0 - dec1; | ||
return TEN.pow(diff); | ||
}; | ||
export { Decimal, BigNumber, ONE, TEN, tenPow }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
730573
30867
0