Created
April 18, 2024 21:08
-
-
Save sekomer/91eba17b43be3edcc2794241a325ca43 to your computer and use it in GitHub Desktop.
Massa Blockchain Transfer Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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