-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Paid query flow (partial impl)
- Loading branch information
Showing
8 changed files
with
333 additions
and
50 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use ethers::signers::coins_bip39::English; | ||
use ethers::signers::MnemonicBuilder; | ||
use ethers::signers::Signer; | ||
use ethers::signers::Wallet; | ||
use ethers_core::k256::ecdsa::SigningKey; | ||
use ethers_core::types::Address; | ||
|
||
use crate::query_processor::SubgraphDeploymentID; | ||
|
||
// TODO: Lots of fields are commented out for now, but will be needed later. | ||
|
||
pub struct Allocation { | ||
pub id: Address, | ||
// pub status: AllocationStatus, | ||
pub subgraph_deployment: SubgraphDeploymentID, | ||
// pub indexer: Address, | ||
// pub allocated_tokens: U256, | ||
pub created_at_epoch: u64, | ||
// pub created_at_block_hash: String, | ||
// pub closed_at_epoch: u64, | ||
// pub closed_at_epoch_start_block_hash: Option<String>, | ||
// pub previous_epoch_start_block_hash: Option<String>, | ||
// pub closed_at_block_hash: String, | ||
// pub poi: Option<String>, | ||
// pub query_fee_rebates: U256, | ||
// pub query_fees_collected: U256, | ||
} | ||
|
||
// pub enum AllocationStatus { | ||
// Null, | ||
// Active, | ||
// Closed, | ||
// Finalized, | ||
// Claimed, | ||
// } | ||
|
||
pub fn derive_key_pair( | ||
indexer_mnemonic: &str, | ||
epoch: u64, | ||
deployment: &SubgraphDeploymentID, | ||
index: u64, | ||
) -> Wallet<SigningKey> { | ||
let mut derivation_path = format!("m/{}/", epoch); | ||
derivation_path.push_str( | ||
&deployment | ||
.ipfs_hash() | ||
.as_bytes() | ||
.iter() | ||
.map(|char| char.to_string()) | ||
.collect::<Vec<String>>() | ||
.join("/"), | ||
); | ||
derivation_path.push_str(format!("/{}", index).as_str()); | ||
|
||
MnemonicBuilder::<English>::default() | ||
.derivation_path(&derivation_path) | ||
.expect("Valid derivation path") | ||
.phrase(indexer_mnemonic) | ||
.build() | ||
.unwrap() | ||
} | ||
|
||
pub fn allocation_signer(indexer_mnemonic: &str, allocation: Allocation) -> Wallet<SigningKey> { | ||
// Guess the allocation index by enumerating all indexes in the | ||
// range [0, 100] and checking for a match | ||
for i in 0..100 { | ||
// The allocation was either created at the epoch it intended to or one | ||
// epoch later. So try both both. | ||
for created_at_epoch in [allocation.created_at_epoch, allocation.created_at_epoch - 1] { | ||
let allocation_wallet = derive_key_pair( | ||
indexer_mnemonic, | ||
created_at_epoch, | ||
&allocation.subgraph_deployment, | ||
i, | ||
); | ||
if allocation_wallet.address() == allocation.id { | ||
return allocation_wallet; | ||
} | ||
} | ||
} | ||
panic!("Could not find allocation signer") | ||
} | ||
|
||
// TODO: tests! |
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
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