generated from proofoftom/buidler-waffle-typechain-quasar
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
128 changed files
with
10,266 additions
and
4,235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
16.14.2 | ||
18.4.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Common clr.fund utility functions used by contracts and vue-app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "@clrfund/common", | ||
"version": "0.0.1", | ||
"description": "Common utility functions used by clrfund scripts and app", | ||
"main": "src/index", | ||
"scripts": { | ||
"build": "tsc", | ||
"lint": "eslint 'src/**/*.ts'", | ||
"clean": "rm -rf build" | ||
}, | ||
"license": "GPL-3.0", | ||
"devDependencies": { | ||
"eslint": "^8.31.0", | ||
"typescript": "^4.9.3" | ||
}, | ||
"dependencies": { | ||
"@openzeppelin/merkle-tree": "^1.0.5", | ||
"ethers": "^5.7.2", | ||
"maci-crypto": "1.1.1", | ||
"maci-domainobjs": "1.1.1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/clrfund/monorepo.git" | ||
}, | ||
"author": "", | ||
"bugs": { | ||
"url": "https://github.com/clrfund/monorepo/issues" | ||
}, | ||
"homepage": "https://github.com/clrfund/monorepo#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { providers, utils } from 'ethers' | ||
|
||
export interface Block { | ||
blockNumber: number | ||
hash: string | ||
stateRoot: string | ||
} | ||
|
||
/* | ||
* get the block stateRoot using eth_getBlockByHash | ||
*/ | ||
export async function getBlock( | ||
blockNumber: number, | ||
provider: providers.JsonRpcProvider | ||
): Promise<Block> { | ||
const blockNumberHex = utils.hexValue(blockNumber) | ||
const blockParams = [blockNumberHex, false] | ||
const rawBlock = await provider.send('eth_getBlockByNumber', blockParams) | ||
return { blockNumber, hash: rawBlock.hash, stateRoot: rawBlock.stateRoot } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export * from './block' | ||
export * from './proof' | ||
export * from './merkle' | ||
export * from './ipfs' | ||
export * from './keypair' | ||
export * from './tally' | ||
export * from './utils' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { utils } from 'ethers' | ||
|
||
const IPFS_BASE_URL = 'https://ipfs.io' | ||
|
||
/** | ||
* Get the IPFS content given the IPFS hash | ||
* @param hash The IPFS hash | ||
* @param gatewayUrl The IPFS gateway url | ||
* @returns The IPFS content | ||
*/ | ||
export async function getIpfsContent( | ||
hash: string, | ||
gatewayUrl = IPFS_BASE_URL | ||
): Promise<any> { | ||
const url = `${gatewayUrl}/ipfs/${hash}` | ||
const result = utils.fetchJson(url) | ||
return result | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { StandardMerkleTree } from '@openzeppelin/merkle-tree' | ||
|
||
/** | ||
* Load users into a merkle tree | ||
* @param users Users to load | ||
* @returns user merkle tree | ||
*/ | ||
export function loadUserMerkleTree( | ||
users: string[] | ||
): StandardMerkleTree<string[]> { | ||
const tree = StandardMerkleTree.of( | ||
users.map((user) => [user.toLowerCase()]), | ||
['address'] | ||
) | ||
return tree | ||
} | ||
|
||
/** | ||
* Get the merkle proof for the user | ||
* @param userAccount User wallet address to get the proof for | ||
* @param userMerkleTree The merkle tree containing all approved users | ||
* @returns | ||
*/ | ||
export function getUserMerkleProof( | ||
userAccount: string, | ||
userMerkleTree: StandardMerkleTree<string[]> | ||
): string[] | null { | ||
try { | ||
return userMerkleTree.getProof([userAccount.toLowerCase()]) | ||
} catch (err) { | ||
console.log('userAccount', userAccount.toLowerCase()) | ||
console.log('getUserMerkleProof error', err) | ||
return null | ||
} | ||
} | ||
|
||
export { StandardMerkleTree } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { utils, providers } from 'ethers' | ||
|
||
/** | ||
* RLP encode the proof returned from eth_getProof | ||
* @param proof proof from the eth_getProof | ||
* @returns | ||
*/ | ||
export function rlpEncodeProof(proof: string[]) { | ||
const decodedProof = proof.map((node: string) => utils.RLP.decode(node)) | ||
|
||
return utils.RLP.encode(decodedProof) | ||
} | ||
|
||
/** | ||
* The storage key used in eth_getProof and eth_getStorageAt | ||
* @param account Account address | ||
* @param slotIndex Slot index of the balanceOf storage | ||
* @returns storage key used in the eth_getProof params | ||
*/ | ||
export function getStorageKey(account: string, slotIndex: number) { | ||
return utils.keccak256( | ||
utils.concat([ | ||
utils.hexZeroPad(account, 32), | ||
utils.hexZeroPad(utils.hexValue(slotIndex), 32), | ||
]) | ||
) | ||
} | ||
|
||
/** | ||
* Get proof from eth_getProof | ||
* @param params Parameter fro eth_getProof | ||
* @returns proof returned from eth_getProof | ||
*/ | ||
async function getProof( | ||
params: Array<string | string[]>, | ||
provider: providers.JsonRpcProvider | ||
): Promise<any> { | ||
try { | ||
const proof = await provider.send('eth_getProof', params) | ||
return proof | ||
} catch (err) { | ||
console.error( | ||
'Unable to get proof. Your node may not support eth_getProof. Try a different provider such as Infura', | ||
err | ||
) | ||
throw err | ||
} | ||
} | ||
/** | ||
* Get the storage proof | ||
* @param token Token contract address | ||
* @param blockHash The block hash to get the proof for | ||
* @param provider provider to connect to the node | ||
* @returns proof returned from eth_getProof | ||
*/ | ||
export async function getAccountProof( | ||
token: string, | ||
blockHash: string, | ||
provider: providers.JsonRpcProvider | ||
): Promise<any> { | ||
const params = [token, [], blockHash] | ||
return getProof(params, provider) | ||
} | ||
|
||
/** | ||
* Get the storage proof | ||
* @param token Token contract address | ||
* @param blockHash The block hash to get the storage proof for | ||
* @param userAccount User account to get the proof for | ||
* @param storageSlotIndex The storage index for the balanceOf storage | ||
* @param provider provider to connect to the node | ||
* @returns proof returned from eth_getProof | ||
*/ | ||
export async function getStorageProof( | ||
token: string, | ||
blockHash: string, | ||
userAccount: string, | ||
storageSlotIndex: number, | ||
provider: providers.JsonRpcProvider | ||
): Promise<any> { | ||
const storageKey = getStorageKey(userAccount, storageSlotIndex) | ||
|
||
const params = [token, [storageKey], blockHash] | ||
return getProof(params, provider) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.