@0xcert/wanchain-utils 中文文档教程

发布于 4年前 浏览 27 项目主页 更新于 3年前

通用 Wanchain 实用模块,具有 Wanchain 区块链的辅助功能。

0xcert Framework 是一个免费的开源 JavaScript 库,它提供用于构建强大的去中心化应用程序的工具。 请参阅官方文档了解更多详情。

该模块是 0xcert Framework 的组成部分之一。 它是用 TypeScript 编写的,并且得到积极维护。 源代码可在 GitHub 上找到,您还可以在其中找到我们的 问题跟踪器

Wanchain Utilities

该模块包装了几个有用的 Wanchain 函数,这些函数将通过 0xcert 框架发挥作用。 当前支持的是来自 ethers.js 的这些函数和类:

ABI coder

这将值与打包的 以太坊 ABI 编码

  • encodeParameters(types: any, values: Array<any>): string
  • decodeParameters(types: any, data: any): any

编码示例:

import { decodeParameters, encodeParameters } from '0xcert/ethereum-utils/abi';

const types = ['tuple(uint256, uint256[])'];
const values = [[ 42, [ 45 ] ]];
const encodedValues = encodeParameters(types, values);

解码示例:

import { decodeParameters, encodeParameters } from '0xcert/ethereum-utils/abi';

const types = ['tuple(uint256, uint256[])'];
const encoded = '0x' +
  '0000000000000000000000000000000000000000000000000000000000000020' +
  '000000000000000000000000000000000000000000000000000000000000002a' +
  '0000000000000000000000000000000000000000000000000000000000000040' +
  '0000000000000000000000000000000000000000000000000000000000000001' +
  '000000000000000000000000000000000000000000000000000000000000002d';
const values = decodeParameters(types, values);

Big numbers

这是一个改编自 ethers.js 文档

import { bigNumberify } from '0xcert/ethereum-utils/big-number';

let gasPriceWei = bigNumberify("20902747399");
let gasLimit = bigNumberify(3000000);

let maxCostWei = gasPriceWei.mul(gasLimit)
console.log("Max Cost: " + maxCostWei.toString());
// "Max Cost: 62708242197000000"

console.log("Number: " + maxCostWei.toNumber());
// throws an Error, the value is too large for JavaScript to handle safely

Address normalization

ethers.js 地址规范化函数实现EIP-55 混合大小写校验和地址编码

import { getAddress } from '0xcert/ethereum-utils/normalize-address';

let zxcTokenAddress = '0x83e2be8d114f9661221384b3a50d24b96a5653f5';
let zxcTokenAddressNormalized = normalizeAddress(zxcToenAddress);
// 0x83e2BE8d114F9661221384B3a50d24B96a5653F5

General Wanchain utility module with helper functions for the Wanchain blockchain.

The 0xcert Framework is a free and open-source JavaScript library that provides tools for building powerful decentralized applications. Please refer to the official documentation for more details.

This module is one of the bricks of the 0xcert Framework. It's written with TypeScript and it's actively maintained. The source code is available on GitHub where you can also find our issue tracker.

Wanchain Utilities

This module wraps several useful Wanchain functions which will be useful through the 0xcert Framework. Currently supported are these functions and classes from ethers.js:

ABI coder

This converts the value to and from the packed Ethereum ABI encoding.

  • encodeParameters(types: any, values: Array<any>): string
  • decodeParameters(types: any, data: any): any

Encoding example:

import { decodeParameters, encodeParameters } from '0xcert/ethereum-utils/abi';

const types = ['tuple(uint256, uint256[])'];
const values = [[ 42, [ 45 ] ]];
const encodedValues = encodeParameters(types, values);

Decoding example:

import { decodeParameters, encodeParameters } from '0xcert/ethereum-utils/abi';

const types = ['tuple(uint256, uint256[])'];
const encoded = '0x' +
  '0000000000000000000000000000000000000000000000000000000000000020' +
  '000000000000000000000000000000000000000000000000000000000000002a' +
  '0000000000000000000000000000000000000000000000000000000000000040' +
  '0000000000000000000000000000000000000000000000000000000000000001' +
  '000000000000000000000000000000000000000000000000000000000000002d';
const values = decodeParameters(types, values);

Big numbers

Here is a basic example adapted from the ethers.js documentation.

import { bigNumberify } from '0xcert/ethereum-utils/big-number';

let gasPriceWei = bigNumberify("20902747399");
let gasLimit = bigNumberify(3000000);

let maxCostWei = gasPriceWei.mul(gasLimit)
console.log("Max Cost: " + maxCostWei.toString());
// "Max Cost: 62708242197000000"

console.log("Number: " + maxCostWei.toNumber());
// throws an Error, the value is too large for JavaScript to handle safely

Address normalization

The ethers.js address normalization function implements EIP-55 Mixed-case checksum address encoding.

import { getAddress } from '0xcert/ethereum-utils/normalize-address';

let zxcTokenAddress = '0x83e2be8d114f9661221384b3a50d24b96a5653f5';
let zxcTokenAddressNormalized = normalizeAddress(zxcToenAddress);
// 0x83e2BE8d114F9661221384B3a50d24B96a5653F5
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文