-
-
Save owen-reorg/568ae7e5ae24df1db2ef2f58a623621b to your computer and use it in GitHub Desktop.
opbnb testnet withdraw poc with viem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// doc reference: https://viem.sh/op-stack/guides/withdrawals | |
import { | |
createPublicClient, | |
createWalletClient, | |
defineChain, | |
http, | |
parseAbi, | |
parseEther, | |
} from "viem"; | |
import { privateKeyToAccount } from "viem/accounts"; | |
import { base, bsc, bscTestnet } from "viem/chains"; | |
import { | |
chainConfig, | |
getWithdrawals, | |
publicActionsL1, | |
publicActionsL2, | |
walletActionsL1, | |
walletActionsL2, | |
} from "viem/op-stack"; | |
const sourceId = 97; | |
const opbnbTestnet = defineChain({ | |
id: 5611, | |
name: "opBNB Testnet", | |
nativeCurrency: { name: "tBNB", symbol: "tBNB", decimals: 18 }, | |
rpcUrls: { | |
default: { | |
http: ["https://opbnb-testnet-rpc.bnbchain.org"], | |
}, | |
}, | |
blockExplorers: { | |
default: { | |
name: "BscScan", | |
url: "https://opbnb-testnet.bscscan.com", | |
apiUrl: "https://api-testnet.bscscan.com/api", | |
}, | |
}, | |
contracts: { | |
...chainConfig.contracts, | |
l2OutputOracle: { | |
[sourceId]: { | |
address: "0xFf2394Bb843012562f4349C6632a0EcB92fC8810", | |
}, | |
}, | |
multicall3: { | |
address: "0xca11bde05977b3631167028862be2a173976ca11", | |
// blockCreated: 5022, | |
}, | |
portal: { | |
[sourceId]: { | |
address: "0x4386C8ABf2009aC0c263462Da568DD9d46e52a31", | |
// blockCreated: 17482143, | |
}, | |
}, | |
l1StandardBridge: { | |
[sourceId]: { | |
address: "0x677311Fd2cCc511Bbc0f581E8d9a07B033D5E840", | |
// blockCreated: 17482143, | |
}, | |
}, | |
}, | |
sourceId, | |
}); | |
const L2OutputOracleAbi = [ | |
"function latestBlockNumber() external view returns (uint256)", | |
]; | |
async function main() { | |
console.log(`opBNB bridge test`); | |
const publicClientL1 = createPublicClient({ | |
chain: bscTestnet, | |
transport: http(), | |
}).extend(publicActionsL1()); | |
const publicClientL2 = createPublicClient({ | |
chain: opbnbTestnet, | |
transport: http(), | |
}).extend(publicActionsL2()); | |
const txHash = | |
"0xb5658f429c2d7895d2f0306791e1871d2e3f5b28489076aad7cfca89c45d075c"; | |
const receipt = await publicClientL2.getTransactionReceipt({ hash: txHash }); | |
console.dir({ receipt }, { depth: null }); | |
const [withdrawal] = await getWithdrawals(receipt); | |
console.dir({ withdrawal }, { depth: null }); | |
const latestBlockNumberInL2OutputOracle = await publicClientL1.readContract({ | |
address: "0xFf2394Bb843012562f4349C6632a0EcB92fC8810", | |
abi: parseAbi(L2OutputOracleAbi), | |
functionName: "latestBlockNumber", | |
}); | |
console.dir({ latestBlockNumberInL2OutputOracle }, { depth: null }); | |
const output = await publicClientL1.getL2Output({ | |
l2BlockNumber: latestBlockNumberInL2OutputOracle, | |
targetChain: opbnbTestnet, | |
}); | |
console.dir({ output }, { depth: null }); | |
// Build parameters to prove the withdrawal on the L2. | |
const proveArgs = await publicClientL2.buildProveWithdrawal({ | |
output, | |
withdrawal, | |
}); | |
console.dir({ proveArgs }, { depth: null }); | |
// check init account from wallet details: https://viem.sh/docs/clients/wallet#1-initialize-a-wallet-client-1 | |
const account = privateKeyToAccount(process.env.PRIVATE_KEY); | |
const addr = account.address; | |
console.dir({ addr }, { depth: null }); | |
const l1WalletClient = createWalletClient({ | |
account, | |
chain: bscTestnet, | |
transport: http(), | |
}).extend(walletActionsL1()); | |
// send prove transaction | |
// const proveTx = await l1WalletClient.proveWithdrawal(proveArgs) | |
// console.dir({ proveTx }, { depth: null }); | |
// finalize | |
const finalizeHash = await l1WalletClient.finalizeWithdrawal({ | |
targetChain: publicClientL2.chain, | |
withdrawal, | |
}); | |
console.dir({ finalizeHash }, { depth: null }); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment