Created
October 5, 2023 11:45
-
-
Save grsLammy/b9b057526977d544741abd662f1b669e to your computer and use it in GitHub Desktop.
deposit MATIC from L1 to L2 using maticjs-plasma
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 { getChildUser, 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 depositMATIC() { | |
try { | |
console.log("\n-----------------------------------------"); | |
console.log("BRIDGE MATIC from L1 to L2"); | |
console.log("-----------------------------------------\n"); | |
/* ---------------------------- INPUT ------------------------------ */ | |
const maticAddress = maticAddressL1; | |
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(maticAddress, true); | |
/* ---------------------------- APPROVE ---------------------------- */ | |
/* | |
APPROVE ROOT TOKEN | |
*/ | |
console.log("\n-----------------------------------------"); | |
console.log("APPROVE - MATIC TO TRANSFER TO DEPOSIT MANAGER AKA PLASMA CONTRACT ON L1"); | |
console.log("-----------------------------------------\n"); | |
let approveResponse: ITransactionWriteResult = await maticToken.approve(amount); | |
await sleep(60000); // wait at least 60sec for state change | |
console.log(`Approve transaction hash: `, await approveResponse.getTransactionHash()); | |
console.log( | |
`Transaction details: https://goerli.etherscan.io/tx/${await approveResponse.getTransactionHash()}` | |
); | |
console.log("\nMATIC Approved successfully"); | |
/* ---------------------------- DEPOSIT ---------------------------- */ | |
/* | |
DEPOSIT ROOT TOKEN | |
*/ | |
console.log("\n-----------------------------------------"); | |
console.log("DEPOSIT - MATIC TO PLASMA BRIDGE CONTRACT"); | |
console.log("-----------------------------------------\n"); | |
let depositResponse: ITransactionWriteResult = await maticToken.deposit(amount, getChildUser()); | |
console.log("Deposit transaction hash: ", await depositResponse.getTransactionHash()); | |
console.log( | |
`Transaction details: https://goerli.etherscan.io/tx/${await depositResponse.getTransactionHash()}` | |
); | |
console.log(`\nMATIC Deposited successfully to Plasma Bridge Contract`); | |
} else { | |
console.log("plasmaClient is undefined"); | |
} | |
} catch (error) { | |
console.log("Error in depositMATIC: ", error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment