Skip to content

Commit

Permalink
Create CreateTopHatKVPairs contract to demonstrate this
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgall committed Jun 21, 2024
1 parent 712ed05 commit 19992b6
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
33 changes: 33 additions & 0 deletions contracts/CreateTopHatKVPairs.sol
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();
}
}
}
89 changes: 89 additions & 0 deletions test/CreateTopHatKVPairs.test.ts
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");
});
});

0 comments on commit 19992b6

Please sign in to comment.