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 withdrawal finalize step #58

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
86 changes: 86 additions & 0 deletions content/tutorials/how-to-withdraw-eth/10.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,92 @@ L2 Balance is 5712612651486983637
Withdraw transaction sent 0x4905176d42b4c3b4ab10f611e688b2d849e761493f4583119b7c7731b4254cf4
```

## Finalize withdrawal script

Finalize withdrawal is the process that is run later on the L1 (once the L2 batches are proven), and which is actually responsible for transferring
the tokens into your account.

This part is permissionless - so anyone can trigger it. In sepolia, this is done by the cronjob, that is run by the community, but on all the other chains
(mainnet, local networks), you have to trigger it yourself.


### withdraw-finalize.ts script

```typescript
import { Wallet, Provider } from "zksync-ethers";
import * as ethers from "ethers";

// load env file
import dotenv from "dotenv";
dotenv.config();

// HTTP RPC endpoints
const L1_RPC_ENDPOINT = process.env.L1_RPC_ENDPOINT || ""; // or an RPC endpoint from Infura/Chainstack/QuickNode/etc.
const L2_RPC_ENDPOINT = process.env.L2_RPC_ENDPOINT || "https://sepolia.era.zksync.dev"; // or the ZKsync Era mainnet

// Wallet
const WALLET_PRIV_KEY = process.env.WALLET_PRIV_KEY || "";


if (!WALLET_PRIV_KEY) {
throw new Error("Wallet private key is not configured in env file");
}

if (!L1_RPC_ENDPOINT) {
throw new Error("Missing L1 RPC endpoint. Check chainlist.org or an RPC node provider");
}

async function main() {
// Please set your TX id, that you received from the withdraw.ts before.
const WITHDRAW_TX = "<WITHDRAW_TRANSACTION>"

console.log(`Finalizing withdrawal for ${WITHDRAW_TX}`);

// Initialize the wallet.
const l1provider = new ethers.Provider(L1_RPC_ENDPOINT);
const l2provider = new Provider(L2_RPC_ENDPOINT);
const wallet = new Wallet(WALLET_PRIV_KEY, l2provider, l1provider);

const isFinalized = await wallet.isWithdrawalFinalized(WITHDRAW_TX);
console.log("IS finalized ", isFinalized);

if (isFinalized == true) {
console.log("Your withdrawal is already finalized. Your tokens should be on L1");
console.log(`L1 Balance is ${await wallet.getBalanceL1()}`);
console.log(`L2 Balance is ${await wallet.getBalance()}`);

} else {
console.log(`L1 Balance before is ${await wallet.getBalanceL1()}`);
console.log("Finalizing withdrawal now");

const result = await wallet.finalizeWithdrawal(WITHDRAW_TX);
console.log("Waiting for tx :", result.hash);
const receipt = await result.wait();

console.log("Receipt status: ", receipt?.status)
console.log(`L1 Balance after is ${await wallet.getBalanceL1()}`);
}

}

main()
.then()
.catch((error) => {
console.error(error);
process.exitCode = 1;
});
```

### Run the Script

Remember to set your withdrawal transaction from the previous step into the code at line 26 and execute the script using the following command:

```sh
npx ts-node withdraw-finalize.ts
```



## Conclusion

By following this guide, you have successfully withdrawn ETH from L2 to L1 using the ZKsync Javascript SDK.
Expand Down
Loading