From dfc990209293b0c2b87a021a99faf8ec8813bf2a Mon Sep 17 00:00:00 2001 From: silverpill Date: Sun, 30 Jan 2022 16:18:05 +0000 Subject: [PATCH] Move get_nft_contract function to ethereum::contracts module Preparing for more contracts. --- src/ethereum/contracts.rs | 51 +++++++++++++++++++++++++++++++++++++++ src/ethereum/nft.rs | 30 +---------------------- src/scheduler.rs | 16 ++++++------ 3 files changed, 61 insertions(+), 36 deletions(-) diff --git a/src/ethereum/contracts.rs b/src/ethereum/contracts.rs index e508cd8..e2b1780 100644 --- a/src/ethereum/contracts.rs +++ b/src/ethereum/contracts.rs @@ -1,6 +1,17 @@ use std::fs; use std::path::Path; +use web3::{ + api::Web3, + contract::{Contract, Options}, + transports::Http, +}; + +use crate::config::BlockchainConfig; +use super::api::connect; +use super::errors::EthereumError; +use super::utils::parse_address; + pub const ADAPTER: &str = "IAdapter"; pub const ERC721: &str = "IERC721Metadata"; @@ -29,3 +40,43 @@ pub fn load_abi( .to_string().as_bytes().to_vec(); Ok(contract_abi) } + +pub struct ContractSet { + pub web3: Web3, + + #[allow(dead_code)] + pub adapter: Contract, + + pub collectible: Contract, +} + +pub async fn get_contracts( + config: &BlockchainConfig, +) -> Result { + let web3 = connect(&config.api_url)?; + let adapter_abi = load_abi(&config.contract_dir, ADAPTER)?; + let adapter_address = parse_address(&config.contract_address)?; + let adapter = Contract::from_json( + web3.eth(), + adapter_address, + &adapter_abi, + )?; + + let collectible_address = adapter.query( + "collectible", + (), None, Options::default(), None, + ).await?; + let collectibe_abi = load_abi(&config.contract_dir, ERC721)?; + let collectible = Contract::from_json( + web3.eth(), + collectible_address, + &collectibe_abi, + )?; + log::info!("collectible item contract address is {:?}", collectible.address()); + let contract_set = ContractSet { + web3, + adapter, + collectible, + }; + Ok(contract_set) +} diff --git a/src/ethereum/nft.rs b/src/ethereum/nft.rs index 4492e8f..81ccb5c 100644 --- a/src/ethereum/nft.rs +++ b/src/ethereum/nft.rs @@ -20,40 +20,12 @@ use crate::models::posts::queries::{ update_post, get_token_waitlist, }; -use super::api::connect; -use super::contracts::{ADAPTER, ERC721, load_abi}; use super::errors::EthereumError; use super::signatures::{sign_contract_call, CallArgs, SignatureData}; use super::utils::parse_address; const TOKEN_WAIT_TIME: i64 = 10; // in minutes -pub async fn get_nft_contract( - config: &BlockchainConfig, -) -> Result<(Web3, Contract), EthereumError> { - let web3 = connect(&config.api_url)?; - let adapter_abi = load_abi(&config.contract_dir, ADAPTER)?; - let adapter_address = parse_address(&config.contract_address)?; - let adapter = Contract::from_json( - web3.eth(), - adapter_address, - &adapter_abi, - )?; - - let token_address = adapter.query( - "collectible", - (), None, Options::default(), None, - ).await?; - let token_abi = load_abi(&config.contract_dir, ERC721)?; - let token = Contract::from_json( - web3.eth(), - token_address, - &token_abi, - )?; - log::info!("NFT contract address is {:?}", token.address()); - Ok((web3, token)) -} - #[allow(dead_code)] #[derive(Debug)] struct TokenTransfer { @@ -65,7 +37,7 @@ struct TokenTransfer { /// Finds posts awaiting tokenization /// and looks for corresponding Mint events -pub async fn process_events( +pub async fn process_nft_events( web3: &Web3, contract: &Contract, db_pool: &Pool, diff --git a/src/scheduler.rs b/src/scheduler.rs index d1ff0bf..e53d5fd 100644 --- a/src/scheduler.rs +++ b/src/scheduler.rs @@ -6,14 +6,15 @@ use uuid::Uuid; use crate::config::Config; use crate::database::Pool; -use crate::ethereum::nft::{get_nft_contract, process_events}; +use crate::ethereum::contracts::get_contracts; +use crate::ethereum::nft::process_nft_events; pub fn run(config: Config, db_pool: Pool) -> () { actix_rt::spawn(async move { let mut interval = actix_rt::time::interval(Duration::from_secs(30)); - let web3_contract = if let Some(blockchain_config) = &config.blockchain { - // Verify config and create contract interface - get_nft_contract(blockchain_config).await + let maybe_contract_set = if let Some(blockchain_config) = &config.blockchain { + // Create blockchain interface + get_contracts(blockchain_config).await .map_err(|err| log::error!("{}", err)) .ok() } else { @@ -23,10 +24,11 @@ pub fn run(config: Config, db_pool: Pool) -> () { loop { interval.tick().await; - if let Some((web3, contract)) = web3_contract.as_ref() { + if let Some(contract_set) = maybe_contract_set.as_ref() { // Monitor events only if ethereum integration is enabled - process_events( - web3, contract, + process_nft_events( + &contract_set.web3, + &contract_set.collectible, &db_pool, &mut token_waitlist_map, ).await.unwrap_or_else(|err| {