How to use @zilliqa-js/util - 10 common examples

To help you get started, we’ve selected a few @zilliqa-js/util examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Zilliqa / Zilliqa-JavaScript-Library / examples / helloWord.js View on Github external
// 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);
github Zilliqa / Zilliqa-JavaScript-Library / examples / helloWord.js View on Github external
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);
github Zilliqa / nucleus-wallet / functions / src / index.ts View on Github external
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;
github Zilliqa / Zilliqa-JavaScript-Library / examples / helloWord.js View on Github external
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',
github Zilliqa / Zilliqa-JavaScript-Library / examples / helloWord.js View on Github external
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);
github Zilliqa / Zilliqa-JavaScript-Library / packages / zilliqa-js-account / src / transaction.ts View on Github external
private async getBlockNumber(): Promise {
    try {
      const res: RPCResponse = await this.provider.send(
        RPCMethod.GetLatestTxBlock,
      );
      if (res.error === undefined && res.result.header.BlockNum) {
        // if blockNumber is too high, we use BN to be safer
        return new BN(res.result.header.BlockNum);
      } else {
        throw new Error('Can not get latest BlockNumber');
      }
    } catch (error) {
      throw error;
    }
  }
github zilpay / zil-pay / extension / controllers / services / blockchain / zilliqa.js View on Github external
nonce,
      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
    });
  }
github zilpay / zil-pay / extension / controllers / services / blockchain / zilliqa.js View on Github external
toAddr,   // Recipient address. type String.
      nonce,
      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
    });
github zilpay / zil-pay / extension / controllers / services / blockchain / zilliqa.js View on Github external
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
    });
  }
github Zilliqa / nucleus-wallet / src / redux / zil / sagas.ts View on Github external
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);
    }