Skip to main content

Send Transactions to PWR Chain

Sending transactions is a fundamental operation when interacting with the PWR Chain. The PWR SDK provides a simple and intuitive way to send various types of transactions within your software application.

PWR Chain Faucet

To send transactions to PWR Chain you need to have some coins in your wallet. You can use PWR Chain faucet to get some testnet coins.

Just add your wallet address and then claim your coins by clicking on "Give Me 10 PWR".

PWR Chain Transactions

To send transactions to PWR Chain, all what you need's setup the PWR SDK in your project and create wallet as we did in the previous lessons, and check the address balance so that you have enough gas fees.

Types of PWR Chain transactions - there are many types of PWR SDKs, in this guide we will focus on the following:

  • Transfer PWR: Transfers PWR tokens from one wallet to another, facilitating token movement within the network.
  • Send VM Data: Sends data to a specific virtual machine for storage and processing purposes.
  • Send Payable VM Data: Sends data to a specific virtual machine (VM ID) and transfers PWR tokens to the VM as part of the operation.
  • Delegate: Delegates a specified amount of PWR tokens to a validator, contributing to their staking power.
  • Withdraw: Withdraws PWR tokens that were previously delegated to a validator, returning them to the user’s wallet.
  • Move Stake: Moves staked PWR tokens from one validator to another, allowing for flexibility in staking management.
  • Set Guardian: Assigns a trusted guardian to the user’s wallet, providing an added layer of security and control.
  • Remove Guardian: Remove the guardian assigned to the wallet, removing their access or control.

Transfer PWR

To transfer PWR tokens from one wallet to another, use the transfer PWR method:

const { PWRWallet } = require('@pwrjs/core');

// Setting up your wallet in the SDK
const privateKey = "YOUR_PRIVATE_KEY_HERE";
const wallet = new PWRWallet(privateKey);

async function transfer() {
// Tokens recipient address
const recipientAddress = "RECIPIENT_ADDRESS";
// Tokens amount - 1 PWR = 1e9 = 1000000000
const amount = 1e3;
// Transfer pwr tokens from the wallet
const txHash = await wallet.transferPWR(recipientAddress, amount);

// Error handling
if (txHash.success) {
console.log("Transaction Hash:", txHash.transactionHash);
} else {
console.log("Error:", txHash.message);
}
}
transfer();

Send VM Data

Sends data to a specific virtual machine for storage and processing purposes.

const { PWRWallet } = require('@pwrjs/core');

// Setting up your wallet in the SDK
const privateKey = "YOUR_PRIVATE_KEY_HERE";
const wallet = new PWRWallet(privateKey);

async function sendData() {
// VM id used to send the transaction to
const vmId = 123;
// Buffer data to be included in the transaction
const data = Buffer.from('Hello World!');

// Send the data at vmID 123 to the chain
const txHash = await wallet.sendVMDataTxn(vmId, data);

// Error handling
if (txHash.success) {
console.log("Transaction Hash:", txHash.transactionHash);
} else {
console.log("Error:", txHash.message);
}
}
sendData()

You must have wondered, why doesn’t the VM ID work specifically to suit my needs and the rules I want? It seems random? Of course not, we will explain this in the next guide on Claim a VM ID. KEEP DIVING IN.

Send Payable VM Data

Sends data to a specific virtual machine (VM ID) and transfers PWR tokens to the VM as part of the operation.

const { PWRWallet } = require('@pwrjs/core');

// Setting up your wallet in the SDK
const privateKey = "YOUR_PRIVATE_KEY_HERE";
const wallet = new PWRWallet(privateKey);

async function sendPayableData() {
// VM id used to send the transaction to
const vmId = 919;
// Tokens amount - 1 PWR = 1e9 = 1000000000
const amount = 1000;
// Buffer data to be included in the transaction
const data = Buffer.from('Hello World!');

// Send the data at vmID 919 and pay 1e3
const txHash = await wallet.sendPayableVmDataTransaction(vmId, amount, data);

// Error handling
if (txHash.success) {
console.log("Transaction Hash:", txHash.transactionHash);
} else {
console.log("Error:", txHash.message);
}
}
sendPayableData()

Delegate PWR Tokens

Delegates a specified amount of PWR tokens to a validator, contributing to their staking power.

const { PWRWallet } = require('@pwrjs/core');

// Setting up your wallet in the SDK
const privateKey = "YOUR_PRIVATE_KEY_HERE";
const wallet = new PWRWallet(privateKey);

async function delegate() {
// Validator address
const validator = "VALIDATOR_ADDRESS";
// Tokens amount - 1 PWR = 1e9 = 1000000000
const amount = 1e9;

// Delegate the validator
const txHash = await wallet.delegate(validator, amount);

// Error handling
if (txHash.success) {
console.log("Transaction Hash:", txHash.transactionHash);
} else {
console.log("Error:", txHash.message);
}
}
delegate()

Withdraw Delegated PWR Tokens

Withdraws PWR tokens that were previously delegated to a validator, returning them to the user’s wallet.

const { PWRWallet } = require('@pwrjs/core');

// Setting up your wallet in the SDK
const privateKey = "YOUR_PRIVATE_KEY_HERE";
const wallet = new PWRWallet(privateKey);

async function withdraw() {
// Validator address you delegated
const validator = "VALIDATOR_ADDRESS";
// Tokens amount - 1 PWR = 1e9 = 1000000000
const amount = 1e9;

// Withdraw the delegated pwr tokens
const txHash = await wallet.withdraw(validator, amount);

// Error handling
if (txHash.success) {
console.log("Transaction Hash:", txHash.transactionHash);
} else {
console.log("Error:", txHash.message);
}
}
withdraw()

Move Stake Between Validators

To move delegated stake from one validator to another.

const { PWRWallet } = require('@pwrjs/core');

// Setting up your wallet in the SDK
const privateKey = "YOUR_PRIVATE_KEY_HERE";
const wallet = new PWRWallet(privateKey);

async function moveStake() {
const fromValidator = "FROM_VALIDATOR_ADDRESS";
const toValidator = "TO_VALIDATOR_ADDRESS";
// Tokens amount - 1 PWR = 1e9 = 1000000000
const amount = 1e9;

// Move stake token from validator to another
const txHash = await wallet.moveStake(amount, fromValidator, toValidator);

// Error handling
if (txHash.success) {
console.log("Transaction Hash:", txHash.transactionHash);
} else {
console.log("Error:", txHash.message);
}
}
moveStake()

Set Guardian

Adding a guardian to your wallet greatly enhances the security of your wallet from mistakes or hacks.

Assigns a trusted guardian to your wallet, providing an added layer of security and control.

Here's how the guardian process works:

  1. Alice designates Bob as her wallet guardian.
  2. Alice initiates a transaction, sending 1 PWR to Eve.
  3. The transaction is placed in a queue, awaiting Bob's approval. He can either approve it or reject it if something seems suspicious.
  4. Eve will receive the 1 PWR only after Bob approves the transaction.
  5. Mallory has already obtained Alice's wallet's private key and successfully stolen all of her coins.
  6. Mallory's transaction will remain in the queue and can only be executed if Bob approves it.
  7. Since Bob rejects the suspicious transaction, Mallory was unable to steal any of Alice's coins.
const { PWRWallet } = require('@pwrjs/core');

// Setting up your wallet in the SDK
const privateKey = "YOUR_PRIVATE_KEY_HERE";
const wallet = new PWRWallet(privateKey);

async function setGuardian() {
// Guardian address that will verify your transactions
const guardian = "GUARDIAN_ADDRESS";

// Guardian validity period - 30 minutes
const futureDate = new Date();
futureDate.setDate(futureDate.getMinutes() + 30); // 30 minutes from now
const expiryDate = Math.floor(futureDate.getTime() / 1000);

// Set your wallet guardian
const txHash = await wallet.setGuardian(guardian, expiryDate);

// Error handling
if (txHash.success) {
console.log("Transaction Hash:", txHash.transactionHash);
} else {
console.log("Error:", txHash.message);
}
}
setGuardian()

NOTE: Choose the guardian of your wallet carefully and set an expiration period such as a week or less so that nothing happens to your money if anything happens to the guardian's wallet.

Remove Guardian

Remove the guardian assigned to the wallet, removing their access or control.

const { PWRWallet } = require('@pwrjs/core');

// Setting up your wallet in the SDK
const privateKey = "YOUR_PRIVATE_KEY_HERE";
const wallet = new PWRWallet(privateKey);

async function removeGuardian() {
// Remove your wallet guardian
const txHash = await wallet.removeGuardian();

// Error handling
if (txHash.success) {
console.log("Transaction Hash:", txHash.transactionHash);
} else {
console.log("Error:", txHash.message);
}
}
removeGuardian()

Transaction Fees

When sending transactions, be aware of the transaction fees associated with each transaction type. The PWR Chain charges fees based on the transaction size and the current network conditions. The PWR SDK automatically calculates and includes the necessary fees for you.

Ensure that your wallet has sufficient PWR balance to cover the transaction fees. If the wallet balance is insufficient, the transaction will fail.