Skip to content

Commit

Permalink
Merge branch 'apt-847-amount-to-integers'
Browse files Browse the repository at this point in the history
# Conflicts:
#	package.json
#	src/main.test.ts
  • Loading branch information
sirWill committed Nov 17, 2022
2 parents b4c0698 + 3d4e8d6 commit 1ad989c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 73 deletions.
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ const sdk = new SDK({
### You want to convert 15 coins to Decimal type with 8 decimals (coins like APTOS, BTC, etc);

```typescript

const decimalValue = convertValueToDecimal(15, 8);
or // convertValueToDecimal return Decimal type;
const decimalValue2 = convertValueToDecimal('15', 8);
// convertValueToDecimal return Decimal type;
const decimalValue = convertValueToDecimal(15, 8); // 1500000000 (15 coin with 8 decimals)
or
const decimalValue2 = convertValueToDecimal('0.005', 8); // 500000 (0.005 coin with 8 decimals)

```

Expand All @@ -48,21 +48,21 @@ const decimalValue2 = convertValueToDecimal('15', 8);
// Get USDT amount
try {
const output = await sdk.Swap.calculateRates({
fromToken: '0x1::aptos_coin::AptosCoin',
toToken: '0xf22bede237a07e121b56d91a491eb7bcdfd1f5907926a9e58338f964a01b17fa::asset::USDT', // layerzero USDT
amount: 1, // 1 APTOS as Decimal amount
curveType: 'uncorrelated',
interactiveToken: 'from',
fromToken: '0x1::aptos_coin::AptosCoin', // full 'from' token address
toToken: '0xf22bede237a07e121b56d91a491eb7bcdfd1f5907926a9e58338f964a01b17fa::asset::USDT', // full 'to' token address layerzero USDT
amount: 100000000, // 1 APTOS, or you can use convertValueToDecimal(1, 8)
curveType: 'uncorrelated', // can be 'uncorrelated' or 'stable'
interactiveToken: 'from', // which token is 'base' to calculate other token rate.
})
console.log(amount) // '4.304638' USDT
console.log(output) // '4304638' (4.304638 USDT)

// Generate TX payload for swap 1 APTOS to maximum 4.304638 USDT
// and minimum 4.283115 USDT (with slippage -0.5%)
const txPayload = sdk.Swap.createSwapTransactionPayload({
fromToken: '0x1::aptos_coin::AptosCoin',
toToken: '0xf22bede237a07e121b56d91a491eb7bcdfd1f5907926a9e58338f964a01b17fa::asset::USDT', // layerzero USDT
fromAmount: convertValueToDecimal(1, 8), // 1 APTOS,
toAmount: convertValueToDecimal(4.304638, 6), // 4.304638 USDT,
fromAmount: 100000000, // 1 APTOS, or you can use convertValueToDecimal(1, 8)
toAmount: 4304638, // 4.304638 USDT, or you can use convertValueToDecimal(4.304638, 6)
interactiveToken: 'from',
slippage: 0.005, // 0.5% (1 - 100%, 0 - 0%)
stableSwapType: 'high',
Expand Down Expand Up @@ -98,11 +98,11 @@ const decimalValue2 = convertValueToDecimal('15', 8);
const amount = await sdk.Swap.calculateRates({
fromToken: '0x1::aptos_coin::AptosCoin',
toToken: '0xf22bede237a07e121b56d91a491eb7bcdfd1f5907926a9e58338f964a01b17fa::asset::USDT',
amount: 1, // 1 layerzero USDT
amount: 1000000, // 1 layerzero USDT
interactiveToken: 'to',
curveType: 'uncorrelated',
})
console.log(amount) // '0.23211815' APTOS
console.log(amount) // '23211815' ('0.23211815' APTOS)

// Generate TX payload for get EXACTLY 1 USDT
// and minimum send 0.23327874 (with slippage +0.5%)
Expand Down Expand Up @@ -145,11 +145,11 @@ const decimalValue2 = convertValueToDecimal('15', 8);
const amount = await sdk.Swap.calculateRates({
fromToken: '0x1::aptos_coin::AptosCoin',
toToken: '0xcc8a89c8dce9693d354449f1f73e60e14e347417854f029db5bc8e7454008abb::coin::T', // wormhole WETH (whWETH)
amount: 1, // 1 APTOS
amount: 100000000, // 1 APTOS
interactiveToken: 'from',
curveType: 'stable',
})
console.log(amount) // '0.00175257' whWETH
console.log(amount) // '175257' ('0.00175257' whWETH)

// Generate TX payload to swap 1 APTOS to
// and minimum send 0.00174381 (with slippage -0.5%)
Expand Down Expand Up @@ -192,11 +192,11 @@ const decimalValue2 = convertValueToDecimal('15', 8);
const amount = await sdk.Swap.calculateRates({
fromToken: '0x1::aptos_coin::AptosCoin',
toToken: '0x1000000fa32d122c18a6a31c009ce5e71674f22d06a581bb0a15575e6addadcc::usda::USDA', // USDA
amount: 1, // 1 USDA
amount: 1000000, // 1 USDA
interactiveToken: 'to',
curveType: 'stable',
})
console.log(amount) // '0.12356861' APTOS
console.log(amount) // '12356861' ('0.12356861' APTOS)

// Generate TX payload to swap 1 APTOS to
// and minimum send 0.12418645 (with slippage +0.5%)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pontem/liquidswap-sdk",
"version": "0.3.1",
"version": "0.3.2",
"description": "SDK for use any functions of LiquidSwap",
"author": "Igor Demko <igor@pontem.network>",
"repository": "https://github.com/pontem-network/liquidswap-sdk",
Expand Down
71 changes: 31 additions & 40 deletions src/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CURVE_STABLE, CURVE_UNCORRELATED } from './constants/index';
import SDK from './main';
import { d, decimalsMultiplier, convertValueToDecimal } from "./utils";
import { convertValueToDecimal } from "./utils";

const TokensMapping: Record<string, string> = {
APTOS: '0x1::aptos_coin::AptosCoin',
Expand All @@ -9,19 +9,6 @@ const TokensMapping: Record<string, string> = {
WETH: '0xcc8a89c8dce9693d354449f1f73e60e14e347417854f029db5bc8e7454008abb::coin::T' // wormhole WETH
};

const CoinInfo: Record<string, { decimals: number; }> = {
APTOS: { decimals: 8 },
USDT: { decimals: 6 },
BTC: { decimals: 8 },
WETH: { decimals: 8 }
};

function convertToDecimals(amount: number | string, token: string) {
const mul = decimalsMultiplier(CoinInfo[token]?.decimals || 0);

return d(amount).mul(mul);
}

describe('Swap Module', () => {
const sdk = new SDK({
nodeUrl: 'https://fullnode.mainnet.aptoslabs.com/v1',
Expand All @@ -30,12 +17,16 @@ describe('Swap Module', () => {
const output = await sdk.Swap.calculateRates({
fromToken: TokensMapping.APTOS,
toToken: TokensMapping.USDT,
amount: 1,
amount: 100000000, // 1 APTOS
curveType: 'uncorrelated',
interactiveToken: 'from',
});

<<<<<<< HEAD
console.log(`1 APT → ${output} USDT`);
=======
console.log(`100000000 APT → ${output} USDT`);
>>>>>>> apt-847-amount-to-integers

expect(typeof output).toBe('string');
expect(output.length).toBeGreaterThan(0);
Expand All @@ -45,12 +36,12 @@ describe('Swap Module', () => {
const output = await sdk.Swap.calculateRates({
fromToken: TokensMapping.APTOS,
toToken: TokensMapping.USDT,
amount: 1,
amount: 1000000, // 1 USDT
curveType: 'uncorrelated',
interactiveToken: 'to',
});

console.log(`${output} APT → 1 USDT`);
console.log(`${output} APT → 1000000 USDT`);

expect(typeof output).toBe('string');
expect(output.length).toBeGreaterThan(0);
Expand All @@ -60,12 +51,12 @@ describe('Swap Module', () => {
const output = await sdk.Swap.calculateRates({
fromToken: TokensMapping.APTOS,
toToken: TokensMapping.WETH,
amount: 1,
amount: 100000000, // 1 APTOS
curveType: 'stable',
interactiveToken: 'from',
});

console.log(`1 APT → ${output} WETH`);
console.log(`100000000 APT → ${output} WETH`);

expect(typeof output).toBe('string');
expect(output.length).toBeGreaterThan(0);
Expand All @@ -76,7 +67,7 @@ describe('Swap Module', () => {
await sdk.Swap.calculateRates({
fromToken: TokensMapping.BTC,
toToken: TokensMapping.WETH,
amount: 1,
amount: 100000000, // 1 WETH
curveType: 'stable',
interactiveToken: 'to',
});
Expand All @@ -88,7 +79,7 @@ describe('Swap Module', () => {
await sdk.Swap.calculateRates({
fromToken: TokensMapping.APTOS + '0',
toToken: TokensMapping.WETH,
amount: 1,
amount: 100000000, // 1 WETH
curveType: 'stable',
interactiveToken: 'to',
});
Expand All @@ -100,7 +91,7 @@ describe('Swap Module', () => {
await sdk.Swap.calculateRates({
fromToken: TokensMapping.APTOS,
toToken: TokensMapping.WETH + '0',
amount: 1,
amount: 100000000, // 1 WETH
curveType: 'stable',
interactiveToken: 'to',
});
Expand All @@ -112,7 +103,7 @@ describe('Swap Module', () => {
await sdk.Swap.calculateRates({
fromToken: TokensMapping.APTOS,
toToken: TokensMapping.WETH,
amount: 1,
amount: 100000000, // 1 WETH
curveType: 'stable',
interactiveToken: 'to',
});
Expand All @@ -124,7 +115,7 @@ describe('Swap Module', () => {
await sdk.Swap.calculateRates({
fromToken: TokensMapping.APTOS,
toToken: TokensMapping.WETH,
amount: 0,
amount: 0, // 0 WETH
curveType: 'stable',
interactiveToken: 'to',
});
Expand All @@ -137,8 +128,8 @@ describe('Swap Module', () => {
const output = sdk.Swap.createSwapTransactionPayload({
fromToken: TokensMapping.APTOS,
toToken: TokensMapping.USDT,
fromAmount: convertValueToDecimal(1, 8),
toAmount: convertValueToDecimal('4.304638', 6),
fromAmount: 100000000, // 1 APTOS
toAmount: 4304638, // 4.304638 USDT
interactiveToken: 'from',
slippage: 0.005,
stableSwapType: 'high',
Expand All @@ -161,8 +152,8 @@ describe('Swap Module', () => {
const output = sdk.Swap.createSwapTransactionPayload({
fromToken: TokensMapping.APTOS,
toToken: TokensMapping.USDT,
fromAmount: convertValueToDecimal(0.23211815, 8),
toAmount: convertValueToDecimal(1, 6),
fromAmount: 23211815, // 0.23211815 APTOS
toAmount: 1000000, // 1 USDT
interactiveToken: 'to',
slippage: 0.005,
stableSwapType: 'high',
Expand All @@ -185,8 +176,8 @@ describe('Swap Module', () => {
const output = sdk.Swap.createSwapTransactionPayload({
fromToken: TokensMapping.APTOS,
toToken: TokensMapping.WETH,
fromAmount: convertValueToDecimal(0.04, 8),
toAmount: convertValueToDecimal('0.00037818', 8),
fromAmount: 4000000, // 0.04 APTOS
toAmount: 37818, // 0.00037818 WETH
interactiveToken: 'from',
slippage: 0.005,
stableSwapType: 'high',
Expand All @@ -209,8 +200,8 @@ describe('Swap Module', () => {
const output = sdk.Swap.createSwapTransactionPayload({
fromToken: TokensMapping.APTOS,
toToken: TokensMapping.WETH,
fromAmount: convertToDecimals('0.03998981', 'APTOS'),
toAmount: convertToDecimals('0.0003781', 'WETH'),
fromAmount: convertValueToDecimal('0.03998981', 8),
toAmount: convertValueToDecimal('0.0003781', 8),
interactiveToken: 'to',
slippage: 0.005,
stableSwapType: 'high',
Expand All @@ -233,8 +224,8 @@ describe('Swap Module', () => {
const output = sdk.Swap.createSwapTransactionPayload({
fromToken: TokensMapping.APTOS,
toToken: TokensMapping.WETH,
fromAmount: convertToDecimals('1', 'APTOS'),
toAmount: convertToDecimals('0.00175257', 'WETH'),
fromAmount: convertValueToDecimal('1', 8),
toAmount: convertValueToDecimal('0.00175257', 8),
interactiveToken: 'from',
slippage: 0.005,
stableSwapType: 'normal',
Expand All @@ -257,8 +248,8 @@ describe('Swap Module', () => {
const output = sdk.Swap.createSwapTransactionPayload({
fromToken: TokensMapping.APTOS,
toToken: TokensMapping.WETH,
fromAmount: convertToDecimals('0.00400045', 'APTOS'),
toAmount: convertToDecimals('0.00004339', 'WETH'),
fromAmount: convertValueToDecimal('0.00400045', 8),
toAmount: convertValueToDecimal('0.00004339', 8),
interactiveToken: 'to',
slippage: 0.005,
stableSwapType: 'normal',
Expand All @@ -284,8 +275,8 @@ describe('Swap Module', () => {
sdk.Swap.createSwapTransactionPayload({
fromToken: TokensMapping.APTOS,
toToken: TokensMapping.WETH,
fromAmount: convertToDecimals('1', 'APTOS'),
toAmount: convertToDecimals('0.000846', 'WETH'),
fromAmount: convertValueToDecimal('1', 8),
toAmount: convertValueToDecimal('0.000846', 8),
interactiveToken: 'to',
slippage: -0.01,
stableSwapType: 'high',
Expand All @@ -299,8 +290,8 @@ describe('Swap Module', () => {
sdk.Swap.createSwapTransactionPayload({
fromToken: TokensMapping.APTOS,
toToken: TokensMapping.WETH,
fromAmount: convertToDecimals('1', 'APTOS'),
toAmount: convertToDecimals('0.000846', 'WETH'),
fromAmount: convertValueToDecimal('1', 8),
toAmount: convertValueToDecimal('0.000846', 8),
interactiveToken: 'to',
slippage: 1.01,
stableSwapType: 'high',
Expand Down
21 changes: 7 additions & 14 deletions src/modules/SwapModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import {
getCoinsInWithFeesStable,
d,
is_sorted,
convertValueToDecimal,
convertDecimalToFixedString
} from "../utils";
import {
MODULES_ACCOUNT,
Expand All @@ -32,16 +30,16 @@ import {
export type CalculateRatesParams = {
fromToken: AptosResourceType;
toToken: AptosResourceType;
amount: number;
amount: Decimal | number;
interactiveToken: 'from' | 'to';
curveType: CurveType;
};

export type CreateTXPayloadParams = {
fromToken: AptosResourceType;
toToken: AptosResourceType;
fromAmount: Decimal;
toAmount: Decimal;
fromAmount: Decimal | number;
toAmount: Decimal | number;
interactiveToken: 'from' | 'to';
slippage: number;
stableSwapType: 'high' | 'normal';
Expand Down Expand Up @@ -114,9 +112,7 @@ export class SwapModule implements IModule {
const coinFromDecimals = +sortedFromCoinInfo.data.decimals;
const coinToDecimals = +sortedToCoinInfo.data.decimals;

const amount = params.interactiveToken === 'from'
? convertValueToDecimal(params.amount, coinToDecimals)
: convertValueToDecimal(params.amount, coinFromDecimals);
const amount = d(params.amount);

if (amount.comparedTo(0) === 0) {
throw new Error(`Amount equals zero or undefined`);
Expand Down Expand Up @@ -151,10 +147,7 @@ export class SwapModule implements IModule {
fee
);
}
const outputRate = params.interactiveToken === 'from'
? convertDecimalToFixedString(rate, coinFromDecimals)
: convertDecimalToFixedString(rate, coinToDecimals);
return outputRate;
return rate.toFixed(0);
}

createSwapTransactionPayload(params: CreateTXPayloadParams): TAptosTxPayload {
Expand Down Expand Up @@ -186,11 +179,11 @@ export class SwapModule implements IModule {
const fromAmount =
params.interactiveToken === 'from'
? params.fromAmount
: withSlippage(slippage, params.fromAmount, true).toFixed(0);
: withSlippage(slippage, d(params.fromAmount), true).toFixed(0);
const toAmount =
params.interactiveToken === 'to'
? params.toAmount
: withSlippage(slippage, params.toAmount, false).toFixed(0);
: withSlippage(slippage, d(params.toAmount), false).toFixed(0);

const args = [fromAmount.toString(), toAmount.toString()];

Expand Down

0 comments on commit 1ad989c

Please sign in to comment.