@bitgo/eth-contracts

中文文档
1.0.0发布于 2020/12/12TypeScriptCommonJS近 30 天下载 3.3万

A library to build Ethereum smart contract function calls.

原始使用文档
文档目录7 节
  1. Installing
  2. Example Usage
  3. Integration with BitGo SDK
  4. Example Usage with BitGo
  5. Types
  6. Supported Protocols:
  7. Adding a new ABI type

Ethereum Contract SDK

A simple library for building Ethereum smart contract interactions. When offline, or away from a web3 wallet, smart contract interaction is quite difficult. This is because it requires special transaction data that defines a function call on a smart contract. This library intends to improve this experience by providing a simple interface for common smart contract function calls. It also aims to be extensible to a wide variety of contracts.

Installing

npm i @bitgo/eth-contracts

Example Usage

The basic usage enables users to specify contracts by name and build transaction data from them.

import { Contract } from '@bitgo/eth-contracts';
const cDAI = new Contract('Compound').instance('cDAI');
const { data, amount, address } = cDAI.methods().mint.call({ mintAmount: '1000000000' });

Users can specify an instance of the contract protocol by address instead of name

import { Contract } from '@bitgo/eth-contracts';
const cDAI = new Contract('Compound').address('0x5d3a536e4d6dbd6114cc1ead35777bab948e3643');
const { data, amount, address } = cDAI.methods().mint.call({ mintAmount: '1000000000' });

The decoder can parse call data and output a human-readable explanation of a given contract call.

import { Decoder } from '@bitgo/eth-contracts';
const decoder = new Decoder();
decoder.decode(Buffer.from('a9059cbb00000000000000000000000010d4f942617a231eb1430c88fe43c8c2050437d90000000000000000000000000000000000000000000000000000000000002710', 'hex'));
{ methodId: '0xa9059cbb',
  name: 'transfer',
  args:
   [ { name: '_to',
       type: 'address',
       value: '0x10d4f942617a231eb1430c88fe43c8c2050437d9' },
     { name: '_value', type: 'uint256', value: 10000 } ],
  contractName: 'StandardERC20' }

Integration with BitGo SDK

The output of this library is well formed as an argument to a BitGo SDK sendMany call. This makes it useful for integration alongside the Bitgo SDK.

Example Usage with BitGo

import { Contract } from '@bitgo/eth-contracts';

import { BitGo, Coin } from 'bitgo';

async function sendBitGoTx() {
    const bitGo = new BitGo({ env: 'test' });
    const baseCoin = bitGo.coin('eth');
    const bitGoWallet = await baseCoin.wallets().get({ id: '5941ce2db42fcbc70717e5a898fd1595' });

    const cDAI = new Contract('Compound').instance('cDAI');
    
    const transaction = await bitGoWallet.sendMany({
      recipients: cDAI.methods().mint.call({ mintAmount: '1000000000' }),
      walletPassphrase: 'password'
    })
      
}

sendBitGoTx();

Types

Contract

listContractTypes() -- get the available contract types.

const types = Contract.listContractTypes();
// response: ['Compound', 'StandardERC20']

listMethods() -- get the available contract methods.

const types = new Contract('StandardERC20').listMethods();
// response: [{ name: 'transfer', inputs: [...], outputs: [...] }, { name: 'approve', ... }]

methods() -- get contract method builder objects

const types = new Contract('StandardERC20').methods();
// response: { transfer: { call: <function to build transfer> }, approve: { call: <function to build approve> } }

getName() -- get contract name

const types = new Contract('StandardERC20').getName();
// response: StandardERC20

address() -- set contract address

const types = new Contract('StandardERC20').address('0x5d3a536e4d6dbd6114cc1ead35777bab948e3643');
// response: Contract with address set

instance() -- set contract instance

const types = new Contract('StandardERC20').instance('DAI');
// response: Contract with DAI address set

Supported Protocols:

This library supports a limited number of smart contract protocols, as it maintains solidity ABIs locally.

Adding a new ABI type

This library is quite extensible to new protocols -- if there are other contract types that you would like to use, feel free to submit a PR adding them. To do so, make the following changes:

  • Add the JSON ABI to abis directory, named [ProtocolName].json
  • Add the ProtocolName and addresses for various instances of the protocol in config/instances.json
    • For example, Compound protocol has cDAI, cUSDC, etc.
  • Add the protocol to the README above
  • Add some example usages in the examples directory

版本与发布记录

@bitgo/eth-contracts 共发布 1 个版本,首次发布于 2020/12/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2020/12/125 年前

关于 @bitgo/eth-contracts 的常见问题

@bitgo/eth-contracts 是什么?

A library to build Ethereum smart contract function calls.

如何安装 @bitgo/eth-contracts?

在项目目录运行 npm install @bitgo/eth-contracts,或使用 pnpm add / yarn add / bun add。

@bitgo/eth-contracts 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bitgo/eth-contracts 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bitgo/eth-contracts 可以商用吗?

@bitgo/eth-contracts 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@bitgo/eth-contracts 的下载量有多少?

根据 npm 官方下载统计,@bitgo/eth-contracts 在 2026/10/08 – 2026/11/06 这 30 天内被下载 33,291 次,日均约 1,110 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bitgo/eth-contracts 还在维护吗?

最新版本 1.0.0 发布于 2020/12/12(5 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/12/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。