Created
October 5, 2023 11:47
-
-
Save grsLammy/9bdf753f095a5422bb265509cab2624c to your computer and use it in GitHub Desktop.
withdraw MATIC from L2 to L1 and withdraw exit from L1
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
import { sleep } from "../utils/utilityFunctions"; | |
import { getPlasmaClient } from "../utils/plasmaClient"; | |
import { maticAddressL1 } from "../../config"; | |
import { ITransactionWriteResult } from "@maticnetwork/maticjs"; | |
import { PlasmaClient } from "@maticnetwork/maticjs-plasma"; | |
import type { ERC20 } from "@maticnetwork/maticjs-plasma/dist/ts/erc20"; | |
import ps from "prompt-sync"; | |
const prompt = ps(); | |
export async function withdrawExitMATIC() { | |
try { | |
console.log("\n-----------------------------------------"); | |
console.log("WITHDRAW MATIC TOKEN ON L1"); | |
console.log("-----------------------------------------\n"); | |
/* ---------------------------- INPUT ------------------------------ */ | |
const transactionHash = prompt("Enter the transaction hash of the burned token on L2: "); | |
if (!transactionHash) return console.log("The address of Root Token to bridge cannot be null"); | |
if (transactionHash.length !== 66) | |
return console.log(`${transactionHash} is not a valid transaction hash`); | |
/* ---------------------------- SETUP ------------------------------ */ | |
/* | |
SETUP PLASMA CLIENT | |
*/ | |
const plasmaClient: PlasmaClient | undefined = await getPlasmaClient(); | |
if (plasmaClient) { | |
let maticToken: ERC20 = plasmaClient.erc20(maticAddressL1, true); | |
/* ---------------------------- WITHDRAW EXIT ---------------------------- */ | |
/* | |
WITHDRAW EXIT MATIC TOKEN ON L1 | |
*/ | |
console.log("\n-----------------------------------------"); | |
console.log("WITHDRAW - MATIC FROM L2 to L1"); | |
console.log("-----------------------------------------\n"); | |
let withdrawResponse: ITransactionWriteResult = await maticToken.withdrawConfirm(transactionHash); | |
console.log("WithdrawExit transaction hash: ", await withdrawResponse.getTransactionHash()); | |
console.log( | |
`Transaction details: https://goerli.etherscan.io/tx/${await withdrawResponse.getTransactionHash()}` | |
); | |
console.log(`\nMATIC withdrawal Exit successfully`); | |
} else { | |
console.log("plasmaClient is undefined"); | |
} | |
} catch (error) { | |
console.log("Error in withdrawExitMATIC: ", error); | |
} | |
} |
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
import { sleep } from "../utils/utilityFunctions"; | |
import { getPlasmaClient } from "../utils/plasmaClient"; | |
import { maticAddressL2 } from "../../config"; | |
import { ITransactionWriteResult } from "@maticnetwork/maticjs"; | |
import { PlasmaClient } from "@maticnetwork/maticjs-plasma"; | |
import type { ERC20 } from "@maticnetwork/maticjs-plasma/dist/ts/erc20"; | |
import ps from "prompt-sync"; | |
const prompt = ps(); | |
export async function withdrawMATIC() { | |
try { | |
console.log("\n-----------------------------------------"); | |
console.log("BRIDGE MATIC from L2 to L1"); | |
console.log("-----------------------------------------\n"); | |
/* ---------------------------- INPUT ------------------------------ */ | |
const amount: string | null = prompt("Enter the total amount of MATIC to bridge: "); | |
if (!amount) return console.log("Total amount of MATIC to bridge cannot be null"); | |
if (Number(amount) <= 0) | |
return console.log("Total amount of MATIC to bridge cannot be less than or equal to zero"); | |
/* ---------------------------- SETUP ------------------------------ */ | |
/* | |
SETUP PLASMA CLIENT | |
*/ | |
const plasmaClient: PlasmaClient | undefined = await getPlasmaClient(); | |
if (plasmaClient) { | |
let maticToken: ERC20 = plasmaClient.erc20(maticAddressL2); | |
/* ---------------------------- WITHDRAW ---------------------------- */ | |
/* | |
WITHDRAW MATIC TOKEN FROM L2 | |
*/ | |
console.log("\n-----------------------------------------"); | |
console.log("WITHDRAW - MATIC FROM L2 to L1"); | |
console.log("-----------------------------------------\n"); | |
let withdrawResponse: ITransactionWriteResult = await maticToken.withdrawStart(amount); | |
console.log("Withdraw transaction hash: ", await withdrawResponse.getTransactionHash()); | |
console.log( | |
`Transaction details: https://mumbai.polygonscan.com/tx/${await withdrawResponse.getTransactionHash()}` | |
); | |
console.log(`\nMATIC withdrawn successfully`); | |
console.log( | |
`\nNext step is to Withdraw Exit, kindly store the Transaction Hash: ${await withdrawResponse.getTransactionHash()}` | |
); | |
} else { | |
console.log("plasmaClient is undefined"); | |
} | |
} catch (error) { | |
console.log("Error in withdrawMATIC: ", error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment