Skip to content

Instantly share code, notes, and snippets.

@Theo6890
Last active April 21, 2023 15:57
Show Gist options
  • Save Theo6890/8d118c2ecc1ad5625611d961f2949e05 to your computer and use it in GitHub Desktop.
Save Theo6890/8d118c2ecc1ad5625611d961f2949e05 to your computer and use it in GitHub Desktop.
Encode a solidity struct in using etherjs
// 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