Calling from JS
Place long term order
import { ethers, Signer, providers } from "ethers";
import { defaultAbiCoder } from "ethers/lib/utils";
const PLACE_LONG_TERM_ORDER_JOIN_KIND = 4;
const VAULT_ADDRESS = '0xBA12222222228d8Ba445958a75a0704d566BF2C8';
const VAULT_CONTRACT_ABI = '...';
export async function placeLongTermOrder(
tokenInIndex: number,
tokenOutIndex: number,
amountIn: BigNumber,
numberOfBlockIntervals: number,
signer: Signer | providers.Provider,
walletAddress: string,
poolId: string
) {
const exchangeContract = getExchangeContract(signer);
const placeLongTermOrderRequest = getPlaceLongTermOrderRequest(
tokenInIndex,
tokenOutIndex,
amountIn,
numberOfBlockIntervals,
poolId
);
const swapData = [
poolId,
walletAddress,
walletAddress,
placeLongTermOrderRequest,
];
const placeLongTermOrderTx = await exchangeContract.joinPool(...swapData, {
gasLimit: getGasLimit(exchangeContract, swapData, "joinPool"),
});
return placeLongTermOrderTx;
}
function getPlaceLongTermOrderRequest(
tokenInIndex: number,
tokenOutIndex: number,
amountIn: BigNumber,
numberOfBlockIntervals: number,
poolId: string
) {
const abiCoder = ethers.utils.defaultAbiCoder;
const encodedRequest = abiCoder.encode(
["uint256", "uint256", "uint256", "uint256", "uint256"],
[
PLACE_LONG_TERM_ORDER_JOIN_KIND,
tokenInIndex,
tokenOutIndex,
amountIn,
BigNumber.from(numberOfBlockIntervals),
]
);
return {
assets: getPoolTokenAddresses(poolId),
maxAmountsIn: [MAX_UINT256, MAX_UINT256],
fromInternalBalance: false,
userData: encodedRequest,
};
}
export const getExchangeContract = (
signer: Signer | providers.Provider
): Contract => {
return new Contract(
VAULT_ADDRESS,
VAULT_CONTRACT_ABI,
signer
);
};
Withdraw long term order
import { ethers, Signer, providers } from "ethers";
import { defaultAbiCoder } from "ethers/lib/utils";
const WITHDRAW_LONG_TERM_ORDER_EXIT_KIND = 5;
const VAULT_ADDRESS = '0xBA12222222228d8Ba445958a75a0704d566BF2C8';
const VAULT_CONTRACT_ABI = '...';
export async function withdrawLongTermOrder(
walletAddress: string,
signer: Signer | providers.Provider,
orderId: number,
poolId: string
): Promise<any> {
const withdrawLongTermOrderRequest = getWithdrawLongTermOrderRequest(
orderId,
poolId
);
const data = [
poolId,
walletAddress,
walletAddress,
withdrawLongTermOrderRequest,
];
const vaultContract = getExchangeContract(currentNetwork, signer);
withdrawLongTermOrderTx = await vaultContract.exitPool(...data, {
gasLimit: getGasLimit(vaultContract, data, "exitPool"),
});
return withdrawLongTermOrderTx;
}
function getWithdrawLongTermOrderRequest(
orderId: number,
poolId: string
) {
const encodedRequest: string = defaultAbiCoder.encode(
["uint256", "uint256"],
[WITHDRAW_LONG_TERM_ORDER_EXIT_KIND, orderId]
);
return {
assets: getPoolTokenAddresses(poolId),
minAmountsOut: [0, 0],
userData: encodedRequest,
toInternalBalance: false,
};
}
export const getExchangeContract = (
signer: Signer | providers.Provider
): Contract => {
return new Contract(
VAULT_ADDRESS,
VAULT_CONTRACT_ABI,
signer
);
};
Cancel long term order
import { ethers, Signer, providers } from "ethers";
import { defaultAbiCoder } from "ethers/lib/utils";
const CANCEL_LONG_TERM_ORDER_EXIT_KIND = 4;
const VAULT_ADDRESS = '0xBA12222222228d8Ba445958a75a0704d566BF2C8';
const VAULT_CONTRACT_ABI = '...';
export async function cancelLongTermOrder(
walletAddress: string,
signer: Signer | providers.Provider,
orderId: number,
poolId: string
): Promise<any> {
const vaultContract = getExchangeContract(signer);
const cancelLongTermOrderRequest = getCancelLongTermOrderRequest(
orderId,
poolId
);
const data = [
poolId,
walletAddress,
walletAddress,
cancelLongTermOrderRequest,
];
const exitPoolTx = await vaultContract.exitPool(...data, {
gasLimit: getGasLimit(vaultContract, data, "exitPool"),
});
return exitPoolTx;
}
function getCancelLongTermOrderRequest(
orderId: number,
poolId: string
) {
const encodedRequest: string = defaultAbiCoder.encode(
["uint256", "uint256"],
[4, orderId]
);
return {
assets: getPoolTokenAddresses(poolId),
minAmountsOut: [0, 0],
userData: encodedRequest,
toInternalBalance: false,
};
}
export const getExchangeContract = (
signer: Signer | providers.Provider
): Contract => {
return new Contract(
VAULT_ADDRESS,
VAULT_CONTRACT_ABI,
signer
);
};
Last updated