Swap
Swaps typically occur in two steps:
1. calc swap amount
Use functions provided by @magmaprotocol/calc_almm
import { get_swap_out, get_swap_in } from "@magmaprotocol/calc_almm";
Example
import { get_swap_out, get_swap_in } from "@magmaprotocol/calc_almm";
const pools = await magmaClmmSDK.Almm.getPoolInfo([
"0x540e56df0e57ac0d779a64d1f9f01fe9ac03d4758623105e05b4f9facc5d0f61",
]);
const bins = await magmaClmmSDK.Almm.fetchBins({
pair: "0x540e56df0e57ac0d779a64d1f9f01fe9ac03d4758623105e05b4f9facc5d0f61",
coinTypeA:
"0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC",
coinTypeB:
"0x06864a6f921804860930db6ddbe2e16acdf8504495ea7481637a1c8b9a8fe54b::cetus::CETUS",
offset: 0,
limit: 500,
});
const params_str = JSON.stringify({
params: pools[0].params,
bins,
bin_step: pools[0].bin_step,
});
const now_timestamp = Date.now();
const swap_out = get_swap_out(
params_str,
BigInt(10000),
true,
BigInt(now_timestamp),
);
console.log("swap_out: ", swap_out);
// { amount_in_left: 10, amount_out: 110849443, fee: 10 }
const swap_in = get_swap_in(
params_str,
BigInt(110849443),
true,
BigInt(now_timestamp),
);
console.log("swap_in: ", swap_in);
// { amount_in: 9999, amount_out_left: 0, fee: 10 }
Swap
Method
Method: sdk.Almm.swap({ pair: string coinTypeA: string coinTypeB: string amountIn: number minAmountOut: number swapForY: boolean to: string }: ALMMSwapParams )
Params
pool_id
: The pool object ID.coinTypeA
: the coin type address about coinA.coinTypeB
: the coin type address about coinB.amountIn
: the amount of input coinminAmountOut
: the amount limit of coin what you get.swapForY
: if true, swap X to Y; if false, swap Y to Xto
: swap result will transfer to whom
Example:
const res = await magmaClmmSDK.Almm.swap({
pair: "0x540e56df0e57ac0d779a64d1f9f01fe9ac03d4758623105e05b4f9facc5d0f61",
coinTypeA:
"0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC",
coinTypeB:
"0x06864a6f921804860930db6ddbe2e16acdf8504495ea7481637a1c8b9a8fe54b::cetus::CETUS",
amountIn: 9999,
minAmountOut: 0,
swapForY: true,
to: "YourAddress",
});
Last updated