Sell

Examples

SellOptions

interface sellOptions extends BuyOptions {}

Sell

async sell(options: sellOptions) {
    const { coinType, tx, walletAddress, isExact, threshold, amount } = 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: false,
      isExact,
    });
    const coinBalance = coinWithBalance({
      type: coinType,
      balance: Number(isExact ? coinAmount : tokenAmount),
    });
    console.log(coinAmount, tokenAmount, thresholdAmount);
    txb.setSenderIfNotSet(walletAddress);
    const [sui_coin_returned, remaining_token_coin] = txb.moveCall({
      target: `${pakcageId}::parrot_fun::sell`,
      arguments: [
        txb.object(globalConfig),
        txb.object(funConfig),
        coinBalance,
        txb.pure.u64(coinAmount),
        txb.pure.u64(thresholdAmount),
        txb.pure.bool(isExact),
      ],
      typeArguments: [coinType],
    });
    txb.transferObjects([sui_coin_returned!, remaining_token_coin!], walletAddress);
    return txb;
  }

calculateTokenAndThresholdAmount

Last updated