Executing a standalone script
Run JavaScript/TypeScript files directly to interact with the blockchain using the tsx package.
Set up a simple project using MeshSDK. Create a wallet, build and sign transactions, and submit them to the blockchain.
This tutorial covers:
- Creating a
package.jsonfile. - Installing MeshSDK and dependencies.
- Writing a script to create a wallet and send a transaction.
- Running the project.
System Setup
Create package.json
Create package.json in the project root:
{
"type": "module",
"dependencies": {},
"scripts": {
"dev": "tsx index.ts"
}
}Install Packages
Install required packages:
npm install
npm install tsx @meshsdk/corepackage.json should look like this:
{
"type": "module",
"dependencies": {
"@meshsdk/core": "^1.5.18",
"tsx": "^4.9.4"
},
"scripts": {
"dev": "tsx index.ts"
}
}@meshsdk/core: Core functionality.tsx: Runs TypeScript files directly.
Make a Simple Transaction
Create index.ts
Create index.ts and add the following code:
import { BlockfrostProvider, MeshWallet, Transaction } from "@meshsdk/core";
// Set up the blockchain provider with your key
const provider = new BlockfrostProvider("YOUR_KEY_HERE");
// Initialize the wallet with a mnemonic key
const wallet = new MeshWallet({
networkId: 0,
fetcher: provider,
submitter: provider,
key: {
type: "mnemonic",
words: [
"your", "mnemonic", "...", "here",
],
},
});
// Create and send a transaction
const tx = new Transaction({ initiator: wallet }).sendLovelace(
"addr_test1qp2k7wnshzngpqw0xmy33hvexw4aeg60yr79x3yeeqt3s2uvldqg2n2p8y4kyjm8sqfyg0tpq9042atz0fr8c3grjmysdp6yv3",
"1000000"
);
const unsignedTx = await tx.build();
const signedTx = await wallet.signTx(unsignedTx);
const txHash = await wallet.submitTx(signedTx);Run Application
Replace YOUR_KEY_HERE with a valid Blockfrost key and the mnemonic words with your own. Get a key from Blockfrost and generate a mnemonic from Mesh.
Start the application:
npm run devThe transaction hash will log to the console.
View the complete code in the Mesh GitHub repo.