Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/additional jar types #26

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/components/JarCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export const JarCard = ({ record }: { record: CookieJar }) => {
const { isMember } = useCookieJar({ cookieJarId: record.jarUid });
const [balance, setBalance] = useState<string>("Loading");

console.log("Rendering JarCard: ", record);

useEffect(() => {
const getBalance = async () => {
const _balance = await publicClient?.getBalance({
Expand All @@ -96,7 +98,7 @@ export const JarCard = ({ record }: { record: CookieJar }) => {
<StyledCardHeader>
<div>
<img src={cookie} alt="cookie" height={"20px"} />
<H4>{record.name}</H4>
<H4>{`${record.name} - ${record.type}`}</H4>
<ParMd style={{ marginBottom: ".4rem" }}>
{record.description}
</ParMd>
Expand Down
7 changes: 0 additions & 7 deletions src/hooks/useIndexer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,9 @@ const useIndexer = () => {
}
}, [addresses, indexer, publicClient]);

const cookieJars = useLiveQuery(() =>
db.cookieJars.where({ chainId: Number(chainId) }).toArray()
);
const cookies = useLiveQuery(() => db.cookies.toArray());

return {
indexer,
client: publicClient,
cookieJars,
cookies,
};
};

Expand Down
9 changes: 7 additions & 2 deletions src/pages/Jars.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {

import { JarCard } from "../components/JarCard";
import { StyledRouterLink } from "../components/Layout";
import { useIndexer } from "../hooks/useIndexer";
import { useLiveQuery } from "dexie-react-hooks";
import { db } from "../utils/indexer";
import { useDHConnect } from "@daohaus/connect";

const LinkBox = styled.div`
display: flex;
Expand All @@ -33,7 +35,10 @@ const JarContainer = styled(Card)`
`;

export const Jars = () => {
const { cookieJars } = useIndexer();
const { chainId } = useDHConnect();
const cookieJars = useLiveQuery(() =>
db.cookieJars.where({ chainId: Number(chainId) }).toArray()
);

return (
<SingleColumnLayout>
Expand Down
2 changes: 1 addition & 1 deletion src/targetDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ export const TARGETS: { [key: string]: Targets } = {
development: TARGET_GOERLI,
};

export const DEFAULT_TARGET: Targets = TARGET_GNOSIS;
export const DEFAULT_TARGET: Targets = TARGET_GOERLI;
24 changes: 23 additions & 1 deletion src/utils/indexer/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,29 @@ export interface ListInitializer extends CookieJarInitializer {
allowList: string[];
}

export type Initializer = CookieJarInitializer | ListInitializer;
export interface ERC20Initializer extends CookieJarInitializer {
_erc20addr: string;
_threshold: bigint;
}

export interface ERC721Initializer extends CookieJarInitializer {
_erc721addr: string;
_threshold: bigint;
}

export interface BaalInitializer extends CookieJarInitializer {
_dao: string;
_threshold: bigint;
_useShares: boolean;
_useLoot: boolean;
}

export type Initializer =
| CookieJarInitializer
| ListInitializer
| ERC20Initializer
| ERC721Initializer
| BaalInitializer;

export interface CookieJar {
chainId: 5 | 100;
Expand Down
208 changes: 186 additions & 22 deletions src/utils/indexer/eventHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { parseAbi, parseAbiItem, parseAbiParameters } from "abitype";
import { Cookie, CookieJar, db } from "./db";
import {
BaalInitializer,
Cookie,
CookieJar,
CookieJarInitializer,
ERC20Initializer,
ERC721Initializer,
Initializer,
ListInitializer,
db,
} from "./db";
import {
Log,
decodeAbiParameters,
Expand All @@ -8,6 +18,7 @@ import {
stringToBytes,
keccak256,
} from "viem";
import _ from "lodash";

export type EventHandlers = "StoreCookieJar" | "StoreCookie";
type SummonLog = {
Expand All @@ -27,13 +38,31 @@ const hasArgs = (obj: unknown): obj is { args: unknown } => {
return typeof obj === "object" && obj !== null && "args" in obj;
};

export type Details = {
type Details6551 = {
type: string;
name: string;
title: string;
description: string;
link: string;
};

const DETAILS = {
ERC20: "ERC20 Cookie Jar V2",
ERC721: "NFT Cookie Jar V2",
List: "List Cookie Jar V2",
Open: "Open Cookie Jar V2",
Baal: "Baal CookieJar V2",
};

const parameterString = {
baal: "address safeTarget, uint256 periodLenght, uint256 cookieAmount, address cookieToken, address _dao, uint256 _threshold, bool _useShares, bool _useLoot",
erc20:
"address safeTarget, uint256 periodLenght, uint256 cookieAmount, address cookieToken, address _erc20addr, uint256 _threshold",
erc721:
"address safeTarget, uint256 periodLenght, uint256 cookieAmount, address cookieToken, address _erc721addr, uint256 _threshold",
list: "address safeTarget, uint256 periodLenght, uint256 cookieAmount, address cookieToken, address[] allowlist",
open: "address safeTarget, uint256 periodLenght, uint256 cookieAmount, address cookieToken",
};

const isSummonLog = (obj: unknown): obj is SummonLog => {
return (
typeof obj === "object" &&
Expand Down Expand Up @@ -69,35 +98,170 @@ const storeCookieJar = async (
return;
}

const { cookieJar, initializer, details, uid } = summonCookieJarLog.args;
const chainId = await publicClient.getChainId();

const _details: {} = JSON.parse(details as string) as Details;
const parseSummonArguments = (log: SummonLog) => {
const { cookieJar, initializer, details, uid } = log;
const decoded = decodeFunctionData({
abi: parseAbi(["function setUp(bytes)"]),
data: initializer,
});

const _details = {
type: "",
name: "",
description: "",
link: "",
};

let _initializer: Initializer | undefined;

if (details === DETAILS.Baal) {
_details.type = "Baal";
_details.name = "Cookie Jar V2";

const decodedAbiParameters = decodeAbiParameters(
parseAbiParameters(parameterString.baal),
decoded.args[0]
);

_initializer = {
safeTarget: decodedAbiParameters[0],
periodLength: decodedAbiParameters[1],
cookieAmount: decodedAbiParameters[2],
cookieToken: decodedAbiParameters[3],
_dao: decodedAbiParameters[4],
_threshold: decodedAbiParameters[5],
_useShares: decodedAbiParameters[6],
_useLoot: decodedAbiParameters[7],
} as BaalInitializer;
}

if (details === DETAILS.ERC20) {
_details.type = "ERC20";
_details.name = "Cookie Jar V2";

const decodedAbiParameters = decodeAbiParameters(
parseAbiParameters(parameterString.erc20),
decoded.args[0]
);

_initializer = {
safeTarget: decodedAbiParameters[0],
periodLength: decodedAbiParameters[1],
cookieAmount: decodedAbiParameters[2],
cookieToken: decodedAbiParameters[3],
_erc20addr: decodedAbiParameters[4],
_threshold: decodedAbiParameters[5],
} as ERC20Initializer;
}

if (details === DETAILS.List) {
_details.type = "List";
_details.name = "Cookie Jar V2";

const decodedAbiParameters = decodeAbiParameters(
parseAbiParameters(parameterString.list),
decoded.args[0]
);

_initializer = {
safeTarget: decodedAbiParameters[0],
periodLength: decodedAbiParameters[1],
cookieAmount: decodedAbiParameters[2],
cookieToken: decodedAbiParameters[3],
allowList: (decodedAbiParameters[4] as string[]).map((addr: string) =>
addr.toLowerCase()
),
} as ListInitializer;
}

if (details === DETAILS.ERC721) {
_details.type = "ERC721";
_details.name = "Cookie Jar V2";

const decodedAbiParameters = decodeAbiParameters(
parseAbiParameters(parameterString.erc721),
decoded.args[0]
);

_initializer = {
safeTarget: decodedAbiParameters[0],
periodLength: decodedAbiParameters[1],
cookieAmount: decodedAbiParameters[2],
cookieToken: decodedAbiParameters[3],
_erc721addr: decodedAbiParameters[4],
_threshold: decodedAbiParameters[5],
} as ERC721Initializer;
}

if (details === DETAILS.Open) {
_details.type = "Open";
_details.name = "Cookie Jar V2";

const decodedAbiParameters = decodeAbiParameters(
parseAbiParameters(parameterString.open),
decoded.args[0]
);

_initializer = {
safeTarget: decodedAbiParameters[0],
periodLength: decodedAbiParameters[1],
cookieAmount: decodedAbiParameters[2],
cookieToken: decodedAbiParameters[3],
} as CookieJarInitializer;
}

try {
if (JSON.parse(details as string)) {
const detailsJson = JSON.parse(details as string) as Details6551;

console.log("Parsed details to JSON", detailsJson);

_details.type = detailsJson.type;
_details.name = detailsJson.title;
_details.description = detailsJson.description;
_details.link = detailsJson.link;

const decodedAbiParameters = decodeAbiParameters(
parseAbiParameters(parameterString.list),
decoded.args[0]
);

_initializer = {
safeTarget: decodedAbiParameters[0],
periodLength: decodedAbiParameters[1],
cookieAmount: decodedAbiParameters[2],
cookieToken: decodedAbiParameters[3],
allowList: decodedAbiParameters[4],
} as ListInitializer;
}
} catch (e) {
console.error("Failed to parse details to JSON", e);
}

if (_.isEmpty(_details) || !_initializer || _.isEmpty(_initializer)) {
throw new Error("Invalid details or initializer");
}

const decoded = decodeFunctionData({
abi: parseAbi(["function setUp(bytes)"]),
data: initializer,
});
return {
cookieJar,
uid,
initializer: _initializer,
details: _details,
};
};

const decodedSetUp = decodeAbiParameters(
parseAbiParameters(
"address safeTarget, uint256 periodLenght, uint256 cookieAmount, address cookieToken, address[] allowlist"
),
decoded.args[0]
const { cookieJar, uid, initializer, details } = parseSummonArguments(
summonCookieJarLog.args
);

const _decodedCookieJar = {
..._details,
...details,
chainId,
jarUid: uid,
address: cookieJar.toLowerCase() as `0x${string}`,
initializer: {
safeTarget: decodedSetUp[0],
periodLength: decodedSetUp[1],
cookieAmount: decodedSetUp[2],
cookieToken: decodedSetUp[3],
allowList: decodedSetUp[4],
},
initializer,
} as CookieJar;

try {
Expand Down