Move common code from ethereum::nft to separate modules

This commit is contained in:
silverpill 2021-10-21 16:51:01 +00:00
parent 324ff20480
commit d98e86a93e
6 changed files with 80 additions and 62 deletions

30
src/ethereum/contracts.rs Normal file
View file

@ -0,0 +1,30 @@
use std::fs;
use std::path::PathBuf;
pub const COLLECTIBLE: &str = "Collectible";
pub const MANAGER: &str = "Manager";
#[derive(thiserror::Error, Debug)]
pub enum ArtifactError {
#[error("io error")]
IoError(#[from] std::io::Error),
#[error("json error")]
JsonError(#[from] serde_json::Error),
#[error("key error")]
KeyError,
}
pub fn load_abi(
contract_dir: &PathBuf,
contract_name: &str,
) -> Result<Vec<u8>, ArtifactError> {
let contract_artifact_path = contract_dir.join(format!("{}.json", contract_name));
let contract_artifact = fs::read_to_string(contract_artifact_path)?;
let contract_artifact_value: serde_json::Value = serde_json::from_str(&contract_artifact)?;
let contract_abi = contract_artifact_value.get("abi")
.ok_or(ArtifactError::KeyError)?
.to_string().as_bytes().to_vec();
Ok(contract_abi)
}

42
src/ethereum/errors.rs Normal file
View file

@ -0,0 +1,42 @@
use crate::errors::DatabaseError;
use super::contracts::ArtifactError;
use super::utils::{AddressError, SignatureError};
#[derive(thiserror::Error, Debug)]
pub enum EthereumError {
#[error("io error")]
IoError(#[from] std::io::Error),
#[error("json error")]
JsonError(#[from] serde_json::Error),
#[error("invalid address")]
InvalidAddress(#[from] AddressError),
#[error(transparent)]
Web3Error(#[from] web3::Error),
#[error("artifact error")]
ArtifactError(#[from] ArtifactError),
#[error("abi error")]
AbiError(#[from] web3::ethabi::Error),
#[error("contract error")]
ContractError(#[from] web3::contract::Error),
#[error("improprely configured")]
ImproperlyConfigured,
#[error("data conversion error")]
ConversionError,
#[error("token uri parsing error")]
TokenUriParsingError,
#[error(transparent)]
DatabaseError(#[from] DatabaseError),
#[error("signature error")]
SigError(#[from] SignatureError),
}

View file

@ -2,7 +2,8 @@ use web3::contract::{Contract, Options};
use crate::config::Config;
use super::api::connect;
use super::nft::{MANAGER, load_abi, EthereumError};
use super::contracts::{MANAGER, load_abi};
use super::errors::EthereumError;
use super::utils::parse_address;
pub async fn is_allowed_user(

View file

@ -1,4 +1,6 @@
mod api;
pub mod contracts;
mod errors;
pub mod gate;
pub mod nft;
pub mod utils;

View file

@ -1,7 +1,5 @@
use std::collections::HashMap;
use std::convert::TryInto;
use std::fs;
use std::path::PathBuf;
use chrono::{DateTime, Utc};
use uuid::Uuid;
@ -23,67 +21,12 @@ use crate::models::posts::queries::{
get_token_waitlist,
};
use super::api::connect;
use super::utils::{
parse_address, sign_message,
AddressError, SignatureData, SignatureError,
};
use super::contracts::{MANAGER, COLLECTIBLE, load_abi};
use super::errors::EthereumError;
use super::utils::{parse_address, sign_message, SignatureData};
pub const COLLECTIBLE: &str = "Collectible";
pub const MANAGER: &str = "Manager";
const TOKEN_WAIT_TIME: i64 = 10; // in minutes
#[derive(thiserror::Error, Debug)]
pub enum EthereumError {
#[error("io error")]
IoError(#[from] std::io::Error),
#[error("json error")]
JsonError(#[from] serde_json::Error),
#[error("invalid address")]
InvalidAddress(#[from] AddressError),
#[error(transparent)]
Web3Error(#[from] web3::Error),
#[error("artifact error")]
ArtifactError,
#[error("abi error")]
AbiError(#[from] web3::ethabi::Error),
#[error("contract error")]
ContractError(#[from] web3::contract::Error),
#[error("improprely configured")]
ImproperlyConfigured,
#[error("data conversion error")]
ConversionError,
#[error("token uri parsing error")]
TokenUriParsingError,
#[error(transparent)]
DatabaseError(#[from] DatabaseError),
#[error("signature error")]
SigError(#[from] SignatureError),
}
pub fn load_abi(
contract_dir: &PathBuf,
contract_name: &str,
) -> Result<Vec<u8>, EthereumError> {
let contract_artifact_path = contract_dir.join(format!("{}.json", contract_name));
let contract_artifact = fs::read_to_string(contract_artifact_path)?;
let contract_artifact_value: serde_json::Value = serde_json::from_str(&contract_artifact)?;
let contract_abi = contract_artifact_value.get("abi")
.ok_or(EthereumError::ArtifactError)?
.to_string().as_bytes().to_vec();
Ok(contract_abi)
}
pub async fn get_nft_contract(
config: &Config,
) -> Result<(Web3<Http>, Contract<Http>), EthereumError> {

View file

@ -1,7 +1,7 @@
use serde::Serialize;
use crate::config::Config;
use crate::ethereum::nft::MANAGER;
use crate::ethereum::contracts::MANAGER;
#[derive(Serialize)]
pub struct Instance {