Ordinary Transaction with SDK
This article introduces how to construct a standard transaction (P2PKH) using the MetaContract SDK.
Before using this guide, ensure you have completed the environment setup and received test coins. Please refer to Setting Up an MVC Project.
This guide is based on the previously configured environment and keys.
Modify the Source Code
Replace the src/index.ts file with the following content:
import {Mnemonic} from "meta-contract/dist/mvc";
import {promises as fs} from 'fs';
import {API_NET, Wallet} from "meta-contract";
const FILE_PATH = 'mnemonic-seed.txt'
const WALLET_PATH = "m/44'/10001'/0'/0/0"
let seed = '';
const generateMnemonic = async (): Promise<string> => {
console.log("Generating random mnemonic seed: <%s>, use your own if you already have it", Mnemonic.fromRandom().toString())
const mnemonic = Mnemonic.fromRandom().toString();
await fs.writeFile(FILE_PATH, mnemonic, 'utf8');
return mnemonic;
};
const readMnemonic = async (): Promise<string> => {
return await fs.readFile(FILE_PATH, 'utf8');
};
// Detect if the mnemonic seed file exists, if not, generate a new one
// Then create a wallet with the seed with path WALLET_PATH
const setupMyWalletAndSend = async (): Promise<void> => {
try {
await fs.access(FILE_PATH);
const mnemonic = await readMnemonic();
console.log('Mnemonic seed exists: <%s>, will use this one.', mnemonic);
seed = mnemonic;
} catch (error) {
const mnemonic = await generateMnemonic();
console.log('Mnemonic seed not detected, generating new one. <%s> , please keep it safe for future use.', mnemonic);
seed = mnemonic;
}
console.log('Creating wallet with seed: <%s> and path <%s>', seed, WALLET_PATH);
let mnemonicParsed = Mnemonic.fromString(seed);
let privateKey = mnemonicParsed.toHDPrivateKey("", API_NET.TEST).deriveChild(WALLET_PATH);
let wallet = new Wallet(privateKey.privateKey.toWIF(), API_NET.TEST, 1);
console.log("Your private key %s", privateKey.privateKey.toWIF());
console.log("Your address %s", privateKey.privateKey.toAddress(API_NET.TEST).toString());
console.log("Your balance %s satoshis", await wallet.getBalance());
// Send 10000 satoshi back to the faucet
console.log("Sending 10000 satoshis back to the faucet");
wallet
.send("mktGt8zJo5qYmXc97SVvCLPvtLExymgBKF", 10000)
.then((tx) => {
console.log("Transaction sent, txid %s, rawHex %s", tx.txId, tx.txHex);
})
.then(() => wallet.getBalance())
.then((balance) => {
console.log("Your balance now is %s satoshis", balance);
});
};
setupMyWalletAndSend().catch(console.error);The above code adds the logic for sending a transaction after initializing the wallet.
Code Functionality
Initialize the wallet and print out the address and balance.
Send 10,000 satoshis back to the faucet.
Print the transaction ID and the balance after sending the transaction.
If everything runs correctly, you will see an output similar to the following: