Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: added Bridge-and-Call Interface in lxly.js #54

Merged
merged 3 commits into from
Nov 26, 2024
Merged
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ dist
config.json
.nuxt
docs

examples/package-lock.json
package-lock.json
3 changes: 3 additions & 0 deletions examples/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ NETWORK=testnet
# SEPOLIA DETAILS
NETWORK_0_RPC=
NETWORK_0_BRIDGE=
NETWORK_0_BRIDGE_EXTENSION=
NETWORK_0_WRAPPER=

# CARDONA DETAILS
NETWORK_1_RPC=
NETWORK_1_BRIDGE=
NETWORK_1_BRIDGE_EXTENSION=
7 changes: 5 additions & 2 deletions examples/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ module.exports = {
network: process.env.NETWORK || 'testnet',
configuration: {
0: {
rpc: process.env.NETWORK_0_RPC || 'https://eth-sepolia.g.alchemy.com/v2/demo',
rpc: process.env.NETWORK_0_RPC || 'https://rpc.sepolia.org',
bridgeAddress: process.env.NETWORK_0_BRIDGE || '0x528e26b25a34a4A5d0dbDa1d57D318153d2ED582',
wrapperAddress: '0x0f04f8434bac2e1db8fca8a34d3e177b6c7ccaba',
bridgeExtensionAddress: process.env.NETWORK_0_BRIDGE_EXTENSION || '0x2311BFA86Ae27FC10E1ad3f805A2F9d22Fc8a6a1',
wrapperAddress: process.env.NETWORK_0_WRAPPER || '0x0f04f8434bac2e1db8fca8a34d3e177b6c7ccaba',

isEIP1559Supported: true
},
1: {
rpc: process.env.NETWORK_1_RPC || 'https://rpc.cardona.zkevm-rpc.com',
bridgeAddress: process.env.NETWORK_1_BRIDGE || '0x528e26b25a34a4A5d0dbDa1d57D318153d2ED582',
bridgeExtensionAddress: process.env.NETWORK_1_BRIDGE_EXTENSION || '0x2311BFA86Ae27FC10E1ad3f805A2F9d22Fc8a6a1',
isEIP1559Supported: true
},
},
Expand Down
122 changes: 122 additions & 0 deletions examples/lxly/bridge_and_call/bridge_and_call.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
const { getLxLyClient, tokens, configuration, from } = require('../../utils_lxly');

// counter example smart contract on destination network.
const CounterABI = [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "count",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "increment",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "originAddress",
"type": "address"
},
{
"internalType": "uint32",
"name": "originNetwork",
"type": "uint32"
},
{
"internalType": "bytes",
"name": "metadata",
"type": "bytes"
}
],
"name": "onMessageReceived",
"outputs": [],
"stateMutability": "payable",
"type": "function"
}
]

const execute = async () => {
const client = await getLxLyClient();

// set token as `eth`.
const token = "0x0000000000000000000000000000000000000000";
// not bridging any token this time
const amount = "0x0";
// because we are bridging from cardona.
const sourceNetwork = 1;
// sending to zkyoto
const destinationNetwork = 0;
// change it to the counter smart contract deployed on destination network.
const callAddress = "0x43854F7B2a37fA13182BBEA76E50FC8e3D298CF1";
// if transaction fails, then the funds will be sent back to user's address on destination network.
const fallbackAddress = from;
// if true, then the global exit root will be updated.
const forceUpdateGlobalExitRoot = true;
// get the call Contract ABI instance.
const callContract = client.contract(CounterABI, callAddress, destinationNetwork);
// prepare the call data for the counter smart contract on destination chain.
const callData = await callContract.encodeAbi("increment", "0x4");

let result;
// Call bridgeAndCall function.
if (client.client.network === "testnet") {
console.log("testnet");
result = await client.bridgeExtensions[sourceNetwork].bridgeAndCall(
token,
amount,
destinationNetwork,
callAddress,
fallbackAddress,
callData,
forceUpdateGlobalExitRoot,
permitData="0x0", // permitData is optional
)
} else {
console.log("mainnet");
result = await client.bridgeExtensions[sourceNetwork].bridgeAndCall(
token,
amount,
destinationNetwork,
callAddress,
fallbackAddress,
callData,
forceUpdateGlobalExitRoot,
)
}

console.log("result", result);
const txHash = await result.getTransactionHash();
console.log("txHash", txHash);
const receipt = await result.getReceipt();
console.log("receipt", receipt);
}

execute().then(() => {
}).catch(err => {
console.error("err", err);
}).finally(_ => {
process.exit(0);
});
43 changes: 43 additions & 0 deletions examples/lxly/bridge_and_call/claim_message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const { getLxLyClient, tokens, configuration, from, to } = require('../../utils_lxly');

const execute = async () => {
const client = await getLxLyClient();
// example bridge txn hash from the source chain.
const bridgeTransactionHash = "0x6f312627ea607a39f494f49e8b40e9a71c61ad3173a6876f09dd9de7b540c040";
// Source Network ID, in this example its 1 since its from cardona.
const sourceNetworkId = 1;
// Destination Network ID, in this example its 0 since its to sepolia.
const destinationNetworkId = 0;
// API for building payload for claim
// `bridgeIndex` is needed when there's multiple bridge events in a single txn. It is used to select the claim associated bridge event.
const result =
await client.bridgeUtil.buildPayloadForClaim(bridgeTransactionHash, sourceNetworkId, bridgeIndex=1)
// payload is then passed to `claimMessage` API
.then((payload) => {
return client.bridges[destinationNetworkId].claimMessage(
payload.smtProof,
payload.smtProofRollup,
payload.globalIndex,
payload.mainnetExitRoot,
payload.rollupExitRoot,
payload.originNetwork,
payload.originTokenAddress,
payload.destinationNetwork,
payload.destinationAddress,
payload.amount,
payload.metadata
);
});

const txHash = await result.getTransactionHash();
console.log("txHash", txHash);
const receipt = await result.getReceipt();
console.log("receipt", receipt);
}

execute().then(() => {
}).catch(err => {
console.error("err", err);
}).finally(_ => {
process.exit(0);
});
9 changes: 5 additions & 4 deletions examples/lxly/erc20/claim_asset.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const { getLxLyClient, tokens, configuration, from } = require('../../utils_lxly');

const execute = async () => {
const bridgeTransactionHash = "0x66f0e813ee4eead3709b2db5478e710e74f5d7460a0008d0022dbc7680e5d5ad";
const client = await getLxLyClient('mainnet');
const bridgeTransactionHash = "0x89e43e85eae56d42ae09a23f1c89c47e9815b7f88d05d4a9427ee13b4772e652";
const client = await getLxLyClient('testnet');

const token = client.erc20("0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0", 0);
const token = client.erc20(tokens[0].ether, 0);
const sourceNetworkId = 1;

const result = await token.claimAsset(bridgeTransactionHash, 1, {returnTransaction: true});
const result = await token.claimAsset(bridgeTransactionHash, sourceNetworkId, {returnTransaction: true});
console.log("result", result);
const txHash = await result.getTransactionHash();
console.log("txHash", txHash);
Expand Down
4 changes: 3 additions & 1 deletion examples/utils_lxly.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const config = require('./config');
const SCALING_FACTOR = new bn(10).pow(new bn(18));

use(Web3ClientPlugin)
setProofApi('https://api-gateway.polygon.technology/api/v3/proof/mainnet')
setProofApi('https://api-gateway.polygon.technology/api/v3/proof/testnet')
const getLxLyClient = async (network = 'testnet') => {
const lxLyClient = new LxLyClient();
return await lxLyClient.init({
Expand All @@ -20,6 +20,7 @@ const getLxLyClient = async (network = 'testnet') => {
configuration: {
bridgeAddress: config.configuration[0].bridgeAddress,
wrapperAddress: config.configuration[0].wrapperAddress,
bridgeExtensionAddress: config.configuration[0].bridgeExtensionAddress,
isEIP1559Supported: true
},
defaultConfig: {
Expand All @@ -30,6 +31,7 @@ const getLxLyClient = async (network = 'testnet') => {
provider: new HDWalletProvider([config.user1.privateKey], config.configuration[1].rpc),
configuration: {
bridgeAddress: config.configuration[1].bridgeAddress,
bridgeExtensionAddress: config.configuration[1].bridgeExtensionAddress,
isEIP1559Supported: false
},
defaultConfig: {
Expand Down
Loading