Skip to main content

Burn ERC20 (Myria) Tokens

  • Learn how to burn the ERC20 asset tokens from the Myria L2.
  • To inspect the function in the SDK with details payload and response format you can reference at SDK Docs.

Prerequisites

  • ERC20 Token must be registered to Myria system
  • Make sure that you have ERC-20 tokens (such as Myria tokens) in the L2 wallet
  • You can deposit ERC20 (ex: MYR tokens) from L1 to L2 wallet, can reference at depositERC20 step.
  • Or if your L2 wallet received the ERC-20 tokens from Airdrop / Campaign events.

Burn process

  • Defined the amount of tokens you want to burn.
  • Make sure the current balance of ERC20 tokens you have in L2 wallet is greater >= with the number you want to burn.
  • Trigger the burn process.

Implementation

info

You can find the full React implementation for burn ERC20 tokens in the Myria React Samples repository.

1. Set up a new React project

Burn ERC-20 tokens requires client-side interaction with the MetaMask wallet. It's recommended to use React to implement such behavior.

You can create a React app via Create React App. Note, the below project relies on Web3.js library and needs custom configuration.

2. Create a myria-client.ts helper

It's recommended to have a separate .ts file for quick access to the MyriaClient.

For more details, use the Myria Core SDK overview.

3. Trigger the burn tokens action

You can create the simple React component file with below implementation such as BurnTokensComponent.tsx

Sample Code with Details Implementation

  const mClient: IMyriaClient = {
networkId: Network.SEPOLIA, // This network is couple along with env - for Mainnet it is 1
provider: web3Instance.currentProvider,
web3: web3Instance,
env: EnvTypes.STAGING, // Env that you're interacting on STAGING/PROD
};

const YOUR_TOKEN_CONTRACT_ADDRESS = "0xA06116D9...."; // Your ERC-20 smart contract
const SENDER_WALLET_ADDRESS = '0x724f337bF0F....'; // Must be the owner of tokens, sender wallet address

const moduleFactory = ModuleFactory.getInstance(mClient);
const transactionManager = moduleFactory.getTransactionManager();

const QUANTUM = 10000000000; // 10^10 as pre-defined rules in Myria

// BN lib reference: https://github.com/indutny/bn.js/

// Sample method to convert the pure amount to Quantized amount
function convertAmountToQuantizedAmount(amount: number | string): number {
const wei = convertEthToWei(String(amount));
const quantizedAmount = Number(new BN(wei, 10).div(new BN(QUANTUM, 10)).toString());
return quantizedAmount;
}

function convertEthToWei(amount: string): string {
return ethers.utils.parseEther(String(amount)).toString();
}

const burnTransferredItems: ItemSignableBurnParams[] = [
{
quantizedAmount: convertAmountToQuantizedAmount(1), // Amount must be quantized amount
tokenType: TokenType.ERC20,
tokenData: {
tokenAddress: YOUR_TOKEN_CONTRACT_ADDRESS,
quantum: QUANTUM
},
},
{
quantizedAmount: convertAmountToQuantizedAmount(1),
tokenType: TokenType.ERC20,
tokenData: {
tokenAddress: YOUR_TOKEN_CONTRACT_ADDRESS,
quantum: QUANTUM
},
},
{
quantizedAmount: convertAmountToQuantizedAmount(1),
tokenType: TokenType.ERC20,
tokenData: {
tokenAddress: YOUR_TOKEN_CONTRACT_ADDRESS,
quantum: QUANTUM
},
},
];

const burnTokensParams: BurnTokenParams = {
senderWalletAddress: SENDER_WALLET_ADDRESS,
groupRequestId: '7257d29c-c96a-4302-8eaf-368a0d62b977', // Can use random UUID to generate groupRequestID
requestId: '7257d29c-c96a-4302-8eaf-368a0d62b977', // Can use random UUID to generate requestID
partnerRefId: 'Project-ID', // Project-ID on Myria System
description: 'Test-Test Burn Transfer',
items: burnTransferredItems,
};

const burnTransferResult = await transactionManager.burnTokens(
burnTransferTokenParams,
);