Move get_nft_contract function to ethereum::contracts module
Preparing for more contracts.
This commit is contained in:
parent
c4a0f12555
commit
dfc9902092
3 changed files with 61 additions and 36 deletions
|
@ -1,6 +1,17 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
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 ADAPTER: &str = "IAdapter";
|
||||||
pub const ERC721: &str = "IERC721Metadata";
|
pub const ERC721: &str = "IERC721Metadata";
|
||||||
|
|
||||||
|
@ -29,3 +40,43 @@ pub fn load_abi(
|
||||||
.to_string().as_bytes().to_vec();
|
.to_string().as_bytes().to_vec();
|
||||||
Ok(contract_abi)
|
Ok(contract_abi)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct ContractSet {
|
||||||
|
pub web3: Web3<Http>,
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub adapter: Contract<Http>,
|
||||||
|
|
||||||
|
pub collectible: Contract<Http>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_contracts(
|
||||||
|
config: &BlockchainConfig,
|
||||||
|
) -> Result<ContractSet, 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 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)
|
||||||
|
}
|
||||||
|
|
|
@ -20,40 +20,12 @@ use crate::models::posts::queries::{
|
||||||
update_post,
|
update_post,
|
||||||
get_token_waitlist,
|
get_token_waitlist,
|
||||||
};
|
};
|
||||||
use super::api::connect;
|
|
||||||
use super::contracts::{ADAPTER, ERC721, load_abi};
|
|
||||||
use super::errors::EthereumError;
|
use super::errors::EthereumError;
|
||||||
use super::signatures::{sign_contract_call, CallArgs, SignatureData};
|
use super::signatures::{sign_contract_call, CallArgs, SignatureData};
|
||||||
use super::utils::parse_address;
|
use super::utils::parse_address;
|
||||||
|
|
||||||
const TOKEN_WAIT_TIME: i64 = 10; // in minutes
|
const TOKEN_WAIT_TIME: i64 = 10; // in minutes
|
||||||
|
|
||||||
pub async fn get_nft_contract(
|
|
||||||
config: &BlockchainConfig,
|
|
||||||
) -> Result<(Web3<Http>, Contract<Http>), 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)]
|
#[allow(dead_code)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct TokenTransfer {
|
struct TokenTransfer {
|
||||||
|
@ -65,7 +37,7 @@ struct TokenTransfer {
|
||||||
|
|
||||||
/// Finds posts awaiting tokenization
|
/// Finds posts awaiting tokenization
|
||||||
/// and looks for corresponding Mint events
|
/// and looks for corresponding Mint events
|
||||||
pub async fn process_events(
|
pub async fn process_nft_events(
|
||||||
web3: &Web3<Http>,
|
web3: &Web3<Http>,
|
||||||
contract: &Contract<Http>,
|
contract: &Contract<Http>,
|
||||||
db_pool: &Pool,
|
db_pool: &Pool,
|
||||||
|
|
|
@ -6,14 +6,15 @@ use uuid::Uuid;
|
||||||
|
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::database::Pool;
|
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) -> () {
|
pub fn run(config: Config, db_pool: Pool) -> () {
|
||||||
actix_rt::spawn(async move {
|
actix_rt::spawn(async move {
|
||||||
let mut interval = actix_rt::time::interval(Duration::from_secs(30));
|
let mut interval = actix_rt::time::interval(Duration::from_secs(30));
|
||||||
let web3_contract = if let Some(blockchain_config) = &config.blockchain {
|
let maybe_contract_set = if let Some(blockchain_config) = &config.blockchain {
|
||||||
// Verify config and create contract interface
|
// Create blockchain interface
|
||||||
get_nft_contract(blockchain_config).await
|
get_contracts(blockchain_config).await
|
||||||
.map_err(|err| log::error!("{}", err))
|
.map_err(|err| log::error!("{}", err))
|
||||||
.ok()
|
.ok()
|
||||||
} else {
|
} else {
|
||||||
|
@ -23,10 +24,11 @@ pub fn run(config: Config, db_pool: Pool) -> () {
|
||||||
loop {
|
loop {
|
||||||
interval.tick().await;
|
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
|
// Monitor events only if ethereum integration is enabled
|
||||||
process_events(
|
process_nft_events(
|
||||||
web3, contract,
|
&contract_set.web3,
|
||||||
|
&contract_set.collectible,
|
||||||
&db_pool,
|
&db_pool,
|
||||||
&mut token_waitlist_map,
|
&mut token_waitlist_map,
|
||||||
).await.unwrap_or_else(|err| {
|
).await.unwrap_or_else(|err| {
|
||||||
|
|
Loading…
Reference in a new issue