Buy Methods

launcher presale buy methods.

buy

Users can purchase a presale token using the buy method so that they can withdraw after the withdraw time. It is recommended to use the buyAndWithdraw method if withdraw time has passed. Otherwise, users could use the buy method.

Parameter

ArgumentTypeDescription
paymentMethodStringThe address of an ERC20 token that can be used to exchange for the presale token.
amountStringThe amount of payment token spent to buy the presale token.
signerSignerAn instance to represent the wallet account.

Response

FieldTypeDescription
typenumberThe EIP-2718 type of this transaction.
chainIdnumberThe id of the network.
noncenumberThe nonce used as part of the proof-of-work to mine this block.
maxPriorityFeePerGasBigNumberThe price (in wei) per unit of gas for this transaction.
maxFeePerGasBigNumberThe maximum price (in wei) per unit of gas this transaction will pay for (the combined EIP-1559 block's base fee and this transaction's priority fee).
gasPriceBigNumberThe price (in wei) per unit of gas this transaction will pay.
gasLimitBigNumberThe maximum amount of gas that this block was permitted to use.
tostringThe address of the target.
valueBigNumberThe amount (in wei) this transaction is sending.
datastringTransaction data.
accessListAccessListThe AccessList to include; only available for EIP-2930 and EIP-1559 transactions.
hashstringTransaction hash.
vnumberValues for the transaction's signature.
rstringValues for the transaction's signature.
sstringValues for the transaction's signature.
fromstringThe sender address of the transaction.
confirmationsnumberThe number of blocks that have been mined (including the initial block) since this transaction.

Example

If the payment token is an erc20 token, we need to ensure the presale contract has enough allowance to spend the user's token. If not, developers can always use the following lines to ensure sufficient allowance:

const paymentToken = PAYMENT_METHOD
// assume PAYMENT_METHOD stores a payment method from the list 
// of payment methods returned by getPaymentMethods
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
if(paymentToken !== ZERO_ADDRESS){
    const allowance = await presaleSdk.getAllowance(paymentToken, await signer.getAddress());
    const amount = BigNumber.from(20)
    if(allowance.lt(amount)){
        await presaleSdk.increaseAllowance(paymentToken, amount.toString(), signer)
    }    
}

 

Then call the buy method:

await presaleSdk.buy(paymentToken, '20', signer)

Note:

There is no need to increase allowance through the above code snippet if the payment token is a native token of the specified chain

buyAndWithdraw

Users can purchase a presale token and automatically withdraw the purchased presale token. This method works only if the withdrawal time of the presale contract is passed.

Parameter

ArgumentTypeDescription
paymentMethodStringThe address of an ERC20 token that can be used to exchange for the presale token.
amountStringThe amount of payment token spent to buy the presale token.
signerSignerAn instance to represent the wallet account.

Response

FieldTypeDescription
buyTransactionResponsethe transaction object generated from the buy step of this method
withdrawTransactionResponsethe transaction object generated from the withdraw step of this method

TransactionResponse

FieldTypeDescription
typenumberThe EIP-2718 type of this transaction.
chainIdnumberThe id of the network.
noncenumberThe nonce used as part of the proof-of-work to mine this block.
maxPriorityFeePerGasBigNumberThe price (in wei) per unit of gas for this transaction.
maxFeePerGasBigNumberThe maximum price (in wei) per unit of gas this transaction will pay for (the combined EIP-1559 block's base fee and this transaction's priority fee).
gasPriceBigNumberThe price (in wei) per unit of gas this transaction will pay.
gasLimitBigNumberThe maximum amount of gas that this block was permitted to use.
tostringThe address of the target.
valueBigNumberThe amount (in wei) this transaction is sending.
datastringTransaction data.
accessListAccessListThe AccessList to include; only available for EIP-2930 and EIP-1559 transactions.
hashstringTransaction hash.
vnumberValues for the transaction's signature.
rstringValues for the transaction's signature.
sstringValues for the transaction's signature.
fromstringThe sender address of the transaction.
confirmationsnumberThe number of blocks that have been mined (including the initial block) since this transaction.

Example

If the payment token is an erc20 token, we need to ensure the presale contract has enough allowance to spend the user's token. If not, developers can always use the following lines to ensure sufficient allowance:

const paymentToken = PAYMENT_METHOD
// assume PAYMENT_METHOD stores a payment method from the list 
// of payment methods returned by getPaymentMethods
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
if(paymentToken !== ZERO_ADDRESS){
    const allowance = await presaleSdk.getAllowance(paymentToken, await signer.getAddress());
    const amount = BigNumber.from(20)
    if(allowance.lt(amount)){
        const incrementAllowance = amount.sub(allowance).toString()
        await presaleSdk.increaseAllowance(paymentToken, incrementAllowance, signer)
    }    
}

Note:

There is no need to increase allowance through the above code snippet if the payment token is a native token of the specified chain.

Then call the buyAndWithdraw method:

const paymentToken = PAYMENT_METHOD
// assume PAYMENT_METHOD stores a payment method from the list 
// of payment methods returned by getPaymentMethods
await presaleSdk.buyAndWithdraw(paymentToken, '20', signer)