-
Notifications
You must be signed in to change notification settings - Fork 19
Add fields for syncedBlock and leaves in EVMChainStorage class #106
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
Changes from all commits
7d3be13
781a70b
68cc0f1
9aacadb
1c6f9d1
f5135d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { MixerSize } from '@webb-dapp/react-environment'; | ||
|
|
||
| export class EvmChainStorage { | ||
| constructor( | ||
| public contractsInfo: { | ||
| address: string; | ||
| size: number; | ||
| symbol: string; | ||
| syncedBlock: number; | ||
| leaves: string[]; | ||
| }[] | ||
| ) {} | ||
|
|
||
| get mixerSize(): MixerSize[] { | ||
| return this.contractsInfo.map((contract, index) => { | ||
| return { | ||
| id: contract.address, | ||
| title: `${contract.size} ${contract.symbol}`, | ||
| }; | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { EvmChainStorage } from '@webb-dapp/apps/configs/storages/evm-chain-storage.interface'; | ||
| import { StorageHandler } from '@webb-dapp/utils'; | ||
| import { EVMStorage } from '@webb-dapp/react-environment/api-providers/web3'; | ||
|
|
||
| const rinkebyStore = new EvmChainStorage([ | ||
| { | ||
| size: 0.1, | ||
| address: '0x876eCe69618e8E8dd743250B036785813824D2D7', | ||
| symbol: 'ETH', | ||
| syncedBlock: 120000, // should be hardcoded to deployed block number | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still don't know what syncedBlock means. This should err on the side of being as verbose as needed. Is this the latest synced block? If so, lets call it, I think we should also add another parameter for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree the field can be named something more descriptive like I think that these |
||
| leaves: [], | ||
| }, | ||
| ]); | ||
|
|
||
| const ethMainNet = new EvmChainStorage([ | ||
| { | ||
| size: 0.1, | ||
| address: '0x876eCe69618e8E8dd743250B036785813824D2D7', | ||
| symbol: 'ETH', | ||
| syncedBlock: 1, | ||
| leaves: [], | ||
| }, | ||
| ]); | ||
|
|
||
| const beresheet = new EvmChainStorage([ | ||
| { | ||
| size: 10, | ||
| address: '0x5f771fc87F87DB48C9fB11aA228D833226580689', | ||
| symbol: 'tEDG', | ||
| syncedBlock: 1, | ||
| leaves: [], | ||
| }, | ||
| { | ||
| size: 100, | ||
| address: '0x2ee2e51cab1561E4482cacc8Be8b46CE61E46991', | ||
| symbol: 'tEDG', | ||
| syncedBlock: 1, | ||
| leaves: [], | ||
| }, | ||
| { | ||
| size: 1000, | ||
| address: '0x5696b4AfBc169454d7FA26e0a41828d445CFae20', | ||
| symbol: 'tEDG', | ||
| syncedBlock: 1, | ||
| leaves: [], | ||
| }, | ||
| { | ||
| size: 10000, | ||
| address: '0x626FEc5Ffa7Bf1EE8CEd7daBdE545630473E3ABb', | ||
| symbol: 'tEDG', | ||
| syncedBlock: 1, | ||
| leaves: [], | ||
| }, | ||
| ]); | ||
|
|
||
| const defaultHandler: Omit<StorageHandler<EVMStorage>, 'inner'> = { | ||
| async fetch(key: string) { | ||
| const data = localStorage.getItem(key); | ||
| if (!data) { | ||
| return { | ||
| nativeAnchor: new EvmChainStorage([]), | ||
| }; | ||
| } | ||
| const address = JSON.parse(data).contractsAddresses; | ||
|
drewstone marked this conversation as resolved.
|
||
| return { | ||
| nativeAnchor: new EvmChainStorage(address), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this currently a nativeAnchor because the only mixers we currently have supported in the dApp are native? I suppose we'll want to plan accordingly for ERC20 supported mixers. Might be worth updating this now, thoughts? |
||
| }; | ||
| }, | ||
| async commit(key: string, data) { | ||
| localStorage.setItem(key, JSON.stringify(data.contractsAddresses)); | ||
| }, | ||
| }; | ||
|
|
||
| export const rinkebyStorage: StorageHandler<EVMStorage> = { | ||
| ...defaultHandler, | ||
| inner: { | ||
| nativeAnchor: rinkebyStore, | ||
| }, | ||
| }; | ||
|
|
||
| export const mainStorage: StorageHandler<EVMStorage> = { | ||
| ...defaultHandler, | ||
| inner: { | ||
| nativeAnchor: ethMainNet, | ||
| }, | ||
| }; | ||
|
|
||
| export const beresheetStorage: StorageHandler<EVMStorage> = { | ||
| ...defaultHandler, | ||
| inner: { | ||
| nativeAnchor: beresheet, | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,14 +30,20 @@ export class AnchorContract { | |
| return this._contract.nextIndex(); | ||
| } | ||
|
|
||
| get denomination() { | ||
| return this._contract.denomination(); | ||
| } | ||
|
|
||
| get inner() { | ||
| return this._contract; | ||
| } | ||
|
|
||
| async createDeposit(): Promise<{ note: EvmNote; deposit: Deposit }> { | ||
| async createDeposit(assetSymbol: string): Promise<{ note: EvmNote; deposit: Deposit }> { | ||
| const deposit = createDeposit(); | ||
| const chainId = await this.signer.getChainId(); | ||
| const note = new EvmNote('eth', 0.1, chainId, deposit.preimage); | ||
| const depositSizeBN = await this.denomination; | ||
| const depositSize = Number.parseFloat(utils.fromWei(depositSizeBN.toString(), "ether")); | ||
| const note = new EvmNote(assetSymbol, depositSize, chainId, deposit.preimage); | ||
| return { | ||
| note, | ||
| deposit, | ||
|
|
@@ -48,8 +54,9 @@ export class AnchorContract { | |
| const overrides = { | ||
| gasLimit: 6000000, | ||
| gasPrice: utils.toWei('1', 'gwei'), | ||
| value: '0x16345785D8A0000', | ||
| value: await this.denomination, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I don't know much about getters but why are we awaiting this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes a call to the RPC to read the denomination from the contract deployed on the evm. We need to wait for the RPC response.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome, so this is mostly to generalise over Substrate chains and EVM, since we get denomination info often asynchronously. Good stuff! |
||
| }; | ||
| console.log(commitment); | ||
| const filters = await this._contract.filters.Deposit(commitment, null, null); | ||
| this._contract.once(filters, (commitment, insertedIndex, timestamp) => { | ||
| onComplete?.([commitment, insertedIndex, timestamp]); | ||
|
|
@@ -60,17 +67,47 @@ export class AnchorContract { | |
|
|
||
| private async getDepositEvents(commitment: string | null = null) { | ||
| const filter = this._contract.filters.Deposit(commitment, null, null); | ||
| const logs = await this.web3Provider.getLogs({ | ||
| fromBlock: 0, | ||
| toBlock: 'latest', | ||
| ...filter, | ||
| }); | ||
| const currentBlock = await this.web3Provider.getBlockNumber(); | ||
|
|
||
| // Look in the localStorage for this mixer (should be indexed under address) | ||
| // and get the syncedBlock / leaves that are present in the storage. | ||
| // Then query the blocks after the syncedBlock to update the leaves. | ||
|
|
||
| const startingBlock = 1; // Read starting block from cached storage, syncedBlock | ||
| var logs = []; // variable that holds newly fetched logs | ||
|
|
||
| try { | ||
| logs = await this.web3Provider.getLogs({ | ||
| fromBlock: startingBlock, | ||
| toBlock: 'latest', | ||
| ...filter, | ||
| }); | ||
| } catch (e) { | ||
| console.log(e); | ||
|
|
||
| // If there is a timeout, query the logs in block increments. | ||
| if (e.code == -32603) { | ||
| for (var i=startingBlock; i < currentBlock; i+= 1000) | ||
| { | ||
| logs = [...logs, ...(await this.web3Provider.getLogs({ | ||
| fromBlock: i, | ||
| toBlock: (currentBlock - i > 1000) ? i + 1000 : currentBlock, | ||
| ...filter, | ||
| }))]; | ||
| } | ||
| } else { | ||
| throw e; | ||
| } | ||
| } | ||
| console.log(logs); | ||
| return logs.map((log) => this._contract.interface.parseLog(log)); | ||
| } | ||
|
|
||
| private async generateSnarkProof(deposit: Deposit) { | ||
| // const { path_elements, path_index, root } = await this.generateMerkleProof(deposit); | ||
| // After getting the events, we should parse out and sort the commitments (leaves) and | ||
| // append to the leaves localStorage for this mixer. | ||
| // Dynamically update the storage at _this.contract.address, querying the appropriate | ||
| // EVMChainStorage key of 'main', 'rinkeby', etc. using this.signer.getChainId() and | ||
| // WebbWeb3Provider.storageName(chainId); | ||
|
|
||
| return logs.map((log) => this._contract.interface.parseLog(log)); | ||
| } | ||
|
|
||
| async generateMerkleProof(deposit: Deposit) { | ||
|
|
@@ -105,7 +142,6 @@ export class AnchorContract { | |
| let proving_key = require('../circuits/withdraw_proving_key.bin'); | ||
| proving_key = await fetch(proving_key); | ||
| proving_key = await proving_key.arrayBuffer(); | ||
| const bigInt = snarkjs.bigInt; | ||
|
|
||
| const input = { | ||
| // public | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this class should be renamed to
EvmChainMixerInfo. This is because most of the fields inside of this class should not be dealing with storage.