meshjs-cardano

meshjs-cardano

MeshJS patterns: tx building, wallet connectors, script interactions. TypeScript/JavaScript Cardano development.

1星標
0分支
更新於 1/25/2026
SKILL.md
readonlyread-only
name
meshjs-cardano
description

"MeshJS patterns: tx building, wallet connectors, script interactions. TypeScript/JavaScript Cardano development."

meshjs-cardano

When to use

  • Building Cardano dApps with MeshJS (TypeScript/JavaScript)
  • Integrating CIP-30 wallet connectors
  • Transaction building in browser or Node.js

Operating rules (must follow)

  • Confirm MeshJS version and environment (Next.js, Vite, Node)
  • Never request seed phrases or private keys
  • Handle wallet disconnects gracefully
  • Test on preprod before mainnet

Setup

Installation

npm install @meshsdk/core @meshsdk/react
# or
yarn add @meshsdk/core @meshsdk/react

Provider setup

import { BlockfrostProvider } from '@meshsdk/core';

const provider = new BlockfrostProvider('<PROJECT_ID>');
// or for preprod:
const provider = new BlockfrostProvider('<PROJECT_ID>', 'preprod');

Wallet connection (React)

import { CardanoWallet, useWallet } from '@meshsdk/react';

function App() {
  const { connected, wallet } = useWallet();

  return (
    <div>
      <CardanoWallet />
      {connected && <p>Connected!</p>}
    </div>
  );
}

Transaction building

Simple ADA transfer

import { Transaction } from '@meshsdk/core';

const tx = new Transaction({ initiator: wallet })
  .sendLovelace(
    'addr_test1qz...',
    '5000000'  // 5 ADA
  );

const unsignedTx = await tx.build();
const signedTx = await wallet.signTx(unsignedTx);
const txHash = await wallet.submitTx(signedTx);

With native tokens

const tx = new Transaction({ initiator: wallet })
  .sendAssets(
    'addr_test1qz...',
    [
      { unit: 'lovelace', quantity: '2000000' },
      { unit: '<policy_id><asset_name_hex>', quantity: '1' }
    ]
  );

With metadata

const tx = new Transaction({ initiator: wallet })
  .sendLovelace('addr_test1qz...', '1000000')
  .setMetadata(674, { msg: ['Hello from MeshJS'] });

Script interactions

Lock funds at script

import { Data } from '@meshsdk/core';

const datum: Data = {
  alternative: 0,
  fields: [wallet.getPaymentAddress()]
};

const tx = new Transaction({ initiator: wallet })
  .sendLovelace(
    {
      address: scriptAddress,
      datum: { inline: datum }
    },
    '5000000'
  );

Spend from script

const tx = new Transaction({ initiator: wallet })
  .redeemValue({
    value: scriptUtxo,
    script: plutusScript,
    datum: datum,
    redeemer: { data: { alternative: 0, fields: [] } }
  })
  .sendLovelace(recipientAddress, '4500000')
  .setCollateral([collateralUtxo]);

Examples

Example: NFT minting

User request: "Mint an NFT with MeshJS"

Response:

import { ForgeScript, Transaction } from '@meshsdk/core';

// Create minting script (time-locked)
const { pubKeyHash } = wallet.getPaymentAddress();
const forgingScript = ForgeScript.withOneSignature(pubKeyHash);

const assetMetadata = {
  name: 'My NFT',
  image: 'ipfs://...',
  mediaType: 'image/png'
};

const tx = new Transaction({ initiator: wallet })
  .mintAsset(
    forgingScript,
    {
      assetName: 'MyNFT',
      assetQuantity: '1',
      metadata: assetMetadata,
      label: '721',
      recipient: wallet.getPaymentAddress()
    }
  );

const unsignedTx = await tx.build();
const signedTx = await wallet.signTx(unsignedTx);
const txHash = await wallet.submitTx(signedTx);
console.log('Minted:', txHash);

Example: Query UTxOs

import { BlockfrostProvider } from '@meshsdk/core';

const provider = new BlockfrostProvider('<PROJECT_ID>');

// Get UTxOs for address
const utxos = await provider.fetchAddressUTxOs(address);

// Get UTxOs at script address
const scriptUtxos = await provider.fetchAddressUTxOs(scriptAddress);

// Filter by asset
const nftUtxos = utxos.filter(utxo =>
  utxo.output.amount.some(a => a.unit.includes(policyId))
);

Common patterns

Error handling

try {
  const txHash = await wallet.submitTx(signedTx);
  console.log('Success:', txHash);
} catch (error) {
  if (error.message.includes('UTxO')) {
    console.log('UTxO issue - refresh and retry');
  } else if (error.message.includes('collateral')) {
    console.log('Need ADA-only UTxO for collateral');
  }
}

Wallet state management

const { connected, connecting, disconnect, wallet, name } = useWallet();

// Check connection before operations
if (!connected) {
  throw new Error('Wallet not connected');
}

// Get network
const network = await wallet.getNetworkId();
// 0 = testnet, 1 = mainnet

Safety / key handling

  • Never request seed phrases
  • Validate CIP-30 API availability
  • Handle disconnects gracefully
  • Show confirmation before signing

References

You Might Also Like

Related Skills

verify

verify

243K

Use when you want to validate changes before committing, or when you need to check all React contribution requirements.

facebook avatarfacebook
獲取
test

test

243K

Use when you need to run tests for React core. Supports source, www, stable, and experimental channels.

facebook avatarfacebook
獲取

Use when feature flag tests fail, flags need updating, understanding @gate pragmas, debugging channel-specific test failures, or adding new flags to React.

facebook avatarfacebook
獲取

Use when adding new error messages to React, or seeing "unknown error code" warnings.

facebook avatarfacebook
獲取
flow

flow

243K

Use when you need to run Flow type checking, or when seeing Flow type errors in React code.

facebook avatarfacebook
獲取
flags

flags

243K

Use when you need to check feature flag states, compare channels, or debug why a feature behaves differently across release channels.

facebook avatarfacebook
獲取