Skip to content

Commit

Permalink
#696/guild implementation (#710)
Browse files Browse the repository at this point in the history
* Create hook to determine guild implementation

* Create hook to determine guild implementation

* refactor

* Create script to copy bytecodes from artifacts, update hook to use those

* format

* refator & fix jsdoc

* fix typo
  • Loading branch information
MiltonTulli authored Mar 25, 2022
1 parent 42c48c4 commit 17433ef
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"cy:run:development": "cypress run --env configFile=development --browser electron",
"test:e2e": "start-server-and-test 'yarn dev --no-browser' 3000 cy:run:development",
"test": "react-app-rewired test --watchAll=false --coverage --verbose",
"updateBytecodes": "node scripts/updateDeployedBytecodes.js",
"prepare": "husky install"
},
"engines": {
Expand Down
39 changes: 39 additions & 0 deletions scripts/updateDeployedBytecodes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const path = require('path');
const fs = require('fs');

function main() {
const paths = {
ERC20Guild:
'../artifacts/dxdao-contracts/contracts/erc20guild/ERC20Guild.sol/ERC20Guild.json',
SnapshotRepERC20Guild:
'../artifacts/dxdao-contracts/contracts/erc20guild/implementations/SnapshotRepERC20Guild.sol/SnapshotRepERC20Guild.json',
DXDGuild:
'../artifacts/dxdao-contracts/contracts/erc20guild/implementations/DXDGuild.sol/DXDGuild.json',
//TODO: add other contracts here (IERC20Guild)
};

const data = Object.entries(paths).reduce((acc, [type, path]) => {
try {
const json = require(path);
return [
...acc,
{
type,
bytecode: json.deployedBytecode,
},
];
} catch (e) {
console.error(
`[updateDeployedBytecodes.js] File was not found: ${path}. Skipping ${type}`
);
return acc;
}
}, []);

fs.writeFileSync(
path.resolve(__dirname, '../src/bytecodes/config.json'),
JSON.stringify(data, null, 2)
);
}

main();
14 changes: 14 additions & 0 deletions src/bytecodes/config.json

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions src/hooks/Guilds/guild/useGuildImplementationType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useMemo, useEffect, useState } from 'react';
import useJsonRpcProvider from '../web3/useJsonRpcProvider';
import { GuildImplementationType } from '../../../types/types.guilds.d';
import deployedBytecodes from '../../../bytecodes/config.json';

/**
* @function useGuildImplementationType
* @param {string} guildAddress
* @returns {string} GuildImplementationType. 'SnapshotRepERC20Guild' | 'DXDGuild' | 'ERC20Guild' | 'IERC20Guild'
*/
export default function useGuildImplementationType(
guildAddress: string
): GuildImplementationType {
const [guildBytecode, setGuildBytecode] = useState<string>('');
const provider = useJsonRpcProvider();

useEffect(() => {
const getBytecode = async () => {
const bytecode = await provider.getCode(guildAddress);
setGuildBytecode(bytecode);
};
getBytecode();
}, [guildAddress, provider]);

const implementationType: GuildImplementationType = useMemo(() => {
if (!guildBytecode) return GuildImplementationType.IERC20Guild;

const match = deployedBytecodes.find(
({ bytecode }) => guildBytecode === bytecode
);

return match ? match.type : GuildImplementationType.IERC20Guild; // default to IERC20Guild
}, [guildBytecode]) as GuildImplementationType;

return implementationType;
}
7 changes: 7 additions & 0 deletions src/types/types.guilds.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ export interface Transaction {
lastCheckedBlockNumber?: number
addedTime: number
confirmedTime?: number
}

export enum GuildImplementationType {
SnapshotRepERC20Guild = 'SnapshotRepERC20Guild',
DXDGuild = 'DXDGuild',
ERC20Guild = 'ERC20Guild',
IERC20Guild = 'IERC20Guild',
}

0 comments on commit 17433ef

Please sign in to comment.