|
| 1 | +--- |
| 2 | +sidebar_position: 4 |
| 3 | +sidebar_label: Minting Native Tokens |
| 4 | +--- |
| 5 | + |
| 6 | +# Minting Native Tokens |
| 7 | + |
| 8 | +This guide demonstrates how to mint native tokens within an open Hydra Head using the cip30 interface and `KuberHydraApiProvider`. |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +- Node.js environment |
| 13 | +- `libcardano` and `libcardano-wallet` installed. |
| 14 | +- An active Hydra Head (in "Open" state). |
| 15 | +- A configured `KuberHydraApiProvider` instance. |
| 16 | +- A minting policy script and its corresponding key hash. |
| 17 | + |
| 18 | +This example demonstrates how to build and submit a transaction that mints native tokens within an open Hydra Head. |
| 19 | + |
| 20 | +```typescript |
| 21 | +import { KuberHydraApiProvider } from "kuber-client"; |
| 22 | +import { Value, Ed25519Key, loadCrypto } from "libcardano"; |
| 23 | +import { ShelleyWallet, Cip30ShelleyWallet } from "libcardano-wallet"; |
| 24 | +import { readFileSync } from "fs"; |
| 25 | + |
| 26 | +async function runMintNativeTokensExample() { |
| 27 | + // Initialize Hydra API Provider (replace with your Hydra node URL) |
| 28 | + const hydra = new KuberHydraApiProvider("http://172.31.6.1:8082"); |
| 29 | + |
| 30 | + // Load test wallet signing key (used for signing the transaction) |
| 31 | + const testWalletSigningKey = await Ed25519Key.fromCardanoCliJson( |
| 32 | + JSON.parse(readFileSync(process.env.HOME + "/.cardano/preview/hydra-0/credentials/funds.sk", "utf-8")), |
| 33 | + ); |
| 34 | + |
| 35 | + // Setup libcardano crypto and Shelley wallet |
| 36 | + await loadCrypto(); |
| 37 | + const shelleyWallet = new ShelleyWallet(testWalletSigningKey); |
| 38 | + const cip30Wallet = new Cip30ShelleyWallet(hydra, hydra, shelleyWallet, 0); |
| 39 | + const walletAddress = (await cip30Wallet.getChangeAddress()).toBech32(); |
| 40 | + |
| 41 | + console.log("Wallet Address:", walletAddress); |
| 42 | + |
| 43 | + // Ensure head is in 'Open' state |
| 44 | + const headState = await hydra.queryHeadState(); |
| 45 | + if (headState.state !== "Open") { |
| 46 | + console.log("Head is not in 'Open' state. Please ensure it's in 'Open' state before running this example."); |
| 47 | + return; |
| 48 | + } |
| 49 | + console.log("Hydra Head is Open. Proceeding with minting transaction."); |
| 50 | + |
| 51 | + // Define the minting transaction |
| 52 | + // For a comprehensive reference on transaction builder fields, refer to: |
| 53 | + // https://kuberide.com/kuber/docs/tx-builder-reference |
| 54 | + const mintingTransaction = { |
| 55 | + mint: [ |
| 56 | + { |
| 57 | + script: { |
| 58 | + type: "sig", |
| 59 | + keyHash: shelleyWallet.paymentKey.pkh.toString('hex'), |
| 60 | + }, |
| 61 | + amount: { |
| 62 | + Token1: 2, |
| 63 | + }, |
| 64 | + }, |
| 65 | + ], |
| 66 | + changeAddress: walletAddress, |
| 67 | + }; |
| 68 | + |
| 69 | + try { |
| 70 | + // Use the buildAndSubmitWithWallet function from KuberProvider to mint tokens |
| 71 | + const mintResult = await hydra.buildAndSubmitWithWallet(cip30Wallet, mintingTransaction); |
| 72 | + console.log("Minting transaction submitted to Hydra Head. Hash:", mintResult.hash); |
| 73 | + console.log("CBOR Hex:", mintResult.cborHex); |
| 74 | + } catch (error: unknown) { |
| 75 | + if (error instanceof Error) { |
| 76 | + console.error("Error building or submitting minting transaction:", error.message); |
| 77 | + } else { |
| 78 | + console.error("An unknown error occurred:", error); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +runMintNativeTokensExample(); |
0 commit comments