Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 17 additions & 35 deletions src/lib/l1/account.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,26 @@
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 { L1ClientRole } from "delphinus-deployment/src/types";
import {
getConfigByChainId,
WalletSnap,
} from "delphinus-deployment/src/config";
BlockChainClient,
withBlockchainClient,
} from "web3subscriber/src/client";

export async function loginL1Account() {
return await withBrowerWeb3(async (web3: DelphinusWeb3) => {
let i = await web3.getAccountInfo();
return i;
});
return await withBlockchainClient(async (client: BlockChainClient) => {
return {
address: await client.getAccountInfo(),
chainId: await (await client.getChainID()).toString(),
};
}, true);
}

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,
]);
},
true
);

return sign.result;
}
87 changes: 51 additions & 36 deletions src/lib/l1/query.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import BN from "bn.js";
import { PoolInfo, L1AccountInfo, SubstrateAccountInfo, BridgeMetadata } from "../type";
import { L1Client, withL1Client } from "solidity/clients/client";
import { DelphinusWeb3, withBrowerWeb3 } from "web3subscriber/src/client";
import { L1AccountInfo, BridgeMetadata } from "../type";
import { 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,
Expand All @@ -18,60 +22,71 @@ 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),
true,
async (l1client: L1Client) => {
return l1client.encodeL1Address(l1client.getDefaultAccount());
}
);
return await withL1Connection(async (l1Client: BlockChainClient) => {
return l1Client.encodeL1Address(await l1Client.getAccountInfo());
}, true, await getConfigByChainId(L1ClientRole.Wallet, chainId));
}

export async function queryTokenL1Balance(
chainId: string,
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);
}, false, await getConfigByChainId(L1ClientRole.Wallet, chainId));
}

export async function prepareMetaData(pool_list: Array<Array<number>>) {
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]);
return {
id: poolidx,
tokens: [t1, t2],
};
} catch(e) {
const tokens = await bridge.allTokens();
const t1 = tokens[info[1]];
const t2 = tokens[info[2]];
return {
id: poolidx,
tokens: [wrapTokenInfo(info[1], t1), wrapTokenInfo(info[2], t2)],
};
} catch (e) {
console.log(e);
throw e;
}
})
);
return {
chainInfo: (await bridge.getMetaData()).chainInfo,
chainInfo: await bridge.chainInfo(),
poolInfo: pools,
snap: WalletSnap,
};
});
}, false, config);
}
88 changes: 42 additions & 46 deletions src/lib/l1/tx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import BN from "bn.js";
import { SubstrateAccountInfo} from "../type";
import { L1Client, withL1Client } from "solidity/clients/client";
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";
import { getConfigByChainId } from "delphinus-deployment/src/config";
import { L1ClientRole } from "delphinus-deployment/src/types";

Expand All @@ -16,24 +18,21 @@ export async function deposit(
querying: (m: string) => Promise<string>
) {
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)
)
Expand All @@ -49,31 +48,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);
}
);
}, true, await getConfigByChainId(L1ClientRole.Wallet, chainId));
}


2 changes: 1 addition & 1 deletion src/lib/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface L1AccountInfo {
address: string;
chainId: string;
//chainName: string;
web3: any;
// web3: any;
}

/*
Expand Down