Minting a token off-chain
Although a token can remain offchain as long as your user only wants to execute offchain operations, such as minting and trading, you must enable your users to withdraw the new token onchain.
To do so, you pair it with the onchain contract in which the asset is minted, and register it with the StarkEx smart contract.
When your user makes a withdrawal request, StarkEx burns the offchain token and sends a call to the mintFor function in the paired onchain contract, which mints a corresponding onchain token.
|
The operator needs to inform their users that the operator controls the offchain minting policy. |
-
You must have minting privileges for the
mintForfunction. Only minters should have these privileges.
-
Define a new token asset type by deploying an onchain contract, for ERC-20, ERC-721, or ERC-1155, that includes a
mintForfunction. This function enables the contract to mint the token onchain after the user withdraws the token that was minted offchain.The
mintForfunction is defined in the ERC-20, ERC-1155, and ERC-721 contracts, with the following signature:MINTABLE_TOKEN(address).mintFor(withdrawer=msg.sender, asset_blob, amount) -
After you deploy the new token contract onchain, register it to the StarkEx smart contract with the
registerTokenfunction in theTokenRegister.solsmart contract.Validium vaults and ZK-Rollup vaults each have a range of 31 bits for the ID of each vault. When you allocate new vaults, consider that the simplest way to allocate new vaults is sequentially. You can also reuse vaults with a zero balance.
-
Mint new offchain tokens using the
MintRequesttransaction type in the StarkEx REST API. Thetoken_idfield is computed as explained in Identifiers for supported assets. This field sets the mapping to the token contract’s address.The StarkEx API does not include the keyword
asset_id. Use the keywordtoken_idin API calls to refer to a unique offchain asset.The
MintRequestmethod mints this token to an unused vault that you specify in the function call, such as your own vault or a user’s vault. -
If you mint the new token to your own vault, you can either transfer it to a user, using the
TransferRequesttransaction type, or trade it using theSettlementRequesttransation type. Both of these transactions have a fee mechanism, so you can collect fees.A settlement transaction contains 2 signed limit orders.
-
Your user can hold or trade the token offchain. To execute any onchain transactions, they must first withdraw the token onchain, as described in the withdrawal flow. The withdrawal is included in a batch, and finalized offchain, and then it is available for your user to withdraw onchain. The
withdrawAndMintfunction in your onchain contract actually mints the token onchain and transfers it to your user’s Ethereum account.
-
registerTokenfunction in theTokenRegister.solcontract -
withdrawAndMintfunction in theWithdrawals.solcontract -
The
mintForfunction appears in the ERC-20, ERC-721, and ERC-1155 contracts.