Skip to content

Instantly share code, notes, and snippets.

@sekomer
Created April 18, 2024 21:08
Show Gist options
  • Save sekomer/91eba17b43be3edcc2794241a325ca43 to your computer and use it in GitHub Desktop.
Save sekomer/91eba17b43be3edcc2794241a325ca43 to your computer and use it in GitHub Desktop.
Massa Blockchain Transfer Example
/**
Author: Alperen Serkan Aksoz
Date: 18/04/2024
this script helps you to create a massa transfer transaction
don't forget to create .env file and add your PRIVATE_KEY in it
address below is my wallet, you can thank me with Massa, cheers!
AU1TfGZR41Q2ka2HiAi4n7Ai31qbAycYVeEfPRyStv55ADfPKK27
*/
import {
CHAIN_ID,
IAccount,
WalletClient,
DefaultProviderUrls,
IWalletClient,
IClientConfig,
ProviderType,
PublicApiClient,
ITransactionData,
Web3Account,
} from "@massalabs/massa-web3";
import * as dotenv from "dotenv";
dotenv.config();
async function main() {
const privateKey = process.env.PRIVATE_KEY;
if (!privateKey) throw new Error("Private key is not provided");
const chainId = CHAIN_ID.MainNet;
const baseAccount: IAccount = await WalletClient.getAccountFromSecretKey(
privateKey
);
const clientConfig: IClientConfig = {
providers: [
{
type: ProviderType.PUBLIC,
url: DefaultProviderUrls.MAINNET,
},
],
};
const publicApiClient = new PublicApiClient(clientConfig);
const walletClient: IWalletClient = new WalletClient(
clientConfig,
publicApiClient
);
const web3Client: Web3Account = new Web3Account(
baseAccount,
publicApiClient,
chainId
);
// example
await sendTransaction({
walletClient: walletClient,
web3client: web3Client,
to: "AU1TfGZR41Q2ka2HiAi4n7Ai31qbAycYVeEfPRyStv55ADfPKK27",
amount: "1",
});
}
async function sendTransaction({
walletClient,
web3client,
to,
amount,
}: {
walletClient: IWalletClient;
web3client: Web3Account;
to: string;
amount: string;
}) {
const txData: ITransactionData = {
amount: BigInt(amount),
fee: BigInt("1"), // this is the minimum amount of massa (0.000000001)
recipientAddress: to,
};
const tx = await walletClient.sendTransaction(txData, web3client);
console.log(tx);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment