From 99209d8ff59ef5bfb57e64aff995e31f2a37dfbe Mon Sep 17 00:00:00 2001 From: Zhang Junyu Date: Sat, 22 Jan 2022 17:33:57 +0800 Subject: [PATCH 1/7] use ethers for loginL1Account --- src/lib/l1/account.ts | 11 ++++++----- src/lib/type.ts | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/lib/l1/account.ts b/src/lib/l1/account.ts index e02e8d1..e077942 100644 --- a/src/lib/l1/account.ts +++ b/src/lib/l1/account.ts @@ -1,7 +1,8 @@ -import BN from "bn.js"; -import { L1AccountInfo, SubstrateAccountInfo, BridgeMetadata } from "../type"; import { L1Client, withL1Client } from "solidity/clients/client"; -import { DelphinusWeb3, withBrowerWeb3 } from "web3subscriber/src/client"; +import { + BlockChainClient, + withBlockchainClient, +} from "web3subscriber/src/client"; import { L1ClientRole } from "delphinus-deployment/src/types"; import { getConfigByChainId, @@ -9,8 +10,8 @@ import { } from "delphinus-deployment/src/config"; export async function loginL1Account() { - return await withBrowerWeb3(async (web3: DelphinusWeb3) => { - let i = await web3.getAccountInfo(); + return await withBlockchainClient(true, async (client: BlockChainClient) => { + let i = await client.getAccountInfo(); return i; }); } diff --git a/src/lib/type.ts b/src/lib/type.ts index 5b7c770..4bbdf4c 100644 --- a/src/lib/type.ts +++ b/src/lib/type.ts @@ -12,7 +12,7 @@ export interface L1AccountInfo { address: string; chainId: string; //chainName: string; - web3: any; + // web3: any; } /* From 1c0170fcbfa095d4155d90aef6f5f8705c568bad Mon Sep 17 00:00:00 2001 From: Zhang Junyu Date: Sat, 22 Jan 2022 19:32:32 +0800 Subject: [PATCH 2/7] refine code and use ethers --- src/lib/l1/account.ts | 38 ++++++++------------------------------ 1 file changed, 8 insertions(+), 30 deletions(-) diff --git a/src/lib/l1/account.ts b/src/lib/l1/account.ts index e077942..f9e3947 100644 --- a/src/lib/l1/account.ts +++ b/src/lib/l1/account.ts @@ -1,45 +1,23 @@ -import { L1Client, withL1Client } from "solidity/clients/client"; import { BlockChainClient, withBlockchainClient, } from "web3subscriber/src/client"; -import { L1ClientRole } from "delphinus-deployment/src/types"; -import { - getConfigByChainId, - WalletSnap, -} from "delphinus-deployment/src/config"; export async function loginL1Account() { - return await withBlockchainClient(true, async (client: BlockChainClient) => { + return await withBlockchainClient(async (client: BlockChainClient) => { let i = await client.getAccountInfo(); return i; }); } export async function deriveL2Account(l1Account: string) { - let sign: { [key: string]: string } = await withL1Client( - await getConfigByChainId(L1ClientRole.Wallet, WalletSnap), - true, - async (l1client: L1Client) => - new Promise((resolve, reject) => - (l1client.web3.web3Instance.currentProvider as any).sendAsync( - { - method: "personal_sign", - params: [ - "Sign this message to derive Delphinus L2 account, do not expose the signature to other.", - l1Account, - ], - from: l1Account, - }, - function (err: any, result: any) { - if (err) { - reject(err); - } - resolve(result); - } - ) - ) + let sign: { [key: string]: string } = await withBlockchainClient( + async (l1client: BlockChainClient) => { + return l1client.send("personal_sign", [ + "Sign this message to derive Delphinus L2 account, do not expose the signature to other.", + l1Account, + ]); + } ); - return sign.result; } From 0a866f2b7e8567e35ed2989778104ef39ceb1f14 Mon Sep 17 00:00:00 2001 From: Zhang Junyu Date: Sat, 22 Jan 2022 20:07:58 +0800 Subject: [PATCH 3/7] format query.ts --- src/lib/l1/query.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/lib/l1/query.ts b/src/lib/l1/query.ts index 47b4535..e0af7a8 100644 --- a/src/lib/l1/query.ts +++ b/src/lib/l1/query.ts @@ -1,7 +1,6 @@ import BN from "bn.js"; -import { PoolInfo, L1AccountInfo, SubstrateAccountInfo, BridgeMetadata } from "../type"; +import { L1AccountInfo, BridgeMetadata } from "../type"; import { L1Client, withL1Client } from "solidity/clients/client"; -import { DelphinusWeb3, withBrowerWeb3 } from "web3subscriber/src/client"; import { getConfigByChainId, WalletSnap, @@ -56,13 +55,13 @@ export async function prepareMetaData(pool_list: Array>) { let poolidx = info[0]; console.log("preparing:", poolidx, info[1], info[2]); try { - let t1 = await bridge.getTokenInfo(info[1]); - let t2 = await bridge.getTokenInfo(info[2]); - return { - id: poolidx, - tokens: [t1, t2], - }; - } catch(e) { + let t1 = await bridge.getTokenInfo(info[1]); + let t2 = await bridge.getTokenInfo(info[2]); + return { + id: poolidx, + tokens: [t1, t2], + }; + } catch (e) { console.log(e); throw e; } From 2074a50974a16a36ff7263ea2efa01acba3cd8b4 Mon Sep 17 00:00:00 2001 From: Zhang Junyu Date: Sun, 23 Jan 2022 14:59:11 +0800 Subject: [PATCH 4/7] query meta with ethers --- src/lib/l1/query.ts | 70 ++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/src/lib/l1/query.ts b/src/lib/l1/query.ts index e0af7a8..45c6452 100644 --- a/src/lib/l1/query.ts +++ b/src/lib/l1/query.ts @@ -1,11 +1,20 @@ import BN from "bn.js"; import { L1AccountInfo, BridgeMetadata } from "../type"; -import { L1Client, withL1Client } from "solidity/clients/client"; +import { + L1Client, + withL1Client, + withL1Connection, +} from "solidity/clients/client"; import { getConfigByChainId, WalletSnap, } from "delphinus-deployment/src/config"; import { L1ClientRole } from "delphinus-deployment/src/types"; +import { BlockChainClient } from "web3subscriber/src/client"; +import { getTokenContractConnection } from "solidity/clients/contracts/token"; +import { getBridgeContractConnection } from "solidity/clients/contracts/bridge"; +import { decodeL1address } from "web3subscriber/src/addresses"; +import { Chains, Tokens } from "solidity/clients/contracts/tokenlist"; export function getTokenIndex( metadata: BridgeMetadata, @@ -17,6 +26,12 @@ export function getTokenIndex( return token!.index; } +function hexcmp(x: string, y: string) { + const xx = new BN(x, "hex"); + const yy = new BN(y, "hex"); + return xx.eq(yy); +} + export async function queryCurrentL1Account(chainId: string) { return await withL1Client( await getConfigByChainId(L1ClientRole.Wallet, chainId), @@ -32,34 +47,43 @@ export async function queryTokenL1Balance( tokenAddress: string, l1Account: L1AccountInfo ) { - let config = await getConfigByChainId(L1ClientRole.Wallet, chainId); - return withL1Client(config, false, async (l1client: L1Client) => { - let token = l1client.getTokenContract( - new BN(tokenAddress, 16).toString(16, 20), - l1Account.address - ); - console.log("nid is", await l1client.web3.web3Instance.eth.net.getId()); - console.log("token is", token); - let balance = await token.balanceOf(l1Account.address); - return balance; - }); + return await withL1Connection(async (l1Client: BlockChainClient) => { + let token = await getTokenContractConnection(l1Client, tokenAddress); + let balance: BN = await token.balanceOf(l1Account.address); + return balance.toString(10); + }, await getConfigByChainId(L1ClientRole.Wallet, chainId)); } export async function prepareMetaData(pool_list: Array>) { - let config = await getConfigByChainId(L1ClientRole.Wallet, WalletSnap); - return await withL1Client(config, false, async (l1client: L1Client) => { - let bridge = l1client.getBridgeContract(); - console.log("got bridge"); - let pools = await Promise.all( + const config = await getConfigByChainId(L1ClientRole.Wallet, WalletSnap); + const wrapTokenInfo = (idx: number, token: any) => { + const uid: BN = token.token_uid; + console.log(uid); + const [cid, addr] = decodeL1address(uid.toString()); + return { + tokenAddress: addr, + tokenName: + Tokens.find((x: any) => hexcmp(x.address, addr) && x.chainId == cid) + ?.name || "unknown", + chainName: Chains[cid], + chainId: cid, + index: idx, + }; + }; + + return await withL1Connection(async (l1Client: BlockChainClient) => { + const bridge = await getBridgeContractConnection(l1Client); + const pools = await Promise.all( pool_list.map(async (info) => { - let poolidx = info[0]; + const poolidx = info[0]; console.log("preparing:", poolidx, info[1], info[2]); try { - let t1 = await bridge.getTokenInfo(info[1]); - let t2 = await bridge.getTokenInfo(info[2]); + const tokens = await bridge.allTokens(); + const t1 = tokens[info[1]]; + const t2 = tokens[info[2]]; return { id: poolidx, - tokens: [t1, t2], + tokens: [wrapTokenInfo(info[1], t1), wrapTokenInfo(info[2], t2)], }; } catch (e) { console.log(e); @@ -68,9 +92,9 @@ export async function prepareMetaData(pool_list: Array>) { }) ); return { - chainInfo: (await bridge.getMetaData()).chainInfo, + chainInfo: await bridge.chainInfo(), poolInfo: pools, snap: WalletSnap, }; - }); + }, config); } From cef8c48175eba063e15610ba222cfca56e59ec41 Mon Sep 17 00:00:00 2001 From: Zhang Junyu Date: Sun, 23 Jan 2022 15:17:55 +0800 Subject: [PATCH 5/7] replace l1 client with ethers version --- src/lib/l1/account.ts | 6 ++++-- src/lib/l1/query.ts | 18 +++++------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/lib/l1/account.ts b/src/lib/l1/account.ts index f9e3947..a5410ca 100644 --- a/src/lib/l1/account.ts +++ b/src/lib/l1/account.ts @@ -5,8 +5,10 @@ import { export async function loginL1Account() { return await withBlockchainClient(async (client: BlockChainClient) => { - let i = await client.getAccountInfo(); - return i; + return { + address: await client.getAccountInfo(), + chainId: await (await client.getChainID()).toString(), + }; }); } diff --git a/src/lib/l1/query.ts b/src/lib/l1/query.ts index 45c6452..d1a6b4e 100644 --- a/src/lib/l1/query.ts +++ b/src/lib/l1/query.ts @@ -1,10 +1,6 @@ import BN from "bn.js"; import { L1AccountInfo, BridgeMetadata } from "../type"; -import { - L1Client, - withL1Client, - withL1Connection, -} from "solidity/clients/client"; +import { withL1Connection } from "solidity/clients/client"; import { getConfigByChainId, WalletSnap, @@ -32,14 +28,10 @@ function hexcmp(x: string, y: string) { return xx.eq(yy); } -export async function queryCurrentL1Account(chainId: string) { - return await withL1Client( - await getConfigByChainId(L1ClientRole.Wallet, chainId), - true, - async (l1client: L1Client) => { - return l1client.encodeL1Address(l1client.getDefaultAccount()); - } - ); +export async function queryCurrentL1Account(_chainId: string) { + return await withL1Connection(async (l1Client: BlockChainClient) => { + return l1Client.encodeL1Address(await l1Client.getAccountInfo()); + }); } export async function queryTokenL1Balance( From 4e554aa4f674c1f7999f8b8b364dd7c285c878f6 Mon Sep 17 00:00:00 2001 From: Zhang Junyu Date: Sun, 23 Jan 2022 19:52:29 +0800 Subject: [PATCH 6/7] support ethers bridge contract --- src/lib/l1/tx.ts | 90 ++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 48 deletions(-) diff --git a/src/lib/l1/tx.ts b/src/lib/l1/tx.ts index a7c7ec9..754b060 100644 --- a/src/lib/l1/tx.ts +++ b/src/lib/l1/tx.ts @@ -1,8 +1,8 @@ -import BN from "bn.js"; -import { SubstrateAccountInfo} from "../type"; -import { L1Client, withL1Client } from "solidity/clients/client"; -import { getConfigByChainId } from "delphinus-deployment/src/config"; -import { L1ClientRole } from "delphinus-deployment/src/types"; +import { SubstrateAccountInfo } from "../type"; +import { withL1Connection } from "solidity/clients/client"; +import { BlockChainClient } from "web3subscriber/src/client"; +import { getTokenContractConnection } from "solidity/clients/contracts/token"; +import { getBridgeContractConnection } from "solidity/clients/contracts/bridge"; const ss58 = require("substrate-ss58"); @@ -16,24 +16,21 @@ export async function deposit( querying: (m: string) => Promise ) { const accountAddress = l2Account.address; + // const config = await getConfigByChainId(L1ClientRole.Wallet, chainId); console.log("call deposit", accountAddress, chainId, tokenAddress, amount); - await withL1Client( - await getConfigByChainId(L1ClientRole.Wallet, chainId), - true, - async (l1client: L1Client) => { - try { - let token_address = "0x" + tokenAddress; - let token_id = ss58.addressToAddressId(accountAddress); - let tokenContract = l1client.getTokenContract(token_address); - let BridgeContract = l1client.getBridgeContract(); - let r = BridgeContract.deposit( - tokenContract, - parseInt(amount), - token_id - ); - r.when("snapshot", "Approve", () => - progress("approve", "Wait confirm ...", "", 10) - ) + await withL1Connection(async (l1client: BlockChainClient) => { + try { + let token_address = "0x" + tokenAddress; + let token_id = ss58.addressToAddressId(accountAddress); + let tokenContract = await getTokenContractConnection( + l1client, + token_address + ); + let BridgeContract = await getBridgeContractConnection(l1client); + let r = BridgeContract.deposit(tokenContract, parseInt(amount), token_id); + r.when("snapshot", "Approve", () => + progress("approve", "Wait confirm ...", "", 10) + ) .when("Approve", "transactionHash", (tx: string) => progress("approve", "Transaction Sent", tx, 20) ) @@ -49,31 +46,28 @@ export async function deposit( .when("Deposit", "receipt", (tx: any) => progress("deposit", "Done", tx.blockHash, 70) ); - let tx = await r; - console.log(tx); - const p = async () => { - let tx_status = await querying(tx.transactionHash); - //FIXME: tx_status:Codec should be parsed to number - console.log("tx_status", tx_status); - if (tx_status === "0x00") { - progress("finalize", "Waiting L2", "", 80); - await setTimeout(() => {}, 1000); - await p(); - } else if (tx_status === "0x01") { - //FIXME: we need to put the receipt status into a list for further querying - progress("finalize", "Waiting L2", "", 100); - return; - } else if (tx_status === "0x02") { - progress("finalize", "Done", "", 100); - return; - } else throw "Unexpected TxStatus"; - }; - await p(); - } catch (e: any) { - error(e.message); - } + let tx = await r; + console.log(tx); + const p = async () => { + let tx_status = await querying(tx.transactionHash); + //FIXME: tx_status:Codec should be parsed to number + console.log("tx_status", tx_status); + if (tx_status === "0x00") { + progress("finalize", "Waiting L2", "", 80); + await setTimeout(() => {}, 1000); + await p(); + } else if (tx_status === "0x01") { + //FIXME: we need to put the receipt status into a list for further querying + progress("finalize", "Waiting L2", "", 100); + return; + } else if (tx_status === "0x02") { + progress("finalize", "Done", "", 100); + return; + } else throw "Unexpected TxStatus"; + }; + await p(); + } catch (e: any) { + error(e.message); } - ); + }); } - - From 3aebd9f8edb37eb0d1f115216c8fc53b19338146 Mon Sep 17 00:00:00 2001 From: Zhang Junyu Date: Tue, 1 Feb 2022 13:00:46 +0800 Subject: [PATCH 7/7] adapt withL1Connection interface --- src/lib/l1/account.ts | 5 +++-- src/lib/l1/query.ts | 8 ++++---- src/lib/l1/tx.ts | 4 +++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/lib/l1/account.ts b/src/lib/l1/account.ts index a5410ca..242bc88 100644 --- a/src/lib/l1/account.ts +++ b/src/lib/l1/account.ts @@ -9,7 +9,7 @@ export async function loginL1Account() { address: await client.getAccountInfo(), chainId: await (await client.getChainID()).toString(), }; - }); + }, true); } export async function deriveL2Account(l1Account: string) { @@ -19,7 +19,8 @@ export async function deriveL2Account(l1Account: string) { "Sign this message to derive Delphinus L2 account, do not expose the signature to other.", l1Account, ]); - } + }, + true ); return sign.result; } diff --git a/src/lib/l1/query.ts b/src/lib/l1/query.ts index d1a6b4e..caae898 100644 --- a/src/lib/l1/query.ts +++ b/src/lib/l1/query.ts @@ -28,10 +28,10 @@ function hexcmp(x: string, y: string) { return xx.eq(yy); } -export async function queryCurrentL1Account(_chainId: string) { +export async function queryCurrentL1Account(chainId: string) { return await withL1Connection(async (l1Client: BlockChainClient) => { return l1Client.encodeL1Address(await l1Client.getAccountInfo()); - }); + }, true, await getConfigByChainId(L1ClientRole.Wallet, chainId)); } export async function queryTokenL1Balance( @@ -43,7 +43,7 @@ export async function queryTokenL1Balance( let token = await getTokenContractConnection(l1Client, tokenAddress); let balance: BN = await token.balanceOf(l1Account.address); return balance.toString(10); - }, await getConfigByChainId(L1ClientRole.Wallet, chainId)); + }, false, await getConfigByChainId(L1ClientRole.Wallet, chainId)); } export async function prepareMetaData(pool_list: Array>) { @@ -88,5 +88,5 @@ export async function prepareMetaData(pool_list: Array>) { poolInfo: pools, snap: WalletSnap, }; - }, config); + }, false, config); } diff --git a/src/lib/l1/tx.ts b/src/lib/l1/tx.ts index 754b060..cbdddad 100644 --- a/src/lib/l1/tx.ts +++ b/src/lib/l1/tx.ts @@ -3,6 +3,8 @@ import { withL1Connection } from "solidity/clients/client"; import { BlockChainClient } from "web3subscriber/src/client"; import { getTokenContractConnection } from "solidity/clients/contracts/token"; import { getBridgeContractConnection } from "solidity/clients/contracts/bridge"; +import { getConfigByChainId } from "delphinus-deployment/src/config"; +import { L1ClientRole } from "delphinus-deployment/src/types"; const ss58 = require("substrate-ss58"); @@ -69,5 +71,5 @@ export async function deposit( } catch (e: any) { error(e.message); } - }); + }, true, await getConfigByChainId(L1ClientRole.Wallet, chainId)); }