Advertisement
Aldin_SXR

unifiedRelay.js

Mar 19th, 2023
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function main() {
  2.  
  3.     /** Generate the meta transaction */
  4.     console.log("Generating the meta transaction...")
  5.     // Student address
  6.     let studentAddress = "0x850eb0C74c086Fa11a270F06F6665d1f92553342"
  7.     // Course list
  8.     let courses = [0, 1, 3, 4]
  9.  
  10.     // Get the function signature by hashing it and retrieving the first 4 bytes
  11.     let fnSignature = ethers.utils.id("registerCourses(address,uint256[])").substr(0, 10)
  12.  
  13.     // Encode the function parameters and add them to the calldata
  14.     let fnParams = ethers.utils.defaultAbiCoder.encode(
  15.         ["address", "uint256[]"],
  16.         [studentAddress, courses]
  17.     )
  18.  
  19.     // "Raw" transaction data
  20.     const CALLDATA = fnSignature + fnParams.substr(2)
  21.     console.log("Calldata:", CALLDATA)
  22.  
  23.     /** Sign the meta transaction */
  24.     console.log("Signing the meta transaction...")
  25.  
  26.     // Address of the elective course contract (or any other contract that we want to execute operations on)
  27.     const CONTRACT_ADDRESS = '0x0dda2Ee42F0fC9B6f5140F94728477b434835A05';
  28.  
  29.     // Nonce (prevents transaction replays)
  30.     // Will need to +1 for every next transaction by this user
  31.     const NONCE = 0;
  32.  
  33.     // Encode the relay transaction
  34.     let rawData = ethers.utils.defaultAbiCoder.encode(
  35.         ["address", "bytes", "uint256"],
  36.         [CONTRACT_ADDRESS, CALLDATA, NONCE]
  37.     )
  38.  
  39.     // Hash the data
  40.     let hash = ethers.utils.solidityKeccak256(["bytes"], [rawData])
  41.  
  42.     // Generate the signature
  43.     const provider = new ethers.providers.AlchemyProvider(network = "goerli", process.env.ALCHEMY_API_KEY);
  44.     const signer = new ethers.Wallet(process.env.GOERLI_PRIVATE_KEY2, provider);
  45.  
  46.     const SIGNATURE = await signer.signMessage(ethers.utils.arrayify(hash))
  47.     console.log("Signature:", SIGNATURE)
  48.  
  49.     /** Relay the meta transaction */
  50.     console.log("Accessing the relay...");
  51.  
  52.     // Your relay address
  53.     const RELAY_ADDRESS = '0x608204800b3EB996C77A0629b721a74EFF844F3F'
  54.  
  55.     const relay = await ethers.getContractAt("Relay", RELAY_ADDRESS);
  56.  
  57.     // Whitelist if necessary
  58.     const isWhitelisted = await relay.isInWhitelist(studentAddress);
  59.     if (!isWhitelisted) {
  60.         console.log("Whitelisting the address...");
  61.         await relay.addToWhitelist(studentAddress);
  62.     }
  63.  
  64.     const relayTx = await relay.forward(CONTRACT_ADDRESS, CALLDATA, NONCE, SIGNATURE)
  65.     await relayTx.wait()
  66.  
  67.     console.log("Transaction successfully relayed.");
  68.     console.log("Transaction hash:", relayTx.hash)
  69. }
  70.  
  71. main()
  72.     .then(() => process.exit(0))
  73.     .catch(error => {
  74.         console.error(error);
  75.         process.exit(1);
  76.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement