Buy
Examples
BuyOptions
interface BuyInitOptions {
coinType: string;
amount: string;
walletAddress: string;
isExact: boolean;
tx?: Transaction;
}
interface BuyOptions extends BuyInitOptions {
isExact: boolean;
threshold: string;
}
Buy
async buy(options: BuyOptions) {
const { tx, coinType, isExact, threshold, amount, walletAddress } = options;
const txb = tx || new Transaction();
const coinAmount = new Decimal(amount)
.mul(10 ** (isExact ? this.quote_decimals : this.token_decimals))
.toFixed(0);
const { tokenAmount, thresholdAmount } = await this.calculateTokenAndThresholdAmount({
coinType,
threshold,
amount: coinAmount,
atob: true,
isExact,
});
const coinBalance = coinWithBalance({
type: this.quote_address,
balance: Number(isExact ? coinAmount : tokenAmount),
});
txb.setSenderIfNotSet(walletAddress);
const [remaining_sui_coin, bought_token_coin] = txb.moveCall({
target: `${pakcageId}::parrot_fun::buy`,
arguments: [
txb.object(globalConfig),
txb.object(funConfig),
coinBalance,
txb.pure.u64(coinAmount),
txb.pure.u64(thresholdAmount),
txb.pure.bool(isExact),
],
typeArguments: [coinType],
});
txb.transferObjects([remaining_sui_coin!, bought_token_coin!], walletAddress);
return txb;
}
Last updated