Ethereum合同 SDK
一个简单的图书馆,用于构建埃特鲁姆智能合约互动. 离线时, 或远离 Web3 钱包, 聪明 合同互动相当困难。 因为它需要特别的交易数据来定义 函数调用智能合同。 该图书馆打算通过提供一个简单的接口来改进这一经验 为常见的智能合同功能呼叫。 它还旨在推广到各种合同.
安装
npm i @bitgo/eth-contracts
示例使用
基本使用使用户能够按名称指定合同,并从中建立交易数据.
import { Contract } from '@bitgo/eth-contracts';
const cDAI = new Contract('Compound').instance('cDAI');
const { data, amount, address } = cDAI.methods().mint.call({ mintAmount: '1000000000' });
用户可以通过地址而不是名称指定合同协议实例
import { Contract } from '@bitgo/eth-contracts';
const cDAI = new Contract('Compound').address('0x5d3a536e4d6dbd6114cc1ead35777bab948e3643');
const { data, amount, address } = cDAI.methods().mint.call({ mintAmount: '1000000000' });
解码器可以解析数据,输出对特定合同呼叫的可人读解释.
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' }
与 BitGo SDK 集成
这个库的输出是作为 BitGo SDK 的参数而形成的 sendMany 电话 这使得它有用
与 Bitgo SDK 合并.
使用比特Go的示例
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();
类型
合同
合同类型( ) - 获取可用的合同类型.
const types = Contract.listContractTypes();
// response: ['Compound', 'StandardERC20']
列表 方法( ) - 获取可用的合同方法.
const types = new Contract('StandardERC20').listMethods();
// response: [{ name: 'transfer', inputs: [...], outputs: [...] }, { name: 'approve', ... }]
方法( ) -- 获得合同方法构建对象
const types = new Contract('StandardERC20').methods();
// response: { transfer: { call: <function to build transfer> }, approve: { call: <function to build approve> } }
获取Name () - 获得合同名称
const types = new Contract('StandardERC20').getName();
// response: StandardERC20
地址( ) -- 设定合同地址
const types = new Contract('StandardERC20').address('0x5d3a536e4d6dbd6114cc1ead35777bab948e3643');
// response: Contract with address set
实例( ) -- 定案合同
const types = new Contract('StandardERC20').instance('DAI');
// response: Contract with DAI address set
支持的协议 :
这个库支持数量有限的智能合同协议,因为它在当地维持固态ABIS.
添加新的 ABI 类型
这个图书馆非常适用于新的协议 -- 如果有其他的合同类型,你想使用, 随你便 提交公关协议 加入它们。 为此,作出下列修改:
- 将 JSON ABI 添加到
abis目录, 名称[ProtocolName].json - 在
config/instances.json- 例如,化合物协议有cDAI,cUSDC等.
- 将协议添加到上面的 README
- 在其中添加一些示例用法
examples目录