Skip to content

Commit

Permalink
Merge pull request #56 from 0xPolygon/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
nitinmittal23 authored Nov 27, 2024
2 parents 0275af1 + d5d24ea commit 7e6684d
Show file tree
Hide file tree
Showing 22 changed files with 374 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm ci
run: npm i
- name: Check lint
run: npm run lint
- name: Build
Expand Down
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
32 changes: 30 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@maticnetwork/lxlyjs",
"version": "2.3.0-beta.2",
"version": "2.3.1",
"description": "Javascript developer library for interacting with Polygon LxLy Bridge",
"main": "dist/lxly.node.js",
"types": "dist/ts/index.d.ts",
Expand Down Expand Up @@ -43,6 +43,7 @@
"homepage": "https://github.com/0xpolygon/lxly.js#readme",
"dependencies": {
"@ethereumjs/util": "^9.0.3",
"@maticnetwork/lxlyjs": "^2.3.0-beta.2",
"assert": "^2.1.0",
"buffer": "^6.0.3",
"node-fetch": "^2.7.0",
Expand All @@ -51,10 +52,10 @@
"devDependencies": {
"@types/node-fetch": "^2.6.11",
"@typescript-eslint/parser": "^7.3.1",
"husky": "^9.0.11",
"lint-staged": "^15.2.2",
"copy-webpack-plugin": "^12.0.2",
"cross-env": "^7.0.3",
"husky": "^9.0.11",
"lint-staged": "^15.2.2",
"ts-loader": "^8.0.0",
"tslint": "^6.1.3",
"typescript": "^4.9.5",
Expand Down
1 change: 1 addition & 0 deletions src/abis/mainnet/BridgeExtension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default [{"type":"constructor","inputs":[{"name":"bridge_","type":"address","internalType":"address"}],"stateMutability":"nonpayable"},{"type":"function","name":"bridge","inputs":[],"outputs":[{"name":"","type":"address","internalType":"contract PolygonZkEVMBridgeV2"}],"stateMutability":"view"},{"type":"function","name":"bridgeAndCall","inputs":[{"name":"token","type":"address","internalType":"address"},{"name":"amount","type":"uint256","internalType":"uint256"},{"name":"destinationNetwork","type":"uint32","internalType":"uint32"},{"name":"callAddress","type":"address","internalType":"address"},{"name":"fallbackAddress","type":"address","internalType":"address"},{"name":"callData","type":"bytes","internalType":"bytes"},{"name":"forceUpdateGlobalExitRoot","type":"bool","internalType":"bool"}],"outputs":[],"stateMutability":"payable"},{"type":"function","name":"onMessageReceived","inputs":[{"name":"originAddress","type":"address","internalType":"address"},{"name":"originNetwork","type":"uint32","internalType":"uint32"},{"name":"data","type":"bytes","internalType":"bytes"}],"outputs":[],"stateMutability":"payable"},{"type":"error","name":"AmountDoesNotMatchMsgValue","inputs":[]},{"type":"error","name":"InvalidDepositIndex","inputs":[]},{"type":"error","name":"OriginMustBeBridgeExtension","inputs":[]},{"type":"error","name":"SenderMustBeBridge","inputs":[]},{"type":"error","name":"UnclaimedAsset","inputs":[]}]
4 changes: 3 additions & 1 deletion src/abis/mainnet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import PolygonZkEVMGlobalExitRoot from "./PolygonZkEVMGlobalExitRoot";
import PolygonZkEVMGlobalExitRootL2 from "./PolygonZkEVMGlobalExitRootL2";
import ZkEVMBridgeAdapter from "./ZkEVMBridgeAdapter";
import ZkEVMWrapper from "./ZkEVMWrapper";
import BridgeExtension from "./BridgeExtension";
import GasPorter from "./GasPorter";

export default {
Expand All @@ -15,5 +16,6 @@ export default {
PolygonZkEVMGlobalExitRoot,
PolygonZkEVMGlobalExitRootL2,
ZkEVMBridgeAdapter,
ZkEVMWrapper
ZkEVMWrapper,
BridgeExtension,
}
30 changes: 30 additions & 0 deletions src/abis/testnet/BridgeExtension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export default [
{ "type": "constructor", "inputs": [], "stateMutability": "nonpayable" },
{ "type": "function", "name": "bridge",
"inputs": [],
"outputs": [
{ "name": "", "type": "address", "internalType": "contract PolygonZkEVMBridgeV2" }
], "stateMutability": "view"
},
{ "type": "function", "name": "bridgeAndCall",
"inputs": [
{ "name": "token", "type": "address", "internalType": "address" },
{ "name": "amount", "type": "uint256", "internalType": "uint256" },
{ "name": "permitData", "type": "bytes", "internalType": "bytes" },
{ "name": "destinationNetwork", "type": "uint32", "internalType": "uint32" },
{ "name": "callAddress", "type": "address", "internalType": "address" },
{ "name": "fallbackAddress", "type": "address", "internalType": "address" },
{ "name": "callData", "type": "bytes", "internalType": "bytes" },
{ "name": "forceUpdateGlobalExitRoot", "type": "bool", "internalType": "bool" }
], "outputs": [], "stateMutability": "payable"
},
{ "type": "function", "name": "initialize", "inputs": [{ "name": "bridge_", "type": "address", "internalType": "address" }], "outputs": [], "stateMutability": "nonpayable" },
{ "type": "function", "name": "onMessageReceived", "inputs": [{ "name": "originAddress", "type": "address", "internalType": "address" }, { "name": "originNetwork", "type": "uint32", "internalType": "uint32" }, { "name": "data", "type": "bytes", "internalType": "bytes" }], "outputs": [], "stateMutability": "payable" },
{ "type": "event", "name": "Initialized", "inputs": [{ "name": "version", "type": "uint8", "indexed": false, "internalType": "uint8" }], "anonymous": false },
{ "type": "error", "name": "AmountDoesNotMatchMsgValue", "inputs": [] },
{ "type": "error", "name": "InvalidAddress", "inputs": [] },
{ "type": "error", "name": "InvalidDepositIndex", "inputs": [] },
{ "type": "error", "name": "OriginMustBeBridgeExtension", "inputs": [] },
{ "type": "error", "name": "SenderMustBeBridge", "inputs": [] },
{ "type": "error", "name": "UnclaimedAsset", "inputs": [] }
]
Loading

0 comments on commit 7e6684d

Please sign in to comment.