Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// 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);
console.log(`The transaction status is:`);
console.log(confirmedTxn.receipt);
type: 'ByStr20',
value: `${address}`,
},
];
// Instance of class Contract
const contract = zilliqa.contracts.new(code, init);
// Deploy the contract.
// Also notice here we have a default function parameter named toDs as mentioned above.
// A contract can be deployed at either the shard or at the DS. Always set this value to false.
const [deployTx, hello] = await contract.deploy(
{
version: VERSION,
gasPrice: myGasPrice,
gasLimit: Long.fromNumber(10000),
},
33,
1000,
false,
);
// Introspect the state of the underlying transaction
console.log(`Deployment Transaction ID: ${deployTx.id}`);
console.log(`Deployment Transaction Receipt:`);
console.log(deployTx.txParams.receipt);
// Get the deployed contract address
console.log('The contract address is:');
console.log(hello.address);
//Following line added to fix issue https://github.com/Zilliqa/Zilliqa-JavaScript-Library/issues/168
const deployedContract = zilliqa.contracts.at(hello.address);
const claimedAt = doc.data().claimed_at;
console.log(`The latest claimed at: ${claimedAt}`);
claimInterval = Date.now() - claimedAt;
console.log(`Interval: ${claimInterval}`);
} else {
console.log('New address');
}
let faucetAmount: number = DEFAULT_TRANSFER_AMOUNT;
if (claimInterval !== undefined && claimInterval < PENALTY_TIME) {
faucetAmount = PENALTY_TRANSFER_AMOUNT;
}
console.log(`Faucet amount: ${faucetAmount}`);
const gasLimit = Long.fromNumber(1);
const amount = units.toQa(faucetAmount.toString(), units.Units.Zil); // Sending an amount measured in Zil, converting to Qa.
const gasResponse = await zilliqa.blockchain.getMinimumGasPrice();
const minGasPrice: string = gasResponse.result;
console.log('Min gas price:', minGasPrice);
const gasPrice = new BN(minGasPrice);
const pubKey = PUBLIC_KEY;
const toAddr = address;
const response = await zilliqa.blockchain.getBalance(ADDRESS);
const nonceResult = response.result || { nonce: 0 };
const nonce: number = nonceResult.nonce + 1;
console.log('Nonce:', nonce);
const wallet = zilliqa.wallet;
version // Netowrk version. type Number.
} = txData;
if (!version) {
version = await this.version();
}
if (isNaN(nonce)) {
nonce = balance.nonce;
}
if (currentNonce > balance.nonce) {
nonce = currentNonce;
}
amount = new BN(amount);
gasPrice = new BN(gasPrice);
gasLimit = Long.fromNumber(gasLimit);
nonce++;
return this.transactions.new({
nonce,
gasPrice,
amount,
gasLimit,
version,
toAddr,
pubKey,
code,
data
});
}
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);
}
version // Netowrk version. type Number.
} = txData;
if (!version) {
version = await this.version(msgId);
}
if (isNaN(nonce)) {
nonce = balance.nonce;
}
if (currentNonce > balance.nonce) {
nonce = currentNonce;
}
amount = new BN(amount);
gasPrice = new BN(gasPrice);
gasLimit = Long.fromNumber(gasLimit);
nonce++;
const zilTxData = this.transactions.new({
nonce,
gasPrice,
amount,
gasLimit,
version,
toAddr,
pubKey,
code,
data
});
// Sign transaction by current account. //
const { txParams } = await this.wallet.sign(zilTxData);
if (!version) {
version = await this.version()
}
if (isNaN(nonce)) {
nonce = balance.nonce
}
if (currentNonce > balance.nonce) {
nonce = currentNonce
}
amount = new BN(amount)
gasPrice = new BN(gasPrice)
gasLimit = Long.fromNumber(gasLimit)
nonce++
return this.transactions.new({
nonce,
gasPrice,
amount,
gasLimit,
version,
toAddr,
pubKey,
code,
data
})
}
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)
};
};