文章目录
SDK
HermezJS是与Hermez Rollup网络交互的开源SDK。 它可以作为npm软件包或通过github下载。
SDK介绍
在本教程中,我们将逐步介绍使用SDK的过程:
-
创建一个钱包
-
从以太坊存入Hermez网络
-
进行转帐
-
检查交易状态
-
将资金撤回以太坊网络。
npm安装
npm i @hermeznetwork/hermezjs
Import modules 导入模块
加载hermezjs库
import hermez from './src/index.js'
创建交易池
初始化用于存储用户交易的存储。 这需要在您的应用程序开始时进行初始化。
hermez.txPool.initializeTransactionPool()
连接以太坊网络
Hermez网络中的某些操作(例如发送L1交易)需要与智能合约进行交互。 因此,有必要初始化一个以太坊provider。 在此示例中,我们已在本地区块链中部署了合约。
hermez.Providers.setProvider('http://localhost:8545')
rinkeby测试网官网:
https://www.rinkeby.io/
检查令牌存在于Hermez网络中
在Hermez网络上运行之前,我们必须确保列出了要使用的令牌。 为此,我们调用了Hermez Coordinator API,该API将列出所有可用的令牌。 Hermez Network中的所有令牌都必须为ERC20。 对于本示例的其余部分,我们将使用在Hermez网络中注册的令牌之一。 在此示例中,注册了4个令牌。
const token = await hermez.CoordinatorAPI.getTokens()
console.log(token)
const tokenERC20 = token.tokens[3]
>>>>
{
tokens: [
{
itemId: 1,
id: 0,
ethereumBlockNum: 0,
ethereumAddress: '0x0000000000000000000000000000000000000000',
name: 'Ether',
symbol: 'ETH',
decimals: 18,
USD: null,
fiatUpdate: null
},
{
itemId: 2,
id: 1,
ethereumBlockNum: 37,
ethereumAddress: '0x24650cad431915051e2987455b76e0cdcaa1d4d8',
name: 'ERC20_0',
symbol: '20_0',
decimals: 18,
USD: null,
fiatUpdate: null
},
{
itemId: 3,
id: 2,
ethereumBlockNum: 49,
ethereumAddress: '0x715752d24f27224d4a88957896a141df87a50448',
name: 'ERC20_1',
symbol: '20_1',
decimals: 18,
USD: null,
fiatUpdate: null
},
{
itemId: 4,
id: 3,
ethereumBlockNum: 61,
ethereumAddress: '0x2e9f55f7266d8c7e07d359daba0e743e331b7a1a',
name: 'ERC20_2',
symbol: '20_2',
decimals: 18,
USD: null,
fiatUpdate: null
}
]
}
PS, 其实就是获取可以用于layer2 抵押代币的token列表。如目前只有ETH、HEZ。
创建钱包
PS: 官方example程序:
https://github.com/hermeznetwork/hermezjs/tree/main/examples
hermezjs/examples/creates-accounts-deposit.js
我们可以通过提供与初始化的 providing相关的以太坊账户索引来创建一个新的Hermez钱包。 该钱包将存储Hermez帐户的以太坊和Baby JubJub密钥。 以太坊地址用于授权L1交易,Baby JubJub密钥用于授权L2交易。 我们将创建两个钱包。
// load ethereum network provider
hermez.Providers.setProvider('http://localhost:8545')
// initialize transaction pool
hermez.TxPool.initializeTransactionPool()
// load token to deposit information
const tokenToDeposit = 3
const token = await hermez.CoordinatorAPI.getTokens()
const tokenERC20 = token.tokens[tokenToDeposit]
// load first account
const mnemonicIndex1 = 1
const wallet = await hermez.HermezWallet.createWalletFromEtherAccount(mnemonicIndex1)
const hermezWallet = wallet.hermezWallet
const hermezEthereumAddress = wallet.hermezEthereumAddress
// load second account
const mnemonicIndex2 = 2
const wallet2 = await hermez.HermezWallet.createWalletFromEtherAccount(mnemonicIndex2)
const hermezWallet2 = wallet2.hermezWallet
const hermezEthereumAddress2 = wallet2.hermezEthereumAddress
将以太坊代币存入Hermez网络
作为L1交易,同时创建一个Hermez帐户和 depositing 令牌。 在此示例中,我们将100个ERC20_2令牌存入新创建的Hermez帐户。 这些步骤是:
- Select amount to deposit from Ethereum into Hermez using getTokenAmountBigInt()
使用getTokenAmountBigInt()选择从以太坊存入Hermez的金额 - Select the token denomination of the deposit.
选择存款的代币面额。
// set amount to deposit
const amountDeposit = hermez.Utils.getTokenAmountBigInt('100', 18)
// perform deposit account 1
await hermez.Tx.deposit(
amountDeposit,
hermezEthereumAddress,
tokenERC20,
hermezWallet.publicKeyCompressedHex
)
Internally, the deposit funcion calls the Hermez smart contract to add the L1 transaction.
在内部,存款功能调用Hermez智能合约以添加L1交易。
Verify Balance 验证账户余额
token余额可以通过查询Hermez 节点,传递给 hermezEthereumAddress Hermez账号。
// get sender account information
const infoAccountSender = (await hermez.CoordinatorAPI.getAccounts(hermezEthereumAddress, [tokenERC20.id]))
.accounts[0]
// get receiver account information
const infoAccountReceiver = (await hermez.CoordinatorAPI.getAccounts(hermezEthereumAddress2, [tokenERC20.id]))
.accounts[0]
console.log(infoAccountSender)
console.log(infoAccountReceiver)
>>>>>
{
accountIndex: 'hez:20_2:256',
balance: '100000000000000000000',
bjj: 'hez:1-WYg_cDxmLQPTxBDF2BdJYNsmK2KcaL6tcueTqWoQ6v',
hezEthereumAddress: 'hez:0x8401Eb5ff34cc943f096A32EF3d5113FEbE8D4Eb',
itemId: 1,
nonce: 0,
token: {
USD: null,
decimals: 18,
ethereumAddress: '0x2e9f55f7266d8c7e07d359daba0e743e331b7a1a',
ethereumBlockNum: 61,
fiatUpdate: null,
id: 3,
itemId: 4,
name: 'ERC20_2',
symbol: '20_2'
}
}
{
accountIndex: 'hez:20_2:257',
balance: '100000000000000000000',
bjj: 'hez:_ayj1cwk6Kuch4oodEgYYTRWidBywlsV8cYlOyVPiZzl',
hezEthereumAddress: 'hez:0x306469457266CBBe7c0505e8Aad358622235e768',
itemId: 2,
nonce: 0,
token: {
USD: null,
decimals: 18,
ethereumAddress: '0x2e9f55f7266d8c7e07d359daba0e743e331b7a1a',
ethereumBlockNum: 61,
fiatUpdate: null,
id: 3,
itemId: 4,
name: 'ERC20_2',
symbol: '20_2'
}
我们可以看到,字段accountIndex由它持有的令牌符号和一个索引组成。 Hermez帐户只能持有一种令牌。 帐户索引从256开始。重新设置索引0-255供内部使用。
请注意,某些API端点报告的bjj与Hermez Wallet对象中包含的bjj相同,但是它们以不同的格式表示。
或者,可以使用accountIndex过滤帐户查询
const account1ByIdx = await hermez.CoordinatorAPI.getAccount(infoAccountSender.accountIndex)
console.log(account1ByIdx)
const account2ByIdx = await hermez.CoordinatorAPI.getAccount(infoAccountReceiver.accountIndex)
console.log(account2ByIdx)
>>>>>
{
accountIndex: 'hez:20_2:256',
balance: '100000000000000000000',
bjj: 'hez:1-WYg_cDxmLQPTxBDF2BdJYNsmK2KcaL6tcueTqWoQ6v',
hezEthereumAddress: 'hez:0x8401Eb5ff34cc943f096A32EF3d5113FEbE8D4Eb',
itemId: 1,
nonce: 0,
token: {
USD: null,
decimals: 18,
ethereumAddress: '0x2e9f55f7266d8c7e07d359daba0e743e331b7a1a',
ethereumBlockNum: 61,
fiatUpdate: null,
id: 3,
itemId: 4,
name: 'ERC20_2',
symbol: '20_2'
}
}
{
accountIndex: 'hez:20_2:257',
balance: '100000000000000000000',
bjj: 'hez:_ayj1cwk6Kuch4oodEgYYTRWidBywlsV8cYlOyVPiZzl',
hezEthereumAddress: 'hez:0x306469457266CBBe7c0505e8Aad358622235e768',
itemId: 2,
nonce: 0,
token: {
USD: null,
decimals: 18,
ethereumAddress: '0x2e9f55f7266d8c7e07d359daba0e743e331b7a1a',
ethereumBlockNum: 61,
fiatUpdate: null,
id: 3,
itemId: 4,
name: 'ERC20_2',
symbol: '20_2'
}
}
Exit
js 测试exmaple
You have some worked out examples here (https://github.com/hermeznetwork/hermezjs/tree/main/examples)
如下,你需要配合以太坊的地址,以及 HERMEZ节点API地址。
In file Hermezjs/examples/constants.js you need to initialize you need to initialize some constants:
const EXAMPLES_WEB3_URL = ETH Node URL
const EXAMPLES_HERMEZ_API_URL = 'https://api.testnet.hermez.io/'
const EXAMPLES_HERMEZ_ROLLUP_ADDRESS = '0x14a3b6f3328766c7421034e14472f5c14c5ba090'
const EXAMPLES_HERMEZ_WDELAYER_ADDRESS = '0x6ea0abf3ef52d24427043cad3ec26aa4f2c8e8fd'
const EXAMPLES_PRIVATE_KEY1 = ETH PRIVKEY
const EXAMPLES_PRIVATE_KEY2 = ETH PRIVKEY
Note that you need to supply a ethereum node URL and two rinkeby ethereum private keys.
With the configuration above you are accessing Hermez node deployed in Testnet-Rinkeby
After changing configuration in constants.js, you can call script hermezjs/examples/creates-accounts-deposit.js to create two accounts in Hermez
You can monitor the creation of those accounts using the batch explorer (https://explorer.testnet.hermez.io/).
Hermez Batch Explorer
Web site created using create-react-app
Alternatively, instead of using hermezjs to generate your transactions, you can can use the wallet (https://wallet.testnet.hermez.io/)
Hermez Wallet
Web site created using create-react-app
PS: 个人使用测试通过,可以获取tokens js代码, 其中 hermez.Providers.setProvider(ProviderUrl)配置的就是以太坊节点api地址
demo1:获取可以抵押到L2的token列表信息。当前测试只有ETH、HEZ。
console.log("Stage 2: hermez test star!")
//import hermez from './src/index.js'
const hermez = require("@hermeznetwork/hermezjs");
const EXAMPLES_HERMEZ_API_URL = 'https://api.testnet.hermez.io/'
const EXAMPLES_HERMEZ_ROLLUP_ADDRESS = '0x14a3b6f3328766c7421034e14472f5c14c5ba090'
const EXAMPLES_HERMEZ_WDELAYER_ADDRESS = '0x6ea0abf3ef52d24427043cad3ec26aa4f2c8e8fd'
const EXAMPLES_PRIVATE_KEY1 = '451c81d2f92dc77ca53fad01d225becc169f3c3480c7f0fdcc77b6c86f342e03'
const EXAMPLES_PRIVATE_KEY2 = '0a3d30ae8b52b30669c9fc7e46eb2d37bec5027087b96130ec5b8564b64e46a8'
hermez.TxPool.initializeTransactionPool()
var ProviderUrl=`https://rinkeby.infura.io/v3/${process.env.PROJECT_ID}`
hermez.Providers.setProvider(ProviderUrl)
// set environment
hermez.Environment.setEnvironment({
baseApiUrl: EXAMPLES_HERMEZ_API_URL,
contractAddresses: {
[hermez.Constants.ContractNames.Hermez]: EXAMPLES_HERMEZ_ROLLUP_ADDRESS,
[hermez.Constants.ContractNames.WithdrawalDelayer]: EXAMPLES_HERMEZ_WDELAYER_ADDRESS
}
})
async function getTokensFn(){
let result = await hermez.CoordinatorAPI.getTokens();
console.log(result)
return result
}
const token = getTokensFn().then(
(res) => {console.log(res)}, // 成功
(err) => {console.log(err)} // 失败
).catch((err) => console.log('rejected', err));
console.log(token)
console.log("Stage 2: hermez test end!")
// const tokenERC20 = token.tokens[3]
//const tokenERC20 = token.tokens[3]
查询余额相关自测demo
////////////////Stage 2////////////////////////////////////
console.log("Stage 2: hermez test star!")
//import hermez from './src/index.js'
const hermez = require("@hermeznetwork/hermezjs");
const EXAMPLES_HERMEZ_API_URL = 'https://api.testnet.hermez.io/'
const EXAMPLES_HERMEZ_ROLLUP_ADDRESS = '0x14a3b6f3328766c7421034e14472f5c14c5ba090'
const EXAMPLES_HERMEZ_WDELAYER_ADDRESS = '0x6ea0abf3ef52d24427043cad3ec26aa4f2c8e8fd'
const EXAMPLES_PRIVATE_KEY1 = '451c81d2f92dc77ca53fad01d225becc169f3c3480c7f0fdcc77b6c86f342e03'
const EXAMPLES_PRIVATE_KEY2 = '0a3d30ae8b52b30669c9fc7e46eb2d37bec5027087b96130ec5b8564b64e46a8'
hermez.TxPool.initializeTransactionPool()
var ProviderUrl=`https://rinkeby.infura.io/v3/${process.env.PROJECT_ID}`
hermez.Providers.setProvider(ProviderUrl)
// set environment
hermez.Environment.setEnvironment({
baseApiUrl: EXAMPLES_HERMEZ_API_URL,
contractAddresses: {
[hermez.Constants.ContractNames.Hermez]: EXAMPLES_HERMEZ_ROLLUP_ADDRESS,
[hermez.Constants.ContractNames.WithdrawalDelayer]: EXAMPLES_HERMEZ_WDELAYER_ADDRESS
}
})
var tokenERC20
async function getTokensFn(){
let result = await hermez.CoordinatorAPI.getTokens();
console.log(result)
tokenERC20 = result.tokens[0]
getInfoAccountSender()
return result
}
const token = getTokensFn().then(
(res) => {console.log(res)}, // 成功
(err) => {console.log(err)} // 失败
).catch((err) => console.log('rejected', err));
console.log(token)
console.log("Stage 2: hermez test end!")
// const tokenERC20 = token.tokens[3]
//const tokenERC20 = token.tokens[3]
//const tokenERC20 = token.tokens[2]
//获取账户余额
async function getInfoAccountSender(){
const hermezEthereumAddress = "hez:0xd7645Cb776fa43Cc106122a34fBA11C97ffDDf21"
const infoAccountSender = (await hermez.CoordinatorAPI.getAccounts(hermezEthereumAddress, [tokenERC20.id]))
.accounts[0]
console.log(infoAccountSender)
return infoAccountSender
}
本文介绍了HermezJS SDK的基本使用方法,包括安装、创建钱包、以太坊代币存入Hermez网络、验证账户余额等关键步骤。适用于希望了解Hermez Rollup网络交互方式的开发者。

被折叠的 条评论
为什么被折叠?



