Auto Invest

Create Plan

// Initialize a new transaction instance to manage the on-chain operations.
const tx = new Transaction();

// Create an AutoInvest instance for the "mainnet" network to automate the investment process.
const autoInvest = new AutoInvest("mainnet");

// Instantiate a PlanBuilder to configure the details of the investment plan.
const planBuilder = new PlanBuilder();

// Create a Coin instance representing the token to be sold in the investment process.
const sellCoin = new Coin("0TokenSell");

// Associate the transaction instance with the AutoInvest instance to track operations.
autoInvest.tx(tx);

// Perform a deposit of the specified token using the `AutoInvest` instance.
// - `type`: Specifies the token type being sold.
// - `object`: Represents the token amount to be deposited using the `sellCoin.take()` function.
// - `owner`: Your account address ('0xAddress').
// - `amount`: The amount of tokens to deposit, specified as a string (e.g., 1000000000000).
// - `client`: The provider instance responsible for blockchain interaction.
// - `tx`: The initialized transaction object to include the deposit action.
autoInvest.deposit({
  type: tokenSell.type,
  object: await sellCoin.take({
    owner: '0xAddress', // Replace with your actual account address
    amount: '1000000000000', // Token amount in smallest units
    client: provider as any,
    tx,
  }),
});

// Determine the plan's start time based on the `instantStart` flag.
// - If `instantStart` is true, the plan starts immediately (`undefined`).
// - Otherwise, a custom start date (`customDate`) is used. Milisecond, example: new Date().getTime() 
const startTime = instantStart ? undefined : customDate;

// Build the investment plan using the PlanBuilder instance by setting various parameters:
// - `setReceiver`: Specifies the wallet address to receive the invested tokens.
// - `setSubscriptionStartTime`: Defines when the subscription will start.
// - `setOwner`: Sets the account that owns the investment plan.
// - `setSubscriptionAmount`: Calculates the per-cycle investment amount using the `BigNumberInstance`.
// - `setSubscriptionCycle`: Defines the cycle frequency using the mapped time unit ["HOUR, DAY, WEEK"] and frequency value.
// - `setExecutionLimit`: Limits the number of investment cycles to the specified `repeat` value.
// - `setSourceAsset`: Specifies the asset being sold, normalized using `normalizeStructTag`.
// - `setTargetAsset`: Specifies the asset being purchased, normalized using `normalizeStructTag`.
// - `build()`: Finalizes the plan configuration.
const plan = planBuilder
  .setReceiver("0xAddress")
  .setSubscriptionStartTime(startTime)
  .setOwner("0xAddess")
  .setSubscriptionAmount(
    BigNumberInstance('1000000000000').div(repeat).toFixed(0)
  )
  .setSubscriptionCycle("DAY", 10) //for example execute for each 10 days
  .setExecutionLimit(10) //number order you want to buy, for example with this setup, you need 100 days to execute order.
  .setSourceAsset(normalizeStructTag(tokenSell.type))
  .setTargetAsset(normalizeStructTag(tokenBuy.type))
  .build();


autoInvest.createPlan(plan);

Remove or cancle plan

const tx = new Transaction();
autoInvest.tx(tx);
autoInvest.removePlan({ planId });

Last updated

Was this helpful?