|
@@ -1,19 +1,33 @@
|
|
|
-use ethers::contract::abigen;
|
|
|
|
|
-use ethers::core::types::Address;
|
|
|
|
|
-use ethers::middleware::SignerMiddleware;
|
|
|
|
|
-use ethers::providers::Http;
|
|
|
|
|
-use ethers::providers::Middleware;
|
|
|
|
|
-use ethers::providers::Provider;
|
|
|
|
|
-use ethers::signers::LocalWallet;
|
|
|
|
|
-use ethers::signers::Signer;
|
|
|
|
|
-use ethers::abi::RawLog;
|
|
|
|
|
-use ethers::contract::EthLogDecode;
|
|
|
|
|
-use std::error::Error;
|
|
|
|
|
-use std::sync::Arc;
|
|
|
|
|
-use crate::config::EthereumOptions;
|
|
|
|
|
-use anyhow::anyhow;
|
|
|
|
|
-use sha3::Keccak256;
|
|
|
|
|
-use sha3::Digest;
|
|
|
|
|
|
|
+use {
|
|
|
|
|
+ crate::config::EthereumOptions,
|
|
|
|
|
+ anyhow::anyhow,
|
|
|
|
|
+ ethers::{
|
|
|
|
|
+ abi::RawLog,
|
|
|
|
|
+ contract::{
|
|
|
|
|
+ abigen,
|
|
|
|
|
+ EthLogDecode,
|
|
|
|
|
+ },
|
|
|
|
|
+ core::types::Address,
|
|
|
|
|
+ middleware::SignerMiddleware,
|
|
|
|
|
+ providers::{
|
|
|
|
|
+ Http,
|
|
|
|
|
+ Middleware,
|
|
|
|
|
+ Provider,
|
|
|
|
|
+ },
|
|
|
|
|
+ signers::{
|
|
|
|
|
+ LocalWallet,
|
|
|
|
|
+ Signer,
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ sha3::{
|
|
|
|
|
+ Digest,
|
|
|
|
|
+ Keccak256,
|
|
|
|
|
+ },
|
|
|
|
|
+ std::{
|
|
|
|
|
+ error::Error,
|
|
|
|
|
+ sync::Arc,
|
|
|
|
|
+ },
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
// TODO: Programatically generate this so we don't have to keep committed ABI in sync with the
|
|
// TODO: Programatically generate this so we don't have to keep committed ABI in sync with the
|
|
|
// contract in the same repo.
|
|
// contract in the same repo.
|
|
@@ -22,20 +36,28 @@ abigen!(PythRandom, "src/abi.json");
|
|
|
pub type PythContract = PythRandom<SignerMiddleware<Provider<Http>, LocalWallet>>;
|
|
pub type PythContract = PythRandom<SignerMiddleware<Provider<Http>, LocalWallet>>;
|
|
|
|
|
|
|
|
impl PythContract {
|
|
impl PythContract {
|
|
|
-
|
|
|
|
|
pub async fn from_opts(opts: &EthereumOptions) -> Result<PythContract, Box<dyn Error>> {
|
|
pub async fn from_opts(opts: &EthereumOptions) -> Result<PythContract, Box<dyn Error>> {
|
|
|
let provider = Provider::<Http>::try_from(&opts.geth_rpc_addr)?;
|
|
let provider = Provider::<Http>::try_from(&opts.geth_rpc_addr)?;
|
|
|
let chain_id = provider.get_chainid().await?;
|
|
let chain_id = provider.get_chainid().await?;
|
|
|
- let wallet__ = opts.private_key.clone().ok_or(anyhow!("No private key specified"))?.parse::<LocalWallet>()?.with_chain_id(chain_id.as_u64());
|
|
|
|
|
|
|
+ let wallet__ = opts
|
|
|
|
|
+ .private_key
|
|
|
|
|
+ .clone()
|
|
|
|
|
+ .ok_or(anyhow!("No private key specified"))?
|
|
|
|
|
+ .parse::<LocalWallet>()?
|
|
|
|
|
+ .with_chain_id(chain_id.as_u64());
|
|
|
|
|
|
|
|
Ok(PythRandom::new(
|
|
Ok(PythRandom::new(
|
|
|
opts.contract_addr.parse::<Address>()?,
|
|
opts.contract_addr.parse::<Address>()?,
|
|
|
Arc::new(SignerMiddleware::new(provider, wallet__)),
|
|
Arc::new(SignerMiddleware::new(provider, wallet__)),
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ ))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- pub async fn request_wrapper(&self, provider: &Address, user_randomness: &[u8; 32], use_blockhash: bool) -> Result<u64, Box<dyn Error>> {
|
|
|
|
|
|
|
+ pub async fn request_wrapper(
|
|
|
|
|
+ &self,
|
|
|
|
|
+ provider: &Address,
|
|
|
|
|
+ user_randomness: &[u8; 32],
|
|
|
|
|
+ use_blockhash: bool,
|
|
|
|
|
+ ) -> Result<u64, Box<dyn Error>> {
|
|
|
let hashed_randomness: [u8; 32] = Keccak256::digest(user_randomness).into();
|
|
let hashed_randomness: [u8; 32] = Keccak256::digest(user_randomness).into();
|
|
|
|
|
|
|
|
if let Some(r) = self
|
|
if let Some(r) = self
|
|
@@ -57,7 +79,13 @@ impl PythContract {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- pub async fn reveal_wrapper(&self, provider: &Address, sequence_number: u64, user_randomness: &[u8; 32], provider_randomness: &[u8; 32]) -> Result<[u8;32], Box<dyn Error>> {
|
|
|
|
|
|
|
+ pub async fn reveal_wrapper(
|
|
|
|
|
+ &self,
|
|
|
|
|
+ provider: &Address,
|
|
|
|
|
+ sequence_number: u64,
|
|
|
|
|
+ user_randomness: &[u8; 32],
|
|
|
|
|
+ provider_randomness: &[u8; 32],
|
|
|
|
|
+ ) -> Result<[u8; 32], Box<dyn Error>> {
|
|
|
if let Some(r) = self
|
|
if let Some(r) = self
|
|
|
.reveal(
|
|
.reveal(
|
|
|
*provider,
|
|
*provider,
|
|
@@ -79,6 +107,5 @@ impl PythContract {
|
|
|
} else {
|
|
} else {
|
|
|
Err(anyhow!("Request failed").into())
|
|
Err(anyhow!("Request failed").into())
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}
|