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

Fixed DAO Creation #538

Merged
merged 1 commit into from
Apr 13, 2022
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
2 changes: 1 addition & 1 deletion packages/govern-console/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.23.1",
"eslint-plugin-react-hooks": "^4.2.0",
"ethers": "^5.4.1",
"ethers": "^5.5.0",
"graphql": "^15.5.0",
"immer": "^9.0.2",
"ipfs-core": "^0.6.1",
Expand Down
11 changes: 5 additions & 6 deletions packages/govern/internal/actions/RegisterToken.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CensusErc20Api } from 'dvote-js'
import { CensusErc20Api } from './lib/Token'
import { getPool } from './lib/Gateway'
import { ContractReceipt, Wallet, Signer } from 'ethers'
import { ContractReceipt, Signer, Wallet } from 'ethers'

/**
*
Expand All @@ -13,7 +13,8 @@ export const isTokenRegistered = async (
tokenAddress: string
): Promise<boolean> => {
const pool = await getPool(signer.provider)
return await CensusErc20Api.isRegistered(tokenAddress, pool)
return CensusErc20Api.getTokenInfo(tokenAddress, pool)
.then(tokenInfo => tokenInfo.isRegistered)
}

/**
Expand All @@ -34,7 +35,5 @@ export const registerToken = async (
return 'ALREADY_REGISTERED'
}

const result = CensusErc20Api.registerTokenAuto(tokenAddress, signer, pool)

return result
return CensusErc20Api.registerTokenAuto(tokenAddress, signer, pool)
}
2 changes: 1 addition & 1 deletion packages/govern/internal/actions/lib/Gateway.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { providers } from 'ethers'
import { BRIGE_CONFIG } from '../../configuration/ConfigDefaults'
import { GatewayPool } from 'dvote-js'
import { GatewayPool } from '@vocdoni/client'

export async function getPool(
provider?: providers.Provider
Expand Down
57 changes: 57 additions & 0 deletions packages/govern/internal/actions/lib/Token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { BigNumber } from "@ethersproject/bignumber"
import { ContractReceipt } from "@ethersproject/contracts"
import { Signer } from "@ethersproject/abstract-signer"
import { Wallet } from "@ethersproject/wallet"
import { GatewayPool } from '@vocdoni/client'
import { ERC20Proof } from "@vocdoni/storage-proofs-eth"

export namespace CensusErc20Api {
/** Finds the balance mapping position of the given ERC20 token address and attempts to register it on the blockchain */
export async function registerTokenAuto(tokenAddress: string, walletOrSigner: Wallet | Signer, gw: GatewayPool, customContractAddress?: string): Promise<ContractReceipt> {
const contractInstance = await gw.getTokenStorageProofInstance(walletOrSigner, customContractAddress)

const mapSlot = await CensusErc20Api.findBalanceMappingPosition(tokenAddress, await walletOrSigner.getAddress(), gw.provider)
if (mapSlot === null) throw new Error("The given token contract does not seem to have a defined mapping position for the holder balances")

const tx = await contractInstance.registerToken(tokenAddress, mapSlot)
// @ts-ignore
return tx.wait()
}

/** Associates the given balance mapping position to the given ERC20 token address */
export function registerToken(tokenAddress: string, balanceMappingPosition: number | BigNumber, walletOrSigner: Wallet | Signer, gw: GatewayPool, customContractAddress?: string) {
return gw.getTokenStorageProofInstance(walletOrSigner, customContractAddress)
.then((contractInstance) =>
contractInstance.registerToken(tokenAddress,
balanceMappingPosition
)
)
.then(tx => tx.wait())
}

export function getTokenInfo(tokenAddress: string, gw: GatewayPool, customContractAddress?: string): Promise<{ isRegistered: boolean, isVerified: boolean, balanceMappingPosition: number }> {
return gw.getTokenStorageProofInstance(null, customContractAddress)
.then((contractInstance) => contractInstance.tokens(tokenAddress))
.then((tokenDataTuple) => {
const balanceMappingPosition = BigNumber.isBigNumber(tokenDataTuple[2]) ?
tokenDataTuple[2].toNumber() : tokenDataTuple[2]

return {
isRegistered: tokenDataTuple[0],
isVerified: tokenDataTuple[1],
balanceMappingPosition
}
})
}

// Helpers

/**
* Attempts to find the index at which the holder balances are stored within the token contract.
* If the position cannot be found among the 50 first ones, `null` is returned.
*/
export function findBalanceMappingPosition(tokenAddress: string, holderAddress: string, provider) {
return ERC20Proof.findMapSlot(tokenAddress, holderAddress, provider)
}
}

4 changes: 3 additions & 1 deletion packages/govern/internal/configuration/ConfigDefaults.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IGatewayDiscoveryParameters } from 'dvote-js'
import { IGatewayDiscoveryParameters } from '@vocdoni/client'

type BrigeConfig = {
[key: string]: IGatewayDiscoveryParameters
Expand All @@ -14,10 +14,12 @@ export const BRIGE_CONFIG: BrigeConfig = {
networkId: 'rinkeby',
bootnodesContentUri: 'https://bootnodes.vocdoni.net/gateways.dev.json',
environment: 'dev',
numberOfGateways: 1
},
mainnet: {
networkId: 'mainnet',
bootnodesContentUri: 'https://bootnodes.vocdoni.net/gateways.json',
environment: 'prod',
numberOfGateways: 1
},
}
7 changes: 4 additions & 3 deletions packages/govern/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@
"rollup-plugin-node-resolve": "^5.2.0",
"ts-jest": "^26.4.2",
"tslint": "^6.1.3",
"typescript": "^4.0.5",
"typescript": "^4.4.3",
"uglify-es": "^3.3.9"
},
"dependencies": {
"@urql/core": "^1.13.1",
"dvote-js": "^1.9.14",
"ethers": "^5.4.1",
"@vocdoni/client": "^1.16.7",
"@vocdoni/storage-proofs-eth": "^0.4.1",
"ethers": "^5.5.0",
"graphql": "^15.4.0",
"graphql-tag": "^2.11.0",
"isomorphic-unfetch": "^3.1.0"
Expand Down
1 change: 1 addition & 0 deletions packages/govern/tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"compilerOptions": {
"removeComments": true,
"strictPropertyInitialization": false,
"allowSyntheticDefaultImports": true,
"sourceRoot": "./",
"outDir": "./dist/cjs",
"strict": false,
Expand Down
2 changes: 1 addition & 1 deletion packages/govern/tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"module": "es6",
"target": "es2018",
"removeComments": true,
"allowSyntheticDefaultImports": true,
"strictPropertyInitialization": false,
"strict": false,
"outDir": "./dist/esm",
"strict": false,
"sourceRoot": "./",
"inlineSources": true,
"types": ["jest"],
Expand Down
Loading