From bf28c6a0684c9cb2c5b61d82e01b31777bdabf37 Mon Sep 17 00:00:00 2001 From: scx1332 Date: Wed, 22 May 2024 17:26:16 +0200 Subject: [PATCH] added polygon contract --- crates/erc20_payment_lib/src/contracts.rs | 69 +++++++++++------------ crates/erc20_payment_lib/src/eth.rs | 12 +++- crates/erc20_payment_lib/src/service.rs | 9 +-- 3 files changed, 43 insertions(+), 47 deletions(-) diff --git a/crates/erc20_payment_lib/src/contracts.rs b/crates/erc20_payment_lib/src/contracts.rs index db34de07..f98741a6 100644 --- a/crates/erc20_payment_lib/src/contracts.rs +++ b/crates/erc20_payment_lib/src/contracts.rs @@ -2,15 +2,15 @@ use lazy_static::lazy_static; use crate::err_custom_create; use crate::error::PaymentError; -use std::str::FromStr; use chrono::{DateTime, Utc}; +use std::str::FromStr; use web3::contract::tokens::Tokenize; use web3::contract::Contract; +use erc20_payment_lib_common::utils::datetime_from_u256_timestamp; use web3::transports::Http; use web3::types::{Address, H256, U256}; use web3::{ethabi, Transport, Web3}; -use erc20_payment_lib_common::utils::datetime_from_u256_timestamp; // todo remove DUMMY_RPC_PROVIDER and use ABI instead // todo change to once_cell @@ -45,7 +45,7 @@ pub fn prepare_contract_template(json_abi: &[u8]) -> Result, Paym Address::from_str("0x0000000000000000000000000000000000000000").unwrap(), json_abi, ) - .map_err(|err| err_custom_create!("Failed to create contract {err}"))?; + .map_err(|err| err_custom_create!("Failed to create contract {err}"))?; Ok(contract) } @@ -55,9 +55,9 @@ pub fn contract_encode( func: &str, params: P, ) -> Result, web3::ethabi::Error> - where - P: Tokenize, - T: Transport, +where + P: Tokenize, + T: Transport, { contract .abi() @@ -66,15 +66,15 @@ pub fn contract_encode( } pub fn encode_get_attestation(uid: H256) -> Result, web3::ethabi::Error> { - contract_encode(&EAS_CONTRACT_TEMPLATE, "getAttestation", (uid, )) + contract_encode(&EAS_CONTRACT_TEMPLATE, "getAttestation", (uid,)) } pub fn encode_get_schema(uid: H256) -> Result, web3::ethabi::Error> { - contract_encode(&SCHEMA_REGISTRY_TEMPLATE, "getSchema", (uid, )) + contract_encode(&SCHEMA_REGISTRY_TEMPLATE, "getSchema", (uid,)) } pub fn encode_erc20_balance_of(address: Address) -> Result, web3::ethabi::Error> { - contract_encode(&ERC20_CONTRACT_TEMPLATE, "balanceOf", (address, )) + contract_encode(&ERC20_CONTRACT_TEMPLATE, "balanceOf", (address,)) } pub fn encode_erc20_transfer( @@ -93,14 +93,14 @@ pub fn encode_erc20_allowance( /* - uint256 number; - uint256 timestamp; - uint256 difficulty; - uint256 gaslimit; - address coinbase; - bytes32 blockhash; - uint256 basefee; - */ + uint256 number; + uint256 timestamp; + uint256 difficulty; + uint256 gaslimit; + address coinbase; + bytes32 blockhash; + uint256 basefee; +*/ #[derive(Debug, Clone)] pub struct CallWithDetailsBlockInfo { pub block_number: u64, @@ -111,24 +111,20 @@ pub fn decode_call_with_details( bytes: &[u8], ) -> Result<(crate::contracts::CallWithDetailsBlockInfo, Vec), PaymentError> { let decoded = ethabi::decode( - &[ + &[ethabi::ParamType::Tuple(vec![ ethabi::ParamType::Tuple(vec![ - ethabi::ParamType::Tuple( - vec![ethabi::ParamType::Uint(256), - ethabi::ParamType::Uint(256), - ]), - ethabi::ParamType::Bytes - ]) - ], + ethabi::ParamType::Uint(256), + ethabi::ParamType::Uint(256), + ]), + ethabi::ParamType::Bytes, + ])], bytes, - ).map_err( - |err| err_custom_create!("Failed to decode call with details: {}", err), - )?; + ) + .map_err(|err| err_custom_create!("Failed to decode call with details: {}", err))?; let tuple_main = decoded[0].clone().into_tuple().unwrap(); let tuple_block_info = tuple_main[0].clone().into_tuple().unwrap(); - let number: U256 = tuple_block_info[0].clone().into_uint().unwrap(); let timestamp: U256 = tuple_block_info[1].clone().into_uint().unwrap(); @@ -136,14 +132,13 @@ pub fn decode_call_with_details( let block_details = CallWithDetailsBlockInfo { block_number: number.as_u64(), - block_datetime: datetime_from_u256_timestamp(timestamp).ok_or( - err_custom_create!("Failed to convert timestamp to datetime"), - )?, + block_datetime: datetime_from_u256_timestamp(timestamp).ok_or(err_custom_create!( + "Failed to convert timestamp to datetime" + ))?, }; Ok((block_details, call_result)) } - pub fn encode_call_with_details( call_target_address: Address, call_data: Vec, @@ -251,11 +246,11 @@ pub fn encode_multi_indirect_packed( } pub fn encode_close_deposit(deposit_id: U256) -> Result, web3::ethabi::Error> { - contract_encode(&LOCK_CONTRACT_TEMPLATE, "closeDeposit", (deposit_id, )) + contract_encode(&LOCK_CONTRACT_TEMPLATE, "closeDeposit", (deposit_id,)) } pub fn encode_terminate_deposit(nonce: u64) -> Result, web3::ethabi::Error> { - contract_encode(&LOCK_CONTRACT_TEMPLATE, "terminateDeposit", (nonce, )) + contract_encode(&LOCK_CONTRACT_TEMPLATE, "terminateDeposit", (nonce,)) } pub struct CreateDepositArgs { @@ -307,7 +302,7 @@ pub fn encode_payout_single_and_close( } pub fn encode_get_deposit_details(id: U256) -> Result, web3::ethabi::Error> { - contract_encode(&LOCK_CONTRACT_TEMPLATE, "getDeposit", (id, )) + contract_encode(&LOCK_CONTRACT_TEMPLATE, "getDeposit", (id,)) } pub fn encode_get_validate_deposit_signature() -> Result, web3::ethabi::Error> { @@ -319,7 +314,7 @@ pub fn encode_validate_contract( values: Vec, ) -> Result, web3::ethabi::Error> { #[allow(deprecated)] - let fun = ethabi::Function { + let fun = ethabi::Function { name: "validateDeposit".to_string(), inputs: params, outputs: vec![], diff --git a/crates/erc20_payment_lib/src/eth.rs b/crates/erc20_payment_lib/src/eth.rs index f8cb3792..f97b9d60 100644 --- a/crates/erc20_payment_lib/src/eth.rs +++ b/crates/erc20_payment_lib/src/eth.rs @@ -1,4 +1,8 @@ -use crate::contracts::{decode_call_with_details, encode_call_with_details, encode_erc20_allowance, encode_erc20_balance_of, encode_get_attestation, encode_get_deposit_details, encode_get_schema, encode_get_validate_deposit_signature, encode_validate_contract}; +use crate::contracts::{ + decode_call_with_details, encode_call_with_details, encode_erc20_allowance, + encode_erc20_balance_of, encode_get_attestation, encode_get_deposit_details, encode_get_schema, + encode_get_validate_deposit_signature, encode_validate_contract, +}; use crate::error::*; use crate::runtime::ValidateDepositResult; use crate::{err_create, err_custom_create, err_from}; @@ -518,7 +522,11 @@ pub async fn get_balance( let token_balance = U256::from_big_endian(&call_result); - log::error!("Token balance response: {:?} - token balance: {}", block_info, token_balance); + log::error!( + "Token balance response: {:?} - token balance: {}", + block_info, + token_balance + ); } }; diff --git a/crates/erc20_payment_lib/src/service.rs b/crates/erc20_payment_lib/src/service.rs index 69e5c547..d3929b93 100644 --- a/crates/erc20_payment_lib/src/service.rs +++ b/crates/erc20_payment_lib/src/service.rs @@ -140,16 +140,9 @@ pub async fn transaction_from_chain_and_into_db( .clone() .eth_call( CallRequest { - from: None, to: Some(glm_address), - gas: None, - gas_price: None, - value: None, data: Some(web3::types::Bytes::from(call_data)), - transaction_type: None, - access_list: None, - max_fee_per_gas: None, - max_priority_fee_per_gas: None, + ..Default::default() }, Some(web3::types::BlockId::Number(BlockNumber::Number( chain_tx_dao.block_number.into(),