Last active
September 19, 2021 01:40
-
-
Save carnoxen/17b5a4352282b95d156b15ba4c26666c to your computer and use it in GitHub Desktop.
Gashapon test by hardhat
This file contains hidden or 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
const { ethers } = require("hardhat"); | |
// json -> solidity struct | |
function encodeData(str) { | |
const {users, ranks} = JSON.parse(str) | |
return [users, ranks.map(({name, count}) => [name, count, []])] | |
} | |
// solidity struct -> json | |
function decodeData(enc) { | |
return JSON.stringify(enc.map(([name,,winners,..._]) => ({name, winners}))); | |
} | |
describe("Gashapon contract test", function () { | |
it("Should return the new list of files", async function () { | |
const Gashapon = await ethers.getContractFactory("Gashapon"); | |
const gashapon = await Gashapon.deploy(); | |
await gashapon.deployed(); | |
const data = { | |
users: ["a", "b", "c", "d", "e", "f", "g", "h",], | |
ranks: [ | |
{ | |
name: "first", | |
count: 1 | |
}, | |
{ | |
name: "second", | |
count: 2 | |
} | |
] | |
} | |
const s = JSON.stringify(data); | |
const encoded = encodeData(s); | |
console.log(encoded); | |
const alloc = await gashapon.allocateUsers(encoded); | |
//console.log(alloc); | |
const decoded = decodeData(alloc); | |
console.log(decoded); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment