Last active
April 21, 2023 15:57
-
-
Save Theo6890/8d118c2ecc1ad5625611d961f2949e05 to your computer and use it in GitHub Desktop.
Encode a solidity struct in using etherjs
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
// consider our struct to be: | |
/** | |
struct TaskReward { | |
uint256 igoId; | |
Tier tier; | |
address rewardee; | |
uint256 taskId; | |
} | |
*/ | |
const ethers = require('ethers'); | |
async function sign( | |
igoId = 1, | |
tier = 0, | |
addr = '0x295e26495cef6f69dfa69911d9d8e4f3bbadb89b', | |
taskId = 23512345 | |
) { | |
const coder = ethers.utils.defaultAbiCoder; | |
const coded = coder.encode( | |
['tuple(uint256, uint8, address, uint256)'], // an enum is always a uint8 | |
[[igoId, tier, addr, taskId]] | |
); | |
// you can then hash the encoded struct in `coded` | |
// but you can't use `ethers.utils.solidityKeccak256` to | |
// encode & hash a struct in one call | |
process.stdout.write(coded); | |
} | |
sign() | |
.then(() => process.exit(0)) | |
.catch((error) => { | |
console.error(error); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment