-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create CreateTopHatKVPairs contract to demonstrate this
- Loading branch information
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//SPDX-License-Identifier: MIT | ||
pragma solidity =0.8.19; | ||
|
||
import { IHats } from "./interfaces/hats/IHats.sol"; | ||
import { IKeyValuePairs } from "./interfaces/IKeyValuePairs.sol"; | ||
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol"; | ||
|
||
contract CreateTopHatKVPairs { | ||
address keyValuePairs; | ||
IHats hats; | ||
|
||
error UnsuccessfulTopHatCreation(); | ||
|
||
constructor(address _keyValuePairs, IHats _hats) { | ||
keyValuePairs = _keyValuePairs; | ||
hats = _hats; | ||
} | ||
|
||
function createTopHat(string memory _details, string memory _imageURI) public { | ||
uint256 topHatId = hats.mintTopHat(msg.sender, _details, _imageURI); | ||
|
||
string[] memory keys = new string[](1); | ||
keys[0] = "hatsTreeId"; | ||
|
||
string[] memory values = new string[](1); | ||
values[0] = Strings.toString(topHatId); | ||
|
||
(bool success,) = keyValuePairs.delegatecall(abi.encodeWithSignature("updateValues(string[],string[])", keys, values)); | ||
if (!success) { | ||
revert UnsuccessfulTopHatCreation(); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { ContractTransactionResponse } from "ethers"; | ||
import { expect } from "chai"; | ||
import { | ||
KeyValuePairs, | ||
KeyValuePairs__factory, | ||
CreateTopHatKVPairs, | ||
CreateTopHatKVPairs__factory, | ||
MockHats, | ||
MockHats__factory, | ||
} from "../typechain-types"; | ||
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers"; | ||
import hre from "hardhat"; | ||
|
||
describe.only("CreateTopHatKVPairs", () => { | ||
let createTopHatKVPairs: CreateTopHatKVPairs; | ||
let keyValuePairs: KeyValuePairs; | ||
let hats: MockHats; | ||
|
||
let deployer: SignerWithAddress; | ||
let dao1: SignerWithAddress; | ||
let dao2: SignerWithAddress; | ||
|
||
beforeEach(async () => { | ||
[deployer, dao1, dao2] = await hre.ethers.getSigners(); | ||
|
||
hats = await new MockHats__factory(deployer).deploy(); | ||
keyValuePairs = await new KeyValuePairs__factory(deployer).deploy(); | ||
createTopHatKVPairs = await new CreateTopHatKVPairs__factory( | ||
deployer | ||
).deploy(await keyValuePairs.getAddress(), await hats.getAddress()); | ||
}); | ||
|
||
const getValueUpdatedLogs = async (tx: ContractTransactionResponse) => { | ||
const receipt = await tx.wait(); | ||
if (!receipt) { | ||
throw new Error("no receipt"); | ||
} | ||
|
||
const event = receipt.logs.find( | ||
async (e) => e.address === (await createTopHatKVPairs.getAddress()) | ||
); | ||
|
||
if (!event) { | ||
throw new Error("no event"); | ||
} | ||
|
||
const decodedEvent = keyValuePairs.interface.decodeEventLog( | ||
"ValueUpdated", | ||
event.data, | ||
event.topics | ||
); | ||
|
||
return decodedEvent; | ||
}; | ||
|
||
it("Creates a new Top Hat with expected ID", async () => { | ||
const tx = await createTopHatKVPairs.connect(dao1).createTopHat("", ""); | ||
const result = await getValueUpdatedLogs(tx); | ||
|
||
expect(result[0]).to.eq(dao1.address); | ||
expect(result[1]).to.eq("hatsTreeId"); | ||
expect(result[2]).to.eq("0"); | ||
}); | ||
|
||
it("Creates a Top Hat from the expected address", async () => { | ||
const tx = await createTopHatKVPairs.connect(dao2).createTopHat("", ""); | ||
const result = await getValueUpdatedLogs(tx); | ||
|
||
expect(result[0]).to.eq(dao2.address); | ||
expect(result[1]).to.eq("hatsTreeId"); | ||
expect(result[2]).to.eq("0"); | ||
}); | ||
|
||
it("Creates Top Hats with sequential IDs", async () => { | ||
const tx1 = await createTopHatKVPairs.connect(dao1).createTopHat("", ""); | ||
const result1 = await getValueUpdatedLogs(tx1); | ||
|
||
expect(result1[0]).to.eq(dao1.address); | ||
expect(result1[1]).to.eq("hatsTreeId"); | ||
expect(result1[2]).to.eq("0"); | ||
|
||
const tx2 = await createTopHatKVPairs.connect(dao2).createTopHat("", ""); | ||
const result2 = await getValueUpdatedLogs(tx2); | ||
|
||
expect(result2[0]).to.eq(dao2.address); | ||
expect(result2[1]).to.eq("hatsTreeId"); | ||
expect(result2[2]).to.eq("1"); | ||
}); | ||
}); |