Skip to content

Commit

Permalink
Merge pull request #18 from OffchainLabs/fee-token-bridging
Browse files Browse the repository at this point in the history
Add support for native L2-L3 bridging of the fee token
  • Loading branch information
gzeoneth authored Dec 8, 2023
2 parents 12d9bed + 5f6a418 commit 9062881
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
73 changes: 71 additions & 2 deletions scripts/ethcommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ContractFactory, ethers, Wallet } from "ethers";
import * as consts from "./consts";
import { namedAccount, namedAddress } from "./accounts";
import * as ERC20PresetFixedSupplyArtifact from "@openzeppelin/contracts/build/contracts/ERC20PresetFixedSupply.json";
import * as ERC20 from "@openzeppelin/contracts/build/contracts/ERC20.json";
import * as fs from "fs";
const path = require("path");

Expand Down Expand Up @@ -52,6 +53,38 @@ async function bridgeFunds(argv: any, parentChainUrl: string, chainUrl: string,
}
}

async function bridgeNativeToken(argv: any, parentChainUrl: string, chainUrl: string, inboxAddr: string, token: string) {
argv.provider = new ethers.providers.WebSocketProvider(parentChainUrl);

argv.to = "address_" + inboxAddr;

/// approve inbox to use fee token
const bridgerParentChain = namedAccount(argv.from, argv.threadId).connect(argv.provider)
const nativeTokenContract = new ethers.Contract(token, ERC20.abi, bridgerParentChain)
await nativeTokenContract.approve(inboxAddr, ethers.utils.parseEther(argv.amount))

/// deposit fee token
const iface = new ethers.utils.Interface(["function depositERC20(uint256 amount)"])
argv.data = iface.encodeFunctionData("depositERC20", [ethers.utils.parseEther(argv.amount)]);

await runStress(argv, sendTransaction);

argv.provider.destroy();
if (argv.wait) {
const childProvider = new ethers.providers.WebSocketProvider(chainUrl);
const bridger = namedAccount(argv.from, argv.threadId).connect(childProvider)
const sleep = (ms: number) => new Promise(r => setTimeout(r, ms));
while (true) {
const balance = await bridger.getBalance()
if (balance.gte(ethers.utils.parseEther(argv.amount))) {
return
}
await sleep(100)
}
}
}


export const bridgeFundsCommand = {
command: "bridge-funds",
describe: "sends funds from l1 to l2",
Expand Down Expand Up @@ -84,7 +117,6 @@ export const bridgeFundsCommand = {
},
};


export const bridgeToL3Command = {
command: "bridge-to-l3",
describe: "sends funds from l2 to l3",
Expand Down Expand Up @@ -117,6 +149,43 @@ export const bridgeToL3Command = {
},
};

export const bridgeNativeTokenToL3Command = {
command: "bridge-native-token-to-l3",
describe: "bridge native token from l2 to l3",
builder: {
amount: {
string: true,
describe: "amount to transfer",
default: "10",
},
from: {
string: true,
describe: "account (see general help)",
default: "funnel",
},
token: {
string: true,
describe: "chain's custom fee token",
},
wait: {
boolean: true,
describe: "wait till l3 has balance of amount",
default: false,
},
},
handler: async (argv: any) => {
const deploydata = JSON.parse(
fs
.readFileSync(path.join(consts.configpath, "l3deployment.json"))
.toString()
);
const inboxAddr = ethers.utils.hexlify(deploydata.inbox);

argv.ethamount = "0"
await bridgeNativeToken(argv, argv.l2url, argv.l3url, inboxAddr, argv.token)
},
};

export const createERC20Command = {
command: "create-erc20",
describe: "creates simple ERC20 on L2",
Expand Down Expand Up @@ -228,7 +297,7 @@ export const sendL2Command = {

export const sendL3Command = {
command: "send-l3",
describe: "sends funds between l2 accounts",
describe: "sends funds between l3 accounts",
builder: {
ethamount: {
string: true,
Expand Down
2 changes: 2 additions & 0 deletions scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "./accounts";
import {
bridgeFundsCommand,
bridgeNativeTokenToL3Command,
bridgeToL3Command,
createERC20Command,
sendL1Command,
Expand All @@ -31,6 +32,7 @@ async function main() {
.options(stressOptions)
.command(bridgeFundsCommand)
.command(bridgeToL3Command)
.command(bridgeNativeTokenToL3Command)
.command(createERC20Command)
.command(sendL1Command)
.command(sendL2Command)
Expand Down
8 changes: 7 additions & 1 deletion test-node.bash
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,13 @@ if $force_init; then
echo
fi

if ! $l3_custom_fee_token; then
echo == Fund L3 accounts
if $l3_custom_fee_token; then
native_token=`docker-compose run --entrypoint sh poster -c "jq -r '.[0].rollup[\"native-token\"]' /config/deployed_l3_chain_info.json | tail -n 1 | tr -d '\r\n'"`
docker-compose run scripts bridge-native-token-to-l3 --token $native_token --amount 50000 --from user_token_bridge_deployer --wait
docker-compose run scripts send-l3 --ethamount 500 --from user_token_bridge_deployer --wait
docker-compose run scripts send-l3 --ethamount 500 --from user_token_bridge_deployer --to "key_0x$devprivkey" --wait
else
docker-compose run scripts bridge-to-l3 --ethamount 50000 --wait
docker-compose run scripts bridge-to-l3 --ethamount 500 --wait --from "key_0x$devprivkey"
fi
Expand Down

0 comments on commit 9062881

Please sign in to comment.