Skip to content

krc20

Understanding KRC20 Minting with TypeScript Real Examples: A Step-by-Step Guide

Minting a KRC20 token can seem daunting, especially for newcomers in the crypto world. In this post, we'll walk you through the minting process using TypeScript code pieces as an example. Those code pieces are using rusty-kaspa WASM recommended by aspect. We'll also dive into the composition of transactions and discuss some updates on KIP-9, addressing current challenges and solutions.

Special thanks to KaffinPX for sharing this code in the dev channel inKaspa Discord server.

What is KRC20?

KRC20 is a standard for tokens on the Kaspa network, similar to the BRC20 standard for Bitcoin. It defines a set of rules for tokens, enabling seamless interaction within the Kaspa ecosystem.

Minting KRC20 Tokens: The TypeScript Example

Our TypeScript app leverages the Kaspa WASM SDK to mint KRC20 tokens. Here's a simplified explanation of the process:

Step 1: Setup and Create the Script

We create a script that includes the minting data for the KRC20 token:

import { ScriptBuilder, Opcodes, PrivateKey, addressFromScriptPublicKey } from "./wasm/kaspa"
import config from "./config.json"

const privateKey = new PrivateKey(config.treasury.privateKey)
const publicKey = privateKey.toPublicKey()
const address = publicKey.toAddress('testnet-11')

const data = { "p": "krc-20", "op": "mint", "tick": "TNACHO" }

const script = new ScriptBuilder()
  .addData(publicKey.toXOnlyPublicKey().toString())
  .addOp(Opcodes.OpCheckSig)
  .addOp(Opcodes.OpFalse)
  .addOp(Opcodes.OpIf)
  .addData(Buffer.from("kasplex"))
  .addI64(0n)
  .addData(Buffer.from(JSON.stringify(data, null, 0)))
  .addOp(Opcodes.OpEndIf)

const P2SHAddress = addressFromScriptPublicKey(script.createPayToScriptHashScript(), 'testnet-11')!

Step 2: Commit Transaction

The commit transaction is the first step in the minting process. This transaction locks funds into a Pay-to-Script-Hash (P2SH) address. The P2SH address is used to ensure that the script must be satisfied before the funds can be spent. This provides an additional layer of security and ensures that the minting operation can only be completed if certain conditions are met. Specifically, it means the funds are locked under a script that dictates how and when they can be spent, protecting against unauthorized or premature usage.

const { entries } = await RPC.getUtxosByAddresses({ addresses: [address.toString()] });
const { transactions } = await createTransactions({
  priorityEntries: [],
  entries,
  outputs: [{
    address: P2SHAddress.toString(),
    amount: kaspaToSompi("1")!
  }],
  changeAddress: address.toString(),
  priorityFee: kaspaToSompi("0.1")!,
  networkId: 'testnet-11'
});

for (const transaction of transactions) {
  transaction.sign([privateKey]);
  const hash = await transaction.submit(RPC);
}

In this step, we create and sign a transaction that sends 1 Kaspa to the P2SH address. This transaction is submitted to the network, effectively committing to the minting operation.

Step 3: Reveal Transaction

After the commit transaction is confirmed, we proceed with the reveal transaction. This transaction reveals the minting operation by spending the UTXO created in the commit transaction. The script is included in the reveal process to prove that the conditions for minting the KRC20 token are met.

The priorityEntries parameter includes the UTXO from the commit transaction as the highest priority for spending. This ensures that this specific UTXO is used first in the reveal transaction, which is essential because it contains the locked funds that are governed by the script we created.

setTimeout(async () => {
  try {
    const { entries } = await RPC.getUtxosByAddresses({ addresses: [address.toString()] });
    const revealUTXOs = await RPC.getUtxosByAddresses({ addresses: [P2SHAddress.toString()] });

    const { transactions } = await createTransactions({
      priorityEntries: [revealUTXOs.entries[0]],
      entries,
      outputs: [],
      changeAddress: address.toString(),
      priorityFee: kaspaToSompi("0.1")!,
      networkId: 'testnet-11'
    });

    for (const transaction of transactions) {
      transaction.sign([privateKey], false);

      const ourOutput = transaction.transaction.inputs.findIndex((input) => input.signatureScript === '');

      if (ourOutput !== -1) {
        const signature = transaction.signInput(ourOutput, privateKey);
        transaction.fillInput(ourOutput, script.encodePayToScriptHashSignatureScript(signature));
      }

      const revealHash = await transaction.submit(RPC);
    }
  } catch (revealError) {
    console.error('Reveal transaction error:', revealError);
  }
}, 20000); // Wait for 20 seconds before attempting to reveal

In the reveal transaction, we: 1. Identify the UTXO from the commit transaction. 2. Create a new transaction that spends this UTXO. 3. Include the script to verify that the conditions are met. 4. Sign and submit the transaction.

Why Use P2SH and Scripts?

Using a P2SH address allows us to lock the funds with a script that defines specific conditions for spending. This provides flexibility and security, ensuring that the minting process can only be completed if the defined conditions are met. The script execution happens when the reveal transaction is processed, verifying the minting conditions before allowing the UTXO to be spent.

Update on KIP-9 and Ongoing Challenges

KIP-9 introduces a mechanism to regulate the growth of the UTXO set, addressing state bloat in the chain. Our TypeScript app utilizes the createTransactions() function from Kaspa WASM SDK, which incorporates KIP-9 fee calculations and ensure your app is compatible with the extended mass formula proposed in KIP-9 to regulate the creation and lifecycle of UTXOs. However, two critical features are still in progress:

  1. Dynamic Network Fee Estimation: This will allow for more accurate priority fee calculations based on current network conditions.
  2. Replace-By-Fee (RBF): This feature will enable transaction replacement in the mempool with higher fees, ensuring timely confirmation.

These updates are expected to be available in the coming weeks, enhancing the efficiency and scalability of KRC20 token minting. Additionally, KIP-9 helps to make better block utilization by regulating the growth rate of the UTXO set and optimizing transaction fees, thus reducing state bloat and enhancing overall network performance.

KRC20 Tokenomics Explained: The Future of Fair Launches**

The rise of KRC20 tokens, introduced by the Kasplex Foundation, is revolutionizing the crypto landscape. These tokens bring flexibility to Kaspa via smart contracts and offer a transparent and fair approach to token launches, making them a game-changer compared to Solana and Ethereum. In this blog post, we'll explore how KRC20 tokenomics work and why they are becoming the preferred choice for new projects.

The Growth of KRC20 Tokens

KRC20 tokens are rapidly gaining popularity due to their unique approach to token distribution. Unlike traditional tokens on Solana or Ethereum, KRC20 tokens allow anyone to mint them. This open-access minting ensures a more equitable distribution, fostering a fair launch environment. In this model, 100% of the tokens can be mintable from the start, enabling a truly decentralized and fair distribution.

Fair Launch Options

One of the standout features of KRC20 tokens is the flexibility they offer in token distribution. While projects can opt for a fully mintable launch, there is also the option to pre-mint a portion of the tokens. This pre-minting allows project creators to reserve tokens for the team, marketing, or future exchange listings. Additionally, projects can decide the mint batches, even attempting to mint an amount equal to the entire supply. This approach, however, could lead to centralization if not managed properly, as creators could potentially control all the tokens.

Transparency and Public Information

All information about KRC20 token deployments is public. Tools like Kasplex and Kastools.com provide detailed insights into how tokens were distributed, who benefited, and how the community and project decisions have been managed. It's crucial to analyze this information to understand the project's dynamics and future potential.

Risks and Considerations

While KRC20 tokens offer a fair launch mechanism, there are risks to consider. For example, tokens quietly pre-minted by creators may lead to projects stalling if the team does not continue to push for exchange listings or further development. This could render the tokens useless in the future. Therefore, understanding the tokenomics and supply limits is vital.

Projects like Nacho, Kasper, and Kaspaw have implemented fair launch principles, but with different approaches to supply and limit ratios. Nacho's higher supply/limit ratio ensures a more stable and bot-resistant distribution but may reduce initial hype. In contrast, Kasper's lower ratio creates a more exciting minting process but is more susceptible to bot and whale manipulation. Meanwhile, projects like GhoadCoin pre-allocate a significant portion of tokens, leaving only 30% for community minting, which could impact decentralization.

The Power of Kaspa and KRC20

KRC20’s flexibility and fairness, combined with the speed and scalability of Kaspa, offer unprecedented benefits to the crypto community. Kaspa’s unique BlockDAG technology provides significant advantages over traditional blockchain systems. Unlike linear blockchains, BlockDAG allows multiple blocks to be created and processed simultaneously, resulting in higher throughput and faster transaction speeds. This scalability ensures that Kaspa can handle a large number of transactions efficiently, making it ideal for the growing demands of KRC20 tokens and other crypto projects.

Recommendations for Potential Investors

  1. Verify the Source:
  2. Ensure the Kasplex Foundation officially introduced KRC20 tokens for smart contracts and flexibility in Kaspa. Cross-check this with official documentation and announcements.

  3. Understand Minting Details:

  4. Familiarize yourself with the specific minting options available for each KRC20 token. Knowing whether a project has opted for full mintability or pre-minting will help you assess its fairness and decentralization.

  5. Use Trusted Tools:

  6. Regularly check tools like Kasplex and Kastools.com for up-to-date information on token distribution and project developments. These tools are essential for making informed decisions.

  7. Analyze Project Tokenomics:

  8. Look into the tokenomics of each project. Understanding supply/limit ratios and distribution methods will give you insights into potential risks and benefits.

  9. Read the Whitepapers:

  10. Always read the whitepapers of any project you consider investing in. These documents contain crucial information about the project's goals, tokenomics, and roadmap.

Conclusion

KRC20 tokens are paving the way for fairer and more transparent token launches. By offering open-access minting and flexible distribution options, they address many of the issues seen with traditional token launches on other platforms. Additionally, the integration of KRC20 tokens with Kaspa’s BlockDAG technology brings speed and scalability, offering unparalleled benefits to the crypto community. However, it's essential to carefully analyze each project's tokenomics and distribution plans to understand the potential risks and benefits. With the right information, KRC20 tokens can be a powerful tool for creating more equitable and decentralized crypto projects.