Skip to content

Commit

Permalink
added polygon contract
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed May 22, 2024
1 parent efd49cb commit bf28c6a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 47 deletions.
69 changes: 32 additions & 37 deletions crates/erc20_payment_lib/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -45,7 +45,7 @@ pub fn prepare_contract_template(json_abi: &[u8]) -> Result<Contract<Http>, 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)
}
Expand All @@ -55,9 +55,9 @@ pub fn contract_encode<P, T>(
func: &str,
params: P,
) -> Result<Vec<u8>, web3::ethabi::Error>
where
P: Tokenize,
T: Transport,
where
P: Tokenize,
T: Transport,
{
contract
.abi()
Expand All @@ -66,15 +66,15 @@ pub fn contract_encode<P, T>(
}

pub fn encode_get_attestation(uid: H256) -> Result<Vec<u8>, 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<Vec<u8>, 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<Vec<u8>, web3::ethabi::Error> {
contract_encode(&ERC20_CONTRACT_TEMPLATE, "balanceOf", (address, ))
contract_encode(&ERC20_CONTRACT_TEMPLATE, "balanceOf", (address,))
}

pub fn encode_erc20_transfer(
Expand All @@ -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,
Expand All @@ -111,39 +111,34 @@ pub fn decode_call_with_details(
bytes: &[u8],
) -> Result<(crate::contracts::CallWithDetailsBlockInfo, Vec<u8>), 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();

let call_result = tuple_main[1].clone().into_bytes().unwrap();

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<u8>,
Expand Down Expand Up @@ -251,11 +246,11 @@ pub fn encode_multi_indirect_packed(
}

pub fn encode_close_deposit(deposit_id: U256) -> Result<Vec<u8>, 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<Vec<u8>, web3::ethabi::Error> {
contract_encode(&LOCK_CONTRACT_TEMPLATE, "terminateDeposit", (nonce, ))
contract_encode(&LOCK_CONTRACT_TEMPLATE, "terminateDeposit", (nonce,))
}

pub struct CreateDepositArgs {
Expand Down Expand Up @@ -307,7 +302,7 @@ pub fn encode_payout_single_and_close(
}

pub fn encode_get_deposit_details(id: U256) -> Result<Vec<u8>, 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<Vec<u8>, web3::ethabi::Error> {
Expand All @@ -319,7 +314,7 @@ pub fn encode_validate_contract(
values: Vec<ethabi::Token>,
) -> Result<Vec<u8>, web3::ethabi::Error> {
#[allow(deprecated)]
let fun = ethabi::Function {
let fun = ethabi::Function {
name: "validateDeposit".to_string(),
inputs: params,
outputs: vec![],
Expand Down
12 changes: 10 additions & 2 deletions crates/erc20_payment_lib/src/eth.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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
);
}
};

Expand Down
9 changes: 1 addition & 8 deletions crates/erc20_payment_lib/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit bf28c6a

Please sign in to comment.