TypeScript web3-utils hexToNumber示例

说明

typescript web3-utils hextonumber示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: TypeScript

命名空间/包名称: web3-utils

示例#1
文件: formatters.ts 项目: littycoin/thorify

  web3.extend.formatters.outputTransactionReceiptFormatter = function(receipt: any) {
    if (receipt && receipt.isThorified) {
      debug("outputTransactionReceiptFormatter");

      if (receipt.hasOwnProperty("transactionIndex")) {
        delete receipt.transactionIndex;
      }
      if (receipt.hasOwnProperty("cumulativeGasUsed")) {
        delete receipt.cumulativeGasUsed;
      }

      receipt.gasUsed = web3Utils.hexToNumber(receipt.gasUsed);

      if (receipt.gasPayer) {
        receipt.gasPayer = web3Utils.toChecksumAddress(receipt.gasPayer);
      }
      if (receipt.meta && receipt.meta.txOrigin) {
        receipt.meta.txOrigin = web3Utils.toChecksumAddress(receipt.meta.txOrigin);
      }

      for (const output of receipt.outputs) {
        if (web3Utils._.isArray(output.events)) {
          output.events = output.events.map((event: any) => {
            if (!event.isThorified) {
              Object.defineProperty(event, "isThorified", { get: () => true});
            }
            return web3.extend.formatters.outputLogFormatter(event);
          });
        }

        if (output.contractAddress) {
          output.contractAddress = web3Utils.toChecksumAddress(output.contractAddress);
        }
      }

      return receipt;
    } else {
      return outputTransactionReceiptFormatter(receipt);
    }
  };

示例#2
文件: formatters.ts 项目: littycoin/thorify

  web3.extend.formatters.outputTransactionFormatter = function(tx: any) {
    if (tx && tx.isThorified) {
      debug("outputTransactionFormatter");
      tx.gas = web3Utils.hexToNumber(tx.gas);
      tx.chainTag = web3Utils.numberToHex(tx.chainTag);

      if (tx.origin) {
        tx.origin = web3Utils.toChecksumAddress(tx.origin);
      }
      if (tx.clauses) {
        for (const clause of tx.clauses) {
          clause.value = web3.extend.formatters.outputBigNumberFormatter(clause.value);
          if (clause.to && web3Utils.isAddress(clause.to)) { // tx.to could be `0x0` or `null` while contract creation
            clause.to = web3Utils.toChecksumAddress(clause.to);
          } else {
            clause.to = null; // set to `null` if invalid address
          }
        }
      }
      return tx;
    } else {
      return outputTransactionFormatter(tx);
    }
  };

展开阅读全文