Skip to content

Commit

Permalink
refactor: add adapter dep. to erc20
Browse files Browse the repository at this point in the history
  • Loading branch information
saranonearth committed Sep 6, 2023
1 parent dc91e24 commit 523e04c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 44 deletions.
3 changes: 2 additions & 1 deletion src/enums/error_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export enum ERROR_TYPE {
NullSpenderAddress = "null_spender_address",
AllowedOnNonNativeTokens = "allowed_on_non_native_token",
AllowedOnMainnet = "allowed_on_mainnet",
}
BridgeAdapterNotFound = "bridge_adapter_address_not_passed"
}
3 changes: 2 additions & 1 deletion src/interfaces/contract_init_param.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface IContractInitParam {
address: string;
isParent: boolean;
bridgeAdapterAddress?: string;
/**
* used to get the predicate
*
Expand All @@ -9,4 +10,4 @@ export interface IContractInitParam {
*/
name: string;
bridgeType?: string;
}
}
2 changes: 0 additions & 2 deletions src/interfaces/zkevm_client_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ import { IBaseClientConfig } from "./base_client_config";
export interface IZkEvmClientConfig extends IBaseClientConfig {
parentBridge?: string;
childBridge?: string;
parentBridgeAdapter?: string;
childBridgeAdapter?: string;
zkEVMWrapper?: string;
}
2 changes: 0 additions & 2 deletions src/interfaces/zkevm_contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { ZkEVMBridgeAdapter } from '../zkevm/zkevm_custom_bridge';
export interface IZkEvmContracts {
parentBridge: ZkEvmBridge;
childBridge: ZkEvmBridge;
rootBridgeAdapter: ZkEVMBridgeAdapter;
childBridgeAdapter: ZkEVMBridgeAdapter;
bridgeUtil: BridgeUtil;
zkEVMWrapper: ZkEVMWrapper;
}
20 changes: 16 additions & 4 deletions src/zkevm/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,33 @@ import { Converter, Web3SideChainClient, promiseAny } from "../utils";
import { ZkEvmToken } from "./zkevm_token";
import { TYPE_AMOUNT } from "../types";
import { BaseContractMethod } from "../abstracts";
import { MAX_AMOUNT, ADDRESS_ZERO, DAI_PERMIT_TYPEHASH, EIP_2612_PERMIT_TYPEHASH, UNISWAP_DOMAIN_TYPEHASH, EIP_2612_DOMAIN_TYPEHASH, Permit, BaseContract, BaseWeb3Client } from "..";
import { MAX_AMOUNT, ADDRESS_ZERO, DAI_PERMIT_TYPEHASH, EIP_2612_PERMIT_TYPEHASH, UNISWAP_DOMAIN_TYPEHASH, EIP_2612_DOMAIN_TYPEHASH, Permit, BaseContract, BaseWeb3Client, ERROR_TYPE } from '..';
import { IAllowanceTransactionOption, IApproveTransactionOption, IBridgeTransactionOption, IZkEvmClientConfig, IZkEvmContracts } from "../interfaces";
import { ZkEVMBridgeAdapter } from './zkevm_custom_bridge';

export class ERC20 extends ZkEvmToken {

private bridgeAdapter: ZkEVMBridgeAdapter;
constructor(
tokenAddress: string,
isParent: boolean,
bridgeAdapterAddress,
client: Web3SideChainClient<IZkEvmClientConfig>,
getContracts: () => IZkEvmContracts
) {
super({
isParent,
address: tokenAddress,
bridgeAdapterAddress,
name: 'ERC20',
bridgeType: 'zkevm'
}, client, getContracts);
if(bridgeAdapterAddress) {
this.bridgeAdapter = new ZkEVMBridgeAdapter(
this.client,
bridgeAdapterAddress,
isParent
);
}
}

/**
Expand Down Expand Up @@ -296,9 +306,10 @@ export class ERC20 extends ZkEvmToken {
depositCustomERC20(amount: TYPE_AMOUNT, userAddress: string, forceUpdateGlobalExitRoot = true) {
// should be allowed to be used only in root chain
this.checkForRoot("depositCustomERC20");
this.checkAdapterPresent("depositCustomERC20");
// should not be allowed to use for native asset
this.checkForNonNative("depositCustomERC20");
return this.rootBridgeAdapter.bridgeToken(userAddress, amount, forceUpdateGlobalExitRoot);
return this.bridgeAdapter.bridgeToken(userAddress, amount, forceUpdateGlobalExitRoot);
}

/**
Expand Down Expand Up @@ -410,9 +421,10 @@ export class ERC20 extends ZkEvmToken {
withdrawCustomERC20(amount: TYPE_AMOUNT, userAddress: string, forceUpdateGlobalExitRoot = true) {
// should be allowed to be used only in root chain
this.checkForChild("withdrawCustomERC20");
this.checkAdapterPresent("depositCustomERC20");
// should not be allowed to use for native asset
this.checkForNonNative("withdrawCustomERC20");
return this.childBridgeAdapter.bridgeToken(userAddress, amount, forceUpdateGlobalExitRoot);
return this.bridgeAdapter.bridgeToken(userAddress, amount, forceUpdateGlobalExitRoot);
}

/**
Expand Down
22 changes: 5 additions & 17 deletions src/zkevm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class ZkEvmClient extends ZkEvmBridgeClient {
const client = this.client;

return client.init(config).then(_ => {
const mainZkEvmContracts = client.mainZkEvmContracts;
const mainZkEvmContracts = client.mainZkEvmContracts;
const zkEvmContracts = client.zkEvmContracts;
client.config = config = Object.assign(
{
Expand Down Expand Up @@ -48,18 +48,6 @@ export class ZkEvmClient extends ZkEvmBridgeClient {
config.zkEVMWrapper
);

this.rootBridgeAdapter = new ZkEVMBridgeAdapter(
this.client,
config.parentBridgeAdapter,
true
);

this.childBridgeAdapter = new ZkEVMBridgeAdapter(
this.client,
config.parentBridgeAdapter,
false
);

this.bridgeUtil = new BridgeUtil(
this.client
);
Expand All @@ -81,14 +69,16 @@ export class ZkEvmClient extends ZkEvmBridgeClient {
*
* @param {string} tokenAddress
* @param {boolean} isParent
*
*
* @param bridgeAdapterAddress Needed if a custom erc20 token is being bridged
* @returns
* @memberof ERC20
*/
erc20(tokenAddress: string, isParent?: boolean) {
erc20(tokenAddress: string, isParent?: boolean, bridgeAdapterAddress?: string) {
return new ERC20(
tokenAddress,
isParent,
bridgeAdapterAddress,
this.client,
this.getContracts_.bind(this)
);
Expand All @@ -98,8 +88,6 @@ export class ZkEvmClient extends ZkEvmBridgeClient {
return {
parentBridge: this.rootChainBridge,
childBridge: this.childChainBridge,
rootBridgeAdapter: this.rootBridgeAdapter,
childBridgeAdapter: this.childBridgeAdapter,
bridgeUtil: this.bridgeUtil,
zkEVMWrapper: this.zkEVMWrapper
} as IZkEvmContracts;
Expand Down
8 changes: 0 additions & 8 deletions src/zkevm/zkevm_token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ export class ZkEvmToken extends BaseToken<IZkEvmClientConfig> {
return this.getZkEvmContracts().childBridge;
}

protected get rootBridgeAdapter() {
return this.getZkEvmContracts().rootBridgeAdapter;
}

protected get childBridgeAdapter() {
return this.getZkEvmContracts().childBridgeAdapter;
}

protected get bridgeUtil() {
return this.getZkEvmContracts().bridgeUtil;
}
Expand Down
18 changes: 9 additions & 9 deletions test/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ const executeZkEvm = async () => {
log: true,
parentBridge: "0xf6beeebb578e214ca9e23b0e9683454ff88ed2a7",
childBridge: "0xf6beeebb578e214ca9e23b0e9683454ff88ed2a7",
parentBridgeAdapter: "0x5eB6485573C2Ea289554A044e1D34b41958c0842",
childBridgeAdapter: "0x6b0393fD45B1a95EfB1bcd93536DaB44417119C3",
zkEVMWrapper: "0xDb5328c50B166545d1e830BB509944d4B98CBb23",
network: 'testnet',
version: 'blueberry',
Expand All @@ -191,16 +189,18 @@ const executeZkEvm = async () => {

/**
* Custom ERC20 calls
* parentBridgeAdapter: "0x5eB6485573C2Ea289554A044e1D34b41958c0842",
* childBridgeAdapter: "0x6b0393fD45B1a95EfB1bcd93536DaB44417119C3",
*/

const erc20t = client.erc20(goerliERC20, false);
// const tx = await erc20t.depositCustomERC20("1000000000000000000", "0x385134a9c83E02ea204007d46550174C43b61332", true );
// const txHash = await tx.getTransactionHash();
// console.log("Transaction Hash", txHash);
const erc20t = client.erc20(goerliERC20, true, "0x5eB6485573C2Ea289554A044e1D34b41958c0842");
const tx = await erc20t.depositCustomERC20("1000000000000000000", "0x385134a9c83E02ea204007d46550174C43b61332", true );
const txHash = await tx.getTransactionHash();
console.log("Transaction Hash", txHash);

const ctx = await erc20t.customERC20DepositClaim("0x696941f6147702d9850ff9798f56543cb33ebc608d3d6c5987288b7c2fe3d868");
const ctxHash = await ctx.getTransactionHash();
console.log("claimed txHash", ctxHash);
// const ctx = await erc20t.customERC20DepositClaim("0x294cee4839a3d6da3e4ff92f79ee7d1ec603fb1fc1f7d4efa277339268d579cb");
// const ctxHash = await ctx.getTransactionHash();
// console.log("claimed txHash", ctxHash);
//
// const tx = await erc20t.bridgeToken("0x385134a9c83E02ea204007d46550174C43b61332", "10", false);
// const txHash = await tx.getTransactionHash();
Expand Down

0 comments on commit 523e04c

Please sign in to comment.