Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async function main() {
- /** Generate the meta transaction */
- console.log("Generating the meta transaction...")
- // Student address
- let studentAddress = "0x850eb0C74c086Fa11a270F06F6665d1f92553342"
- // Course list
- let courses = [0, 1, 3, 4]
- // Get the function signature by hashing it and retrieving the first 4 bytes
- let fnSignature = ethers.utils.id("registerCourses(address,uint256[])").substr(0, 10)
- // Encode the function parameters and add them to the calldata
- let fnParams = ethers.utils.defaultAbiCoder.encode(
- ["address", "uint256[]"],
- [studentAddress, courses]
- )
- // "Raw" transaction data
- const CALLDATA = fnSignature + fnParams.substr(2)
- console.log("Calldata:", CALLDATA)
- /** Sign the meta transaction */
- console.log("Signing the meta transaction...")
- // Address of the elective course contract (or any other contract that we want to execute operations on)
- const CONTRACT_ADDRESS = '0x0dda2Ee42F0fC9B6f5140F94728477b434835A05';
- // Nonce (prevents transaction replays)
- // Will need to +1 for every next transaction by this user
- const NONCE = 0;
- // Encode the relay transaction
- let rawData = ethers.utils.defaultAbiCoder.encode(
- ["address", "bytes", "uint256"],
- [CONTRACT_ADDRESS, CALLDATA, NONCE]
- )
- // Hash the data
- let hash = ethers.utils.solidityKeccak256(["bytes"], [rawData])
- // Generate the signature
- const provider = new ethers.providers.AlchemyProvider(network = "goerli", process.env.ALCHEMY_API_KEY);
- const signer = new ethers.Wallet(process.env.GOERLI_PRIVATE_KEY2, provider);
- const SIGNATURE = await signer.signMessage(ethers.utils.arrayify(hash))
- console.log("Signature:", SIGNATURE)
- /** Relay the meta transaction */
- console.log("Accessing the relay...");
- // Your relay address
- const RELAY_ADDRESS = '0x608204800b3EB996C77A0629b721a74EFF844F3F'
- const relay = await ethers.getContractAt("Relay", RELAY_ADDRESS);
- // Whitelist if necessary
- const isWhitelisted = await relay.isInWhitelist(studentAddress);
- if (!isWhitelisted) {
- console.log("Whitelisting the address...");
- await relay.addToWhitelist(studentAddress);
- }
- const relayTx = await relay.forward(CONTRACT_ADDRESS, CALLDATA, NONCE, SIGNATURE)
- await relayTx.wait()
- console.log("Transaction successfully relayed.");
- console.log("Transaction hash:", relayTx.hash)
- }
- main()
- .then(() => process.exit(0))
- .catch(error => {
- console.error(error);
- process.exit(1);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement