Development
...
Recipes
Dispatch Transactions
Burn Token
1min
Burning a token or even Signa is very easy. Actually, it is just sending the desired quantity to the burn address 0. But SignumJS also provides a slightly more comfortable method.
JS
const {LedgerClientFactory} = require("@signumjs/core");
const {ChainValue} = require("@signumjs/util");
const {generateMasterKeys} = require("@signumjs/crypto");
const LedgerHostUrls = {
TestNet: 'http://localhost:6876',
MainNet: 'https://europe.signum.network',
}
async function mintToken(args) {
try {
const {ledgerType, tokenId, toBurn, passphrase} = args
// we create our client depending on the parameter
const ledger = LedgerClientFactory.create({
nodeHost: LedgerHostUrls[ledgerType])
}
// before we can send we need to get the private signing key from the user
const {publicKey: senderPublicKey, signPrivateKey: senderPrivateKey} = generateMasterKeys(passphrase)
// Now, we check the token for existance, but also fetching its decimals
// such we can compute the quantity to be minted
console.info("Checking token...")
const {decimals, name} = await ledger.asset.getAsset({assetId: tokenId})
// The token quantity is being determined by the given supply and decimals. It's similar to the concept of Signa vs Planck,
// but with dynamic decimals (0-8). The formula to calculate the quantity is: QNT = amount * (10^decimals)
// To simplify the Devs life we can use the value object `ChainValue` which allows us to convert from and to quantity.
// Instead of using the name Planck/Signa the name Atomic/Compound is used
const quantity = ChainValue.create(decimals).setCompound(toBurn).getAtomic()
// Now, we execute the transaction
// within the method the local signing flow is being executed, i.e.
// the private key is used only locally for signinh, but never sent over the network
const {transaction} = await ledger.asset.burnAsset({
asset: tokenId,
quantity,
feePlanck,
senderPublicKey,
senderPrivateKey
})
// now, some final nice message
console.info(`Burnt ${amount} coins of Token ${name} - Transaction Id: ${transaction}`)
} catch (e) {
// If the API returns an exception,
// the return error object is of type HttpError
handleError(e);
}
}
// // Our entry point has to be async, as our subsequent calls are.
// // This pattern keeps your app running until all async calls are executed
(async () => {
await issueToken({
ledgerType: 'TestNet',
tokenId: '1234',
toBurn: 100,
passphrase: 'YourVerySecretPassphraseWhichCannotBeRevealedHere'
});
})();