Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function testBlockchain() {
try {
// Get Balance
const balance = await zilliqa.blockchain.getBalance(address);
// Get Minimum Gas Price from blockchain
const minGasPrice = await zilliqa.blockchain.getMinimumGasPrice();
// Account balance (See note 1)
console.log(`Your account balance is:`);
console.log(balance.result);
console.log(`Current Minimum Gas Price: ${minGasPrice.result}`);
const myGasPrice = units.toQa('1000', units.Units.Li); // Gas Price that will be used by all transactions
console.log(`My Gas Price ${myGasPrice.toString()}`);
const isGasSufficient = myGasPrice.gte(new BN(minGasPrice.result)); // Checks if your gas price is less than the minimum gas price
console.log(`Is the gas price sufficient? ${isGasSufficient}`);
// Send a transaction to the network
console.log('Sending a payment transaction to the network...');
const tx = await zilliqa.blockchain.createTransactionWithoutConfirm(
// Notice here we have a default function parameter named toDs which means the priority of the transaction.
// If the value of toDs is false, then the transaction will be sent to a normal shard, otherwise, the transaction.
// will be sent to ds shard. More info on design of sharding for smart contract can be found in.
// https://blog.zilliqa.com/provisioning-sharding-for-smart-contracts-a-design-for-zilliqa-cd8d012ee735.
// For payment transaction, it should always be false.
zilliqa.transactions.new(
{
version: VERSION,
toAddr: '0xA54E49719267E8312510D7b78598ceF16ff127CE',
const isGasSufficient = myGasPrice.gte(new BN(minGasPrice.result)); // Checks if your gas price is less than the minimum gas price
console.log(`Is the gas price sufficient? ${isGasSufficient}`);
// Send a transaction to the network
console.log('Sending a payment transaction to the network...');
const tx = await zilliqa.blockchain.createTransactionWithoutConfirm(
// Notice here we have a default function parameter named toDs which means the priority of the transaction.
// If the value of toDs is false, then the transaction will be sent to a normal shard, otherwise, the transaction.
// will be sent to ds shard. More info on design of sharding for smart contract can be found in.
// https://blog.zilliqa.com/provisioning-sharding-for-smart-contracts-a-design-for-zilliqa-cd8d012ee735.
// For payment transaction, it should always be false.
zilliqa.transactions.new(
{
version: VERSION,
toAddr: '0xA54E49719267E8312510D7b78598ceF16ff127CE',
amount: new BN(units.toQa('1', units.Units.Zil)), // Sending an amount in Zil (1) and converting the amount to Qa
gasPrice: myGasPrice, // Minimum gasPrice veries. Check the `GetMinimumGasPrice` on the blockchain
gasLimit: Long.fromNumber(1),
},
false,
),
);
// check the pending status
const pendingStatus = await zilliqa.blockchain.getPendingTxn(tx.id);
console.log(`Pending status is: `);
console.log(pendingStatus.result);
// process confirm
console.log(`The transaction id is:`, tx.id);
const confirmedTxn = await tx.confirm(tx.id);
const response = yield zilliqa.blockchain.getMinimumGasPrice();
const minGasPriceInQa: string = response.result;
const nonceResponse = yield zilliqa.blockchain.getBalance(address);
const nonceData = nonceResponse.result.nonce || { nonce: 0 };
const nonce: number = nonceData.nonce + 1;
const toAddr = fromBech32Address(toAddress);
const wallet = zilliqa.wallet;
wallet.addByPrivateKey(privateKey);
const tx = new Transaction(
{
version: VERSION,
toAddr,
amount: units.toQa(amount, units.Units.Zil),
gasPrice: new BN(minGasPriceInQa),
gasLimit: Long.fromNumber(1),
pubKey: publicKey,
nonce
},
provider
);
const signedTx = yield wallet.sign(tx);
const { txParams } = signedTx;
// Send a transaction to the network
const data = yield provider.send(RPCMethod.CreateTransaction, txParams);
if (data.error !== undefined) {
throw Error(data.error.message);
export function toZil(value) {
return units.toQa(value, units.Units.Zil).toString();
}
async confirm() {
this.spiner();
const gasFee = {
gasPrice: units.toQa(this.gasPrice, units.Units.Li).toString(),
gasLimit: this.gasLimit
};
try {
if (this.account.hwType) {
await this.hwConfirm(gasFee);
} else {
await this.confirmTx(gasFee);
}
this.popupClouse();
} catch(err) {
this.errMsg = err.message;
}
this.spiner();
},
private getParams = async (toAddr, amountInZil) => {
const response = await zilliqa.blockchain.getMinimumGasPrice();
const gasPrice: string = response.result || '';
const amountInQa = units.toQa(amountInZil, units.Units.Zil);
return {
toAddr,
version,
amount: amountInQa,
gasPrice: new BN(gasPrice.toString()),
gasLimit: Long.fromNumber(1)
};
};
export const formatSendAmountInZil = (
amountInZil: string,
balanceInZil: string,
minGasPriceInZil: string
): string => {
const amountInQaBN: BN = units.toQa(amountInZil, units.Units.Zil);
const balanceInQaBN: BN = units.toQa(balanceInZil, units.Units.Zil);
const minGasPriceInQaBN: BN = units.toQa(minGasPriceInZil, units.Units.Zil);
const maxAmountInQaBN = balanceInQaBN.sub(minGasPriceInQaBN);
if (amountInQaBN.lte(minGasPriceInQaBN)) {
return units.fromQa(minGasPriceInQaBN, units.Units.Zil).toString();
} else if (amountInQaBN.gt(maxAmountInQaBN)) {
return units.fromQa(maxAmountInQaBN, units.Units.Zil).toString();
} else {
return units.fromQa(amountInQaBN, units.Units.Zil).toString();
}
};
export const formatSendAmountInZil = (
amountInZil: string,
balanceInZil: string,
minGasPriceInZil: string
): string => {
const amountInQaBN: BN = units.toQa(amountInZil, units.Units.Zil);
const balanceInQaBN: BN = units.toQa(balanceInZil, units.Units.Zil);
const minGasPriceInQaBN: BN = units.toQa(minGasPriceInZil, units.Units.Zil);
const maxAmountInQaBN = balanceInQaBN.sub(minGasPriceInQaBN);
if (amountInQaBN.lte(minGasPriceInQaBN)) {
return units.fromQa(minGasPriceInQaBN, units.Units.Zil).toString();
} else if (amountInQaBN.gt(maxAmountInQaBN)) {
return units.fromQa(maxAmountInQaBN, units.Units.Zil).toString();
} else {
return units.fromQa(amountInQaBN, units.Units.Zil).toString();
}
};