chg: Bridge to HermezBridge
MODIFY
src/abstracts/base_web3_client.ts
src/abstracts/base_web3_client.ts
@@ -42,7 +42,7 @@ export abstract class BaseWeb3Client {
});
}
- getAccounts_() {
+ getAccountsUsingRPC_() {
return this.sendRPCRequest({
jsonrpc: '2.0',
method: 'eth_accounts',

MODIFY
src/hermez/erc20.ts
src/hermez/erc20.ts
@@ -34,6 +34,10 @@ export class ERC20 extends HermezToken {
return bridge.contractAddress;
}
+ isEtherToken() {
+ return this.contractParam.address === ADDRESS_ZERO;
+ }
+
/**
* get token balance of user
*
@@ -43,7 +47,7 @@ export class ERC20 extends HermezToken {
* @memberof ERC20
*/
getBalance(userAddress: string, option?: ITransactionOption) {
- if (this.contractParam.address === ADDRESS_ZERO) {
+ if (this.isEtherToken()) {
const client = this.contractParam.isParent ? this.client.parent : this.client.child;
return client.getBalance(userAddress);
} else {
@@ -65,13 +69,13 @@ export class ERC20 extends HermezToken {
* @memberof ERC20
*/
isApprovalNeeded() {
- if (this.contractParam.address === ADDRESS_ZERO) {
+ if (this.isEtherToken()) {
return false;
}
const bridge = this.contractParam.isParent ? this.parentBridge : this.childBridge;
- return bridge.wrappedTokenToTokenInfo(this.contractParam.address)
+ return bridge.getOriginTokenInfo(this.contractParam.address)
.then(tokenInfo => {
return tokenInfo[1] === ADDRESS_ZERO;
});
@@ -154,7 +158,7 @@ export class ERC20 extends HermezToken {
['uint256'],
);
- if (this.contractParam.address === ADDRESS_ZERO) {
+ if (this.isEtherToken()) {
option.value = Converter.toHex(amount);
}
@@ -251,7 +255,7 @@ export class ERC20 extends HermezToken {
['uint256'],
);
- if (this.contractParam.address === ADDRESS_ZERO) {
+ if (this.isEtherToken()) {
option.value = Converter.toHex(amount);
}
@@ -567,7 +571,7 @@ export class ERC20 extends HermezToken {
let contract: BaseContract;
let nonce: string;
- return Promise.all([client.name === 'WEB3' ? client.getAccounts_() : client.getAccounts(), this.getContract(), client.getChainId(), this.getPermit()]).then(result => {
+ return Promise.all([client.name === 'WEB3' ? client.getAccountsUsingRPC_() : client.getAccounts(), this.getContract(), client.getChainId(), this.getPermit()]).then(result => {
account = result[0][0];
contract = result[1];
chainId = result[2];

RENAME
src/hermez/bridge.ts
src/hermez/hermez_bridge.ts
@@ -2,7 +2,7 @@ import { BaseToken, Web3SideChainClient, Converter, promiseResolve } from "../ut
import { IHermezClientConfig, ITransactionOption } from "../interfaces";
import { TYPE_AMOUNT } from "../types";
-export class Bridge extends BaseToken<IHermezClientConfig> {
+export class HermezBridge extends BaseToken<IHermezClientConfig> {
networkID_: number;
@@ -21,6 +21,19 @@ export class Bridge extends BaseToken<IHermezClientConfig> {
});
}
+ /**
+ * bridge function to be called on that network from where token is to be transferred to a different network
+ *
+ * @param {string} token Token address
+ * @param {number} destinationNetwork Network at which tokens will be bridged
+ * @param {string} destinationAddress Address to which tokens will be bridged
+ * @param {TYPE_AMOUNT} amountamount amount of tokens
+ * @param {string} [permitData] Permit data to avoid approve call
+ * @param {ITransactionOption} [option]
+ *
+ * @returns
+ * @memberof HermezBridge
+ */
bridgeAsset(
token: string,
destinationNetwork: number,
@@ -41,6 +54,23 @@ export class Bridge extends BaseToken<IHermezClientConfig> {
});
}
+ /**
+ * Claim function to be called on the destination network
+ *
+ * @param {string[]} smtProof Merkle Proof
+ * @param {number} index Deposit Index
+ * @param {string} mainnetExitRoot Mainnet Exit Root
+ * @param {string} rollupExitRoot RollUP Exit Root
+ * @param {number} originNetwork Network at which token was initially deployed
+ * @param {string} originTokenAddress Address of token at network where token was initially deployed
+ * @param {string} destinationAddress Address to which tokens will be bridged
+ * @param {TYPE_AMOUNT} amount amount of tokens
+ * @param {string} [metadata] Metadata of token
+ * @param {ITransactionOption} [option]
+ *
+ * @returns
+ * @memberof HermezBridge
+ */
claimAsset(
smtProof: string[],
index: number,
@@ -74,7 +104,15 @@ export class Bridge extends BaseToken<IHermezClientConfig> {
});
}
- getTokenWrappedAddress(
+ /**
+ * get the address of token which is created by the bridge contract on the non origin chain
+ *
+ * @param {number} originNetwork Network at which the token was initially deployed
+ * @param {string} originTokenAddress Address at the network where token was initially deployed
+ * @returns
+ * @memberof HermezBridge
+ */
+ getMappedTokenInfo(
originNetwork: number,
originTokenAddress: string
) {
@@ -85,6 +123,13 @@ export class Bridge extends BaseToken<IHermezClientConfig> {
});
}
+ /**
+ * Tells if claim has already happed or not based on the deposit index
+ *
+ * @param {number} index
+ * @returns
+ * @memberof HermezBridge
+ */
claimNullifier(
index: number,
) {
@@ -95,7 +140,15 @@ export class Bridge extends BaseToken<IHermezClientConfig> {
});
}
- precalculatedWrapperAddress(
+ /**
+ * Even if the wrapped contract is not deployed on the destination chain, it will tell us the address which is going to be.
+ *
+ * @param {number} originNetwork Network at which the token was initially deployed
+ * @param {string} originTokenAddress Address at the network where token was initially deployed
+ * @returns
+ * @memberof HermezBridge
+ */
+ precalculatedMappedTokenInfo(
originNetwork: number,
originTokenAddress: string
) {
@@ -106,7 +159,14 @@ export class Bridge extends BaseToken<IHermezClientConfig> {
});
}
- wrappedTokenToTokenInfo(wrappedToken: string) {
+ /**
+ * get the address and network of the wrapped token where it was emerged initially
+ *
+ * @param {number} wrappedToken
+ * @returns
+ * @memberof HermezBridge
+ */
+ getOriginTokenInfo(wrappedToken: string) {
return this.method(
"wrappedTokenToTokenInfo", wrappedToken
).then(method => {
@@ -114,6 +174,12 @@ export class Bridge extends BaseToken<IHermezClientConfig> {
});
}
+ /**
+ * get the network ID for chain in which the bridge contract is deployed
+ *
+ * @returns
+ * @memberof HermezBridge
+ */
networkID() {
if (this.networkID_) {
return promiseResolve<number>(this.networkID_ as any);

MODIFY
src/hermez/index.ts
src/hermez/index.ts
import { ERC20 } from "./erc20";
-import { Bridge } from "./bridge";
+import { HermezBridge } from "./hermez_bridge";
import { BridgeUtil } from "./bridge_util";
import { HermezBridgeClient, setHermezProofApi } from "../utils";
import { IHermezClientConfig, IHermezContracts } from "../interfaces";
import { config as urlConfig } from "../config";
-export * from "./bridge";
+export * from "./hermez_bridge";
export * from "./bridge_util";
export class HermezClient extends HermezBridgeClient<IHermezClientConfig> {
@@ -24,13 +24,13 @@ export class HermezClient extends HermezBridgeClient<IHermezClientConfig> {
config
);
- this.rootChainBridge = new Bridge(
+ this.rootChainBridge = new HermezBridge(
this.client,
config.parentBridge,
true
);
- this.childChainBridge = new Bridge(
+ this.childChainBridge = new HermezBridge(
this.client,
config.childBridge,
false

MODIFY
src/interfaces/hermez_contracts.ts
src/interfaces/hermez_contracts.ts
-import { Bridge, BridgeUtil } from "../hermez";
+import { HermezBridge, BridgeUtil } from "../hermez";
export interface IHermezContracts {
- parentBridge: Bridge;
- childBridge: Bridge;
+ parentBridge: HermezBridge;
+ childBridge: HermezBridge;
bridgeUtil: BridgeUtil;
}

MODIFY
src/utils/hermez_bridge_client.ts
src/utils/hermez_bridge_client.ts
import { Web3SideChainClient } from "../utils";
-import { BridgeUtil, Bridge } from "../hermez";
+import { BridgeUtil, HermezBridge } from "../hermez";
import { service } from "../services";
export class HermezBridgeClient<T> {
client: Web3SideChainClient<T> = new Web3SideChainClient();
bridgeUtil: BridgeUtil;
- rootChainBridge: Bridge;
- childChainBridge: Bridge;
+ rootChainBridge: HermezBridge;
+ childChainBridge: HermezBridge;
/**
* check whether a txHash is synced with child chain

MODIFY
test/debug.js
test/debug.js
@@ -197,12 +197,12 @@ const executeHermez = async () => {
// var result = await client.isExited('0xc3567e692b7f2ad1f3da019c97c6b01483330f5b2cb022ae0d8fdccd4d1c0d60');
// return console.log('result', result);
- // // getTokenWrappedAddress
- // var result = await client.childChainBridge.getTokenWrappedAddress("0", goerliERC20);
+ // // getMappedTokenInfo
+ // var result = await client.childChainBridge.getMappedTokenInfo("0", goerliERC20);
// return console.log('result', result);
- // // wrappedTokenToTokenInfo
- // var result = await client.childChainBridge.wrappedTokenToTokenInfo(litchiERC20);
+ // // getOriginTokenInfo
+ // var result = await client.childChainBridge.getOriginTokenInfo(litchiERC20);
// return console.log('result', result);
// // getBalance on goerli

MODIFY
test/ether.js
test/ether.js
@@ -114,12 +114,12 @@ const executeHermez = async () => {
// var result = await client.isExited('0xc3567e692b7f2ad1f3da019c97c6b01483330f5b2cb022ae0d8fdccd4d1c0d60');
// return console.log('result', result);
- // // getTokenWrappedAddress
- // var result = await client.childChainBridge.getTokenWrappedAddress("0", goerliERC20);
+ // // getMappedTokenInfo
+ // var result = await client.childChainBridge.getMappedTokenInfo("0", goerliERC20);
// return console.log('result', result);
- // // wrappedTokenToTokenInfo
- // var result = await client.childChainBridge.wrappedTokenToTokenInfo(litchiERC20);
+ // // getOriginTokenInfo
+ // var result = await client.childChainBridge.getOriginTokenInfo(litchiERC20);
// return console.log('result', result);
// // getBalance on goerli

chg: Bridge to HermezBridge