Quickstart

This guide will introduce the flow of using the presale sdk library

Prerequisite: Installing dependencies

Add the sdk to your repository

npm i @uniblock/launcher-presale

or

yarn add @uniblock/launcher-presale

Step 1: Create SDK instance

To initialize the SDK, you should first create your presale contract through the Uniblock dashboard. Then you have to provide Uniblock API key which can be found after creating a project on the site. The API key can be seen inside the project page Then collect the contract address of your deployed contract and chain id and pass them into the initialize function with the Uniblock API key:

  • Nodejs:
import { initializePresale } from "@uniblock/launcher-presale"

const uniblockApiKey = <YOUR_UNIBLOCK_API_KEY>
const contractAddress = <YOUR_CONTRACT_ADDRESS>
const chainId = <YOUR_CHAIN_ID>
const option = {
    rpc: <YOUR_RPC_URL> // You can assign your own rpc url
}

const presaleSdk = await initializePresale(uniblockApiKey, {contractAddress, chainId}, option)
// or if you don't have your own rpc url, you can use uniblock sdk default url
// and use the following code:
// const presaleSdk = await initializePresale(uniblockApiKey, {contractAddress, chainId})
// also chainId is optional and the default chainId of the following code will be 1:
// const presaleSdk = await initializePresale(uniblockApiKey, {contractAddress})
  • Web browser:
<script type="module">
    import { initializePresale } from "https://cdn.jsdelivr.net/npm/@uniblock/launcher-presale/index.esm.min.js"
    const uniblockApiKey = <YOUR_UNIBLOCK_API_KEY>
    const marketplaceContractAddress = <YOUR_CONTRACT_ADDRESS>
    const chainId = <YOUR_CHAIN_ID>
    const option = {
        rpc: <YOUR_RPC_URL> // You can assign your own rpc url
    }
    
    const presaleSdk = await initializePresale(uniblockApiKey, {contractAddress, chainId}, option)
</script>

Step 2: Check token address

To check the token address of the token that is on sale in this presale, we simply call the method of getTokenAddress on the sdk instance:

const presaleToken = await presaleSdk.getTokenAddress()

Step 3: Select a method to purchase presale token

We first list out all supported payment methods:

const paymentTokens = await presaleSdk.getPaymentMethods()

The above method will give a list of erc20 tokens that can be used to exchange for the presale token. We can select any of the tokens and pass them into the getPaymentMethodRate to obtain the exchange rate:

// option 1:
const rate = await presaleSdk.getPaymentMethodRate(paymentTokens[0])
// example: { presaleToken: 1, paymentToken: 1}

Step 4: Buy the presale token from the contract

To purchase the token, we will first need to check if our current time is within the start time and end time of the presale. To obtain the start time and end time of the presale, simply call these two methods on the sdk instance:

const startTime = await presaleSdk.getStartTime()
const endTime = await presaleSdk.getEndTime()

Then in order to call the buy method, we first need to create a signer instance.

Depending on the purposes of the application, we may have two different types of environment for development

  • Nodejs:
const provider = new ethers.providers.JsonRpcProvider("API_URL", 1);
const signer = new ethers.Wallet("WALLET_PRIVATE_KEY", provider);
  • Web browser:
import { initializeWagmi } from "@uniblock/wagmi"
const chainId = <CHAIN_ID>
const wagmi = await initializeWagmi(uniblock_api_key);
const { chains, provider, webSocketProvider } = wagmi.configureChains([chainId]);
const client = wagmi.createClient(
  provider,
  webSocketProvider,
  chains,
  [
    connector.METAMASK_CONNECTOR,
  ],
);
await wagmi.connectWallet(client, connector.METAMASK_CONNECTOR);
const signer = await wagmi.getSigner();

Note: To use the wagmi, install it through yarn add @uniblock/wagmi or npm install @uniblock/wagmi

If the payment token is an erc20 token, we need to ensure the contract has enough allowance to spend the user's token.. This can be done by running the following code:

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:

We only need to increase allowance through the above code snippet if the payment token we chose is an ERC20 token. We don't need to increase allowance if the payment token is a native token of the specified chain

Finally, we can then provide our selected payment method, the desired amount and signer to the buy method:

await presaleSdk.buy(paymentTokens[0], '20', signer)

Step 5: Withdraw the presale token after the presale

After the presale is over, the user can withdraw the presale token by calling the withdraw/withdrawAll method. Before they withdraw, they need to check if the current time is after the withdraw time of the contract. To check withdraw time, simply call the getWithdrawTime method:

const withdrawTime = await presaleSdk.getWithdrawTime()

Create the signer instance as the way in step 4 and call the withdraw method:

// developers can specify the amount to withraw
await presaleSdk.withdraw('20', signer)

// developers can also withdraw all of the purchased tokens
await presaleSdk.withdrawAll(signer)

Note:

In case if the claim time is passed but the presale endtime is not reached, users could choose to call buyAndWithdraw method as the following:

await presaleSdk.buyAndWithdraw(paymentTokens[0], '20', signer)