Skip to content

Commit

Permalink
Merge pull request #49 from pontem-network/feat-liquidity-add-vesrion…
Browse files Browse the repository at this point in the history
…-support

Feat liquidity add vesrion support
  • Loading branch information
mrz1703 committed Nov 13, 2023
2 parents 914642e + 0920796 commit ac8936c
Show file tree
Hide file tree
Showing 6 changed files with 656 additions and 145 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ For Yarn:
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.
version: 0
})
console.log(output) // '4304638' (4.304638 USDT)

Expand All @@ -105,6 +106,7 @@ For Yarn:
slippage: 0.005, // 0.5% (1 - 100%, 0 - 0%)
stableSwapType: 'high',
curveType: 'uncorrelated',
version: 0
})
console.log(txPayload);
} catch(e) {
Expand Down Expand Up @@ -142,6 +144,7 @@ For Yarn:
amount: 1000000, // 1 layerzero USDT
interactiveToken: 'to',
curveType: 'uncorrelated',
version: 0
})
console.log(amount) // '23211815' ('0.23211815' APTOS)

Expand All @@ -156,6 +159,7 @@ For Yarn:
slippage: 0.005, // 0.5% (1 - 100%, 0 - 0%)
stableSwapType: 'hign',
curveType: 'uncorrelated',
version: 0
})
console.log(txPayload);
} catch (e) {
Expand Down Expand Up @@ -192,6 +196,7 @@ For Yarn:
amount: 100000000, // 1 APTOS
interactiveToken: 'from',
curveType: 'stable',
version: 0
})
console.log(amount) // '175257' ('0.00175257' whWETH)

Expand All @@ -206,6 +211,7 @@ For Yarn:
slippage: 0.005, // 0.5% (1 - 100%, 0 - 0%)
stableSwapType: 'normal',
curveType: 'stable',
version: 0
})
console.log(txPayload);
} catch (e) {
Expand Down Expand Up @@ -242,6 +248,7 @@ For Yarn:
amount: 1000000, // 1 USDA
interactiveToken: 'to',
curveType: 'stable',
version: 0
})
console.log(amount) // '12356861' ('0.12356861' APTOS)

Expand All @@ -256,6 +263,7 @@ For Yarn:
slippage: 0.005, // 0.5% (1 - 100%, 0 - 0%)
stableSwapType: 'high',
curveType: 'stable',
version: 0
})
console.log(txPayload);
} catch (e) {
Expand Down Expand Up @@ -288,6 +296,7 @@ For Yarn:
fromToken: "0x1::aptos_coin::AptosCoin",
toToken: "0xf22bede237a07e121b56d91a491eb7bcdfd1f5907926a9e58338f964a01b17fa::asset::USDC",
curveType: 'uncorrelated',
version: 0
});

console.log(output); // true
Expand Down Expand Up @@ -363,6 +372,7 @@ For Yarn:
curveType: 'uncorrelated',
interactiveToken: 'from',
slippage: 0.005,
version: 0
});
console.log(rate) // '4472498' ('4.472498' USDC)
console.log(receiveLp) // '19703137' ('19.703137' Minimum Received LP)
Expand All @@ -376,6 +386,7 @@ For Yarn:
slippage: 0.005,
stableSwapType: 'normal',
curveType: 'uncorrelated',
version: 0
})

console.log(payload);
Expand Down Expand Up @@ -406,6 +417,7 @@ For Yarn:
slippage: 0.005,
curveType: 'uncorrelated',
burnAmount: 100000,
version: 0
});

console.log(output);
Expand Down Expand Up @@ -434,6 +446,7 @@ For Yarn:
slippage: 0.005,
curveType: 'uncorrelated',
burnAmount: 100000,
version: 0
});

console.log(output);
Expand Down Expand Up @@ -473,6 +486,7 @@ Code examples to work with Aptos SDK can be found in the [examples](examples) di
- [x] Pools: check pool / create pool
- [x] Examples with Aptos SDK
- [ ] Staking: stake / harvest / unstake
- [ ] Concentrated Liquidity: swap, add, burn

See the [open issues](https://github.com/pontem-network/liquidswap-sdk/issues) for a full list of proposed features (and known issues).

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.6.1",
"version": "0.7.0",
"description": "SDK to use LiquidSwap functions",
"author": "Igor Demko <igor@pontem.network>",
"repository": "https://github.com/pontem-network/liquidswap-sdk",
Expand Down
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export const TOKENS_MAPPING: Record<string, string> = {
BTC: '0xae478ff7d83ed072dbc5e264250e67ef58f57c99d89b447efd8a0a2e8b2be76e::coin::T', // wormhole wrapped BTC
WETH: '0xf22bede237a07e121b56d91a491eb7bcdfd1f5907926a9e58338f964a01b17fa::asset::WETH', // LayerZero WETH
USDC: '0xf22bede237a07e121b56d91a491eb7bcdfd1f5907926a9e58338f964a01b17fa::asset::USDC', // layerzero USDC
amAPT: '0x111ae3e5bc816a5e63c2da97d0aa3886519e0cd5e4b046659fa35796bd11542a::amapt_token::AmnisApt' // Amnis APT
};
68 changes: 47 additions & 21 deletions src/modules/LiquidityModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ import {
} from '../utils';

import { CreateTXPayloadParams } from './SwapModule';
import {VERSION_0, VERSION_0_5} from "../constants";
import {getCurve, getScriptsFor} from "../utils/contracts";

interface ICreateBurnLiquidityPayload {
fromToken: AptosResourceType;
toToken: AptosResourceType;
burnAmount: Decimal | number;
slippage: number;
curveType: CurveType;
version?: typeof VERSION_0 | typeof VERSION_0_5;
}

interface ICalculateRatesParams {
Expand All @@ -40,10 +43,11 @@ interface ICalculateRatesParams {
interactiveToken: 'from' | 'to';
curveType: CurveType;
slippage: number;
version?: typeof VERSION_0 | typeof VERSION_0_5;
}

interface ICalculateSupplyParams
extends Pick<ICalculateRatesParams, 'slippage'> {
extends Pick<ICalculateRatesParams, 'slippage' | 'version'> {
toAmount: Decimal | number;
fromAmount: Decimal | number;
toReserve: Decimal | number;
Expand All @@ -58,6 +62,7 @@ interface ICalculateBurnLiquidityParams {
slippage: number;
burnAmount: Decimal | number;
curveType: CurveType;
version?: typeof VERSION_0 | typeof VERSION_0_5;
}

type TGetResourcesPayload = Omit<
Expand All @@ -79,15 +84,21 @@ export class LiquidityModule implements IModule {
}

async checkPoolExistence(params: TGetResourcesPayload): Promise<boolean> {
const { moduleAccount, resourceAccount } = this.sdk.networkOptions;
const { moduleAccount, resourceAccount, moduleAccountV05, resourceAccountV05 } = this.sdk.networkOptions;
const curves = this.sdk.curves;
const { version = VERSION_0 } = params;

const moduleAcc = version === VERSION_0_5 ? moduleAccountV05 : moduleAccount;
const resourceAcc = version === VERSION_0_5 ? resourceAccountV05 : resourceAccount;

const modulesLiquidityPool = composeType(
moduleAccount,
moduleAcc,
'liquidity_pool',
'LiquidityPool',
);

const curve = curves[params.curveType];
const curve = getCurve(params.curveType, curves, version);

const liquidityPoolType = getPoolStr(
params.fromToken,
params.toToken,
Expand All @@ -98,7 +109,7 @@ export class LiquidityModule implements IModule {
try {
const liquidityPoolResource =
await this.sdk.Resources.fetchAccountResource<AptosResource>(
resourceAccount,
resourceAcc,
liquidityPoolType,
);
return Boolean(liquidityPoolResource?.type);
Expand All @@ -108,14 +119,19 @@ export class LiquidityModule implements IModule {
}

async getLiquidityPoolResource(params: TGetResourcesPayload) {
const { moduleAccount, resourceAccount } = this.sdk.networkOptions;
const { moduleAccount, resourceAccount, moduleAccountV05, resourceAccountV05 } = this.sdk.networkOptions;
const curves = this.sdk.curves;
const { version = VERSION_0 } = params;

const moduleAcc = version === VERSION_0_5 ? moduleAccountV05 : moduleAccount;
const resourceAcc = version === VERSION_0_5 ? resourceAccountV05 : resourceAccount;
const curve = getCurve(params.curveType, curves, version);

const modulesLiquidityPool = composeType(
moduleAccount,
moduleAcc,
'liquidity_pool',
'LiquidityPool',
);
const curve = curves[params.curveType];

const liquidityPoolType = getPoolStr(
params.fromToken,
Expand All @@ -129,7 +145,7 @@ export class LiquidityModule implements IModule {
try {
liquidityPoolResource =
await this.sdk.Resources.fetchAccountResource<AptosPoolResource>(
resourceAccount,
resourceAcc,
liquidityPoolType,
);
} catch (e) {
Expand All @@ -140,9 +156,11 @@ export class LiquidityModule implements IModule {

async getLiquiditySupplyResource(params: TGetResourcesPayload) {
const curves = this.sdk.curves;
const { modules, resourceAccount } = this.sdk.networkOptions;
const { modules, resourceAccount, resourceAccountV05 } = this.sdk.networkOptions;
const { version = VERSION_0 } = params;

const curve = curves[params.curveType];
const curve = getCurve(params.curveType, curves, version);
const resourceAcc = version === VERSION_0_5 ? resourceAccountV05 : resourceAccount;

function getPoolLpStr(
coinX: string,
Expand All @@ -153,8 +171,7 @@ export class LiquidityModule implements IModule {
? [coinX, coinY]
: [coinY, coinX];
return composeType(
//
resourceAccount,
resourceAcc,
'lp_coin',
'LP',
[sortedX, sortedY, curve],
Expand All @@ -168,7 +185,7 @@ export class LiquidityModule implements IModule {
try {
liquidityPoolResource =
await this.sdk.Resources.fetchAccountResource<AptosCoinInfoResource>(
resourceAccount,
resourceAcc,
composeType(modules.CoinInfo, [lpString]),
);
} catch (e) {
Expand Down Expand Up @@ -307,17 +324,21 @@ export class LiquidityModule implements IModule {
}

const isPoolExisted = await this.checkPoolExistence(params);
const { version = VERSION_0 } = params;

const { moduleAccountV05, moduleAccount } = this.sdk.networkOptions;
const moduleAcc = version === VERSION_0_5 ? moduleAccountV05 : moduleAccount;

const { modules } = this.sdk.networkOptions;
const curves = this.sdk.curves;
const scriptsVersion = getScriptsFor(version);

const functionName = composeType(
modules.Scripts,
moduleAcc,
scriptsVersion,
isPoolExisted ? 'add_liquidity' : 'register_pool_and_add_liquidity',
);


const curve = curves[params.curveType];
const curve = getCurve(params.curveType, curves, version);
const isSorted = is_sorted(params.fromToken, params.toToken);

const typeArguments = isSorted
Expand Down Expand Up @@ -366,10 +387,13 @@ export class LiquidityModule implements IModule {
);
}

const { version = VERSION_0 } = params;

const curves = this.sdk.curves;
const curve = curves[params.curveType];
const curve = getCurve(params.curveType, curves, version);

const { modules } = this.sdk.networkOptions;
const { moduleAccountV05, moduleAccount } = this.sdk.networkOptions;
const moduleAcc = version === VERSION_0_5 ? moduleAccountV05 : moduleAccount;

const output = await this.calculateOutputBurn(params);

Expand All @@ -382,7 +406,9 @@ export class LiquidityModule implements IModule {
? [params.burnAmount.toString(), xOutput, yOutput]
: [params.burnAmount.toString(), yOutput, xOutput];

const functionName = composeType(modules.Scripts, 'remove_liquidity');
const scriptsVersion = getScriptsFor(version);

const functionName = composeType(moduleAcc, scriptsVersion, 'remove_liquidity');

const typeArguments = isSorted
? [params.fromToken, params.toToken, curve]
Expand Down

0 comments on commit ac8936c

Please sign in to comment.