Skip to content

Commit

Permalink
chore: downgrade ethers.js, snapshot.js version
Browse files Browse the repository at this point in the history
  • Loading branch information
deep-quality-dev committed Oct 19, 2022
1 parent 921a6a3 commit 31d378f
Show file tree
Hide file tree
Showing 39 changed files with 3,037 additions and 2,659 deletions.
5 changes: 3 additions & 2 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
RINKEBY_RPC_URL=<INFURA_URL>
GOERLI_RPC_URL=<INFURA_URL>
MAINNET_RPC_URL=<MAINNET_RPC_URL>
PRIVATE_KEY=<PRIVET_KEY>
TESTNET_PRIVATE_KEY=<PRIVET_KEY>
MAINNET_PRIVATE_KEY=<PRIVET_KEY>
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zero-tech/zdao-sdk",
"version": "0.12.1",
"version": "0.11.1",
"description": "",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -35,7 +35,6 @@
"cross-fetch": "3.1.5",
"date-fns": "2.28.0",
"dotenv": "16.0.0",
"ethers": "5.6.0",
"graphql": "16.3.0",
"graphql-request": "4.1.0",
"graphql-tag": "2.12.6",
Expand All @@ -44,8 +43,9 @@
"typescript": "4.6.2"
},
"peerDependencies": {
"@zero-tech/zauction-sdk": ">=0.0.44",
"@zero-tech/zns-sdk": ">=0.1.18"
"@zero-tech/zauction-sdk": ">=0.2.11",
"@zero-tech/zns-sdk": ">=0.8.13",
"ethers": ">=5.4.6"
},
"devDependencies": {
"@types/chai": "4.3.0",
Expand All @@ -55,15 +55,17 @@
"@types/node-fetch": "2.6.1",
"@typescript-eslint/eslint-plugin": "5.15.0",
"@typescript-eslint/parser": "5.15.0",
"@zero-tech/zauction-sdk": "^0.0.44",
"@zero-tech/zns-sdk": "^0.1.18",
"@zero-tech/zauction-sdk": "0.2.11",
"@zero-tech/zns-sdk": "0.8.13",
"chai": "4.3.6",
"chai-as-promised": "7.1.1",
"env-var": "7.3.0",
"eslint": "8.11.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-prettier": "4.0.0",
"eslint-plugin-simple-import-sort": "7.0.0",
"eslint-plugin-unused-imports": "2.0.0",
"ethers": "^5.4.6",
"mocha": "9.2.2",
"node-fetch": "3.2.3",
"prettier": "2.5.1",
Expand Down
146 changes: 47 additions & 99 deletions src/SDKInstanceClient.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,50 @@
import { BigNumber, ethers } from 'ethers';
import { BigNumber } from 'ethers';
import shortid from 'shortid';

import DAOClient from './client/DAOClient';
import ERC1967ProxyAbi from './config/constants/abi/ERC1967Proxy.json';
import ZeroTokenAbi from './config/constants/abi/ZeroToken.json';
import ZNAClient from './client/ZNAClient';
import ZNSHubClient from './client/ZNSHubClient';
import GnosisSafeClient from './gnosis-safe';
import SnapshotClient from './snapshot-io';
import {
Config,
CreateZDAOParams,
SDKInstance,
TokenMintOptions,
zDAO,
zNA,
} from './types';
import { Config, CreateZDAOParams, SDKInstance, zDAO, zNA } from './types';
import { getDecimalAmount } from './utilities';
import { getToken, getTotalSupply } from './utilities/calls';
import { errorMessageForError } from './utilities/messages';
import zDAORegistryClient from './zDAORegistry';
import { ZDAORecord } from './zDAORegistry/types';

class SDKInstanceClient implements SDKInstance {
private readonly _config: Config;
private readonly _zDAORegistryClient: zDAORegistryClient;
private readonly _snapshotClient: SnapshotClient;
protected _params: CreateZDAOParams[];
private readonly config: Config;
private readonly zDAORegistryClient: zDAORegistryClient;
private readonly snapshotClient: SnapshotClient;
private readonly gnosisSafeClient: GnosisSafeClient;
protected params: CreateZDAOParams[];

constructor(config: Config) {
this._config = config;
this._zDAORegistryClient = new zDAORegistryClient(config.zNA, config.zNS);
this._snapshotClient = new SnapshotClient(config.snapshot);
this._params = [];
this.config = config;
this.zDAORegistryClient = new zDAORegistryClient(
config.zNA,
config.provider
);
this.snapshotClient = new SnapshotClient(config.snapshot);
this.gnosisSafeClient = new GnosisSafeClient(config.gnosisSafe);
this.params = [];

ZNSHubClient.initialize(config.zNA, config.provider);
ZNAClient.initialize(config.zNS);
}

async listZNAs(): Promise<zNA[]> {
return await this._zDAORegistryClient.listZNAs();
return await this.zDAORegistryClient.listZNAs();
}

async getZDAOByZNA(zNA: zNA): Promise<zDAO> {
// get zDAO information associated with zNA
const zDAORecord: ZDAORecord = await this._zDAORegistryClient
.getZDAORecordByZNA(zNA)
.catch(() => {
throw new Error(errorMessageForError('not-found-zdao'));
});
const zDAORecord: ZDAORecord =
await this.zDAORegistryClient.getZDAORecordByZNA(zNA);

// should be found by ens in snapshot
const space = await this._snapshotClient.getSpaceDetails(zDAORecord.ens);
const space = await this.snapshotClient.getSpaceDetails(zDAORecord.ens);
if (!space) {
throw new Error(errorMessageForError('not-found-ens-in-snapshot'));
}
Expand All @@ -63,11 +62,9 @@ class SDKInstanceClient implements SDKInstance {
const decimals = strategy.params.decimals ?? 0;

const totalSupplyOfVotingToken = await getTotalSupply(
this._config.provider,
this.config.provider,
strategy.params.address
).catch(() => {
throw new Error(errorMessageForError('not-found-strategy-in-snapshot'));
});
);
const minimumTotalVotingTokens = space.quorum
? getDecimalAmount(
BigNumber.from(space.quorum.toString()),
Expand All @@ -83,14 +80,16 @@ class SDKInstanceClient implements SDKInstance {
.toNumber();

return await DAOClient.createInstance(
this._config,
this.config,
this.snapshotClient,
this.gnosisSafeClient,
{
id: zDAORecord.id,
ens: zDAORecord.ens,
zNAs: zDAORecord.zNAs,
title: space.name,
creator: space.admins.length > 0 ? space.admins[0] : zDAORecord.ens,
network: this._config.snapshot.network, // space.network,
network: this.config.snapshot.network, // space.network,
duration: space.duration,
safeAddress: zDAORecord.gnosisSafe,
votingToken: {
Expand Down Expand Up @@ -118,64 +117,11 @@ class SDKInstanceClient implements SDKInstance {
}

async doesZDAOExist(zNA: zNA): Promise<boolean> {
return await this._zDAORegistryClient.doesZDAOExist(zNA);
}

async createZToken(
signer: ethers.Signer,
name: string,
symbol: string,
options?: TokenMintOptions
): Promise<string> {
try {
// create implementation of zToken
const zTokenFactory = new ethers.ContractFactory(
ZeroTokenAbi.abi,
ZeroTokenAbi.bytecode,
signer
);
const zTokenImplementation = await zTokenFactory.deploy();
await zTokenImplementation.deployed();

// create ERC1967 proxy contract
const zTokenInterface = new ethers.utils.Interface(ZeroTokenAbi.abi);
const proxyData = zTokenInterface.encodeFunctionData('initialize', [
name,
symbol,
]);

const proxyFactory = new ethers.ContractFactory(
ERC1967ProxyAbi.abi,
ERC1967ProxyAbi.bytecode,
signer
);

const proxyContract = await proxyFactory.deploy(
zTokenImplementation.address,
proxyData
);
await proxyContract.deployed();

if (options) {
const contract = new ethers.Contract(
proxyContract.address,
ZeroTokenAbi.abi,
signer.provider
);

// mint tokens
await contract.connect(signer).mint(options.target, options.amount);
}

return proxyContract.address;
} catch (error) {
console.error(error);
throw new Error(errorMessageForError('failed-create-token'));
}
return await this.zDAORegistryClient.doesZDAOExist(zNA);
}

async createZDAOFromParams(param: CreateZDAOParams): Promise<zDAO> {
const found = this._params.find(
const found = this.params.find(
(item: CreateZDAOParams) => item.zNA === param.zNA
);
if (found) {
Expand All @@ -191,15 +137,17 @@ class SDKInstanceClient implements SDKInstance {
throw new Error(errorMessageForError('empty-voting-token'));
}

this._params.push(param);
this.params.push(param);

const calls = await Promise.all([
getToken(this._config.provider, param.votingToken),
getTotalSupply(this._config.provider, param.votingToken),
getToken(this.config.provider, param.votingToken),
getTotalSupply(this.config.provider, param.votingToken),
]);

return await DAOClient.createInstance(
this._config,
this.config,
this.snapshotClient,
this.gnosisSafeClient,
{
id: shortid.generate(),
ens: param.ens,
Expand All @@ -222,26 +170,26 @@ class SDKInstanceClient implements SDKInstance {
}

listZNAsFromParams(): Promise<zNA[]> {
return Promise.resolve(this._params.map((param) => param.zNA));
return Promise.resolve(this.params.map((param) => param.zNA));
}

async getZDAOByZNAFromParams(zNA: zNA): Promise<zDAO> {
if (!this.doesZDAOExist(zNA)) {
throw new Error(errorMessageForError('not-found-zdao'));
}

const found = this._params.find((param) => param.zNA === zNA);
const found = this.params.find((param) => param.zNA === zNA);
if (!found) throw new Error(errorMessageForError('not-found-zdao'));

const calls = await Promise.all([
getToken(this._config.provider, found.votingToken),
getTotalSupply(this._config.provider, found.votingToken).catch(() => {
throw new Error(errorMessageForError('not-found-strategy-in-snapshot'));
}),
getToken(this.config.provider, found.votingToken),
getTotalSupply(this.config.provider, found.votingToken),
]);

return await DAOClient.createInstance(
this._config,
this.config,
this.snapshotClient,
this.gnosisSafeClient,
{
id: shortid.generate(),
ens: found.ens,
Expand All @@ -264,7 +212,7 @@ class SDKInstanceClient implements SDKInstance {
}

doesZDAOExistFromParams(zNA: zNA): Promise<boolean> {
const found = this._params.find((param) => param.zNA === zNA);
const found = this.params.find((param) => param.zNA === zNA);
return Promise.resolve(found ? true : false);
}
}
Expand Down
Loading

0 comments on commit 31d378f

Please sign in to comment.