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
Argument | Type | Description |
---|---|---|
paymentMethod | String | The address of an ERC20 token that can be used to exchange for the presale token. |
amount | String | The amount of payment token spent to buy the presale token. |
signer | Signer | An instance to represent the wallet account. |
Response
Field | Type | Description |
---|---|---|
type | number | The EIP-2718 type of this transaction. |
chainId | number | The id of the network. |
nonce | number | The nonce used as part of the proof-of-work to mine this block. |
maxPriorityFeePerGas | BigNumber | The price (in wei) per unit of gas for this transaction. |
maxFeePerGas | BigNumber | The 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). |
gasPrice | BigNumber | The price (in wei) per unit of gas this transaction will pay. |
gasLimit | BigNumber | The maximum amount of gas that this block was permitted to use. |
to | string | The address of the target. |
value | BigNumber | The amount (in wei) this transaction is sending. |
data | string | Transaction data. |
accessList | AccessList | The AccessList to include; only available for EIP-2930 and EIP-1559 transactions. |
hash | string | Transaction hash. |
v | number | Values for the transaction's signature. |
r | string | Values for the transaction's signature. |
s | string | Values for the transaction's signature. |
from | string | The sender address of the transaction. |
confirmations | number | The 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
Argument | Type | Description |
---|---|---|
paymentMethod | String | The address of an ERC20 token that can be used to exchange for the presale token. |
amount | String | The amount of payment token spent to buy the presale token. |
signer | Signer | An instance to represent the wallet account. |
Response
Field | Type | Description |
---|---|---|
buy | TransactionResponse | the transaction object generated from the buy step of this method |
withdraw | TransactionResponse | the transaction object generated from the withdraw step of this method |
TransactionResponse
Field | Type | Description |
---|---|---|
type | number | The EIP-2718 type of this transaction. |
chainId | number | The id of the network. |
nonce | number | The nonce used as part of the proof-of-work to mine this block. |
maxPriorityFeePerGas | BigNumber | The price (in wei) per unit of gas for this transaction. |
maxFeePerGas | BigNumber | The 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). |
gasPrice | BigNumber | The price (in wei) per unit of gas this transaction will pay. |
gasLimit | BigNumber | The maximum amount of gas that this block was permitted to use. |
to | string | The address of the target. |
value | BigNumber | The amount (in wei) this transaction is sending. |
data | string | Transaction data. |
accessList | AccessList | The AccessList to include; only available for EIP-2930 and EIP-1559 transactions. |
hash | string | Transaction hash. |
v | number | Values for the transaction's signature. |
r | string | Values for the transaction's signature. |
s | string | Values for the transaction's signature. |
from | string | The sender address of the transaction. |
confirmations | number | The 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)