Skip to content

Commit

Permalink
Merge pull request #139 from bitfinity-network/more_eth_methods
Browse files Browse the repository at this point in the history
Add eth_getCode method
  • Loading branch information
F3kilo authored Mar 6, 2024
2 parents bb97ffc + 5274955 commit 881d3dd
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 179 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ include = ["src/**/*", "LICENSE", "README.md"]
license = "MIT"
name = "bitfinity-evm-sdk"
repository = "https://github.com/bitfinity-network/bitfinity-evm-sdk"
version = "0.13.0"
version = "0.14.0"

[workspace.dependencies]
alloy-primitives = { version = "0.6", default-feures = false }
Expand Down
31 changes: 29 additions & 2 deletions src/ethereum-json-rpc-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use anyhow::Context;
use did::certified::CertifiedResult;
use did::transaction::StorableExecutionResult;
use ethers_core::types::{
Block, BlockNumber, Log, Transaction, TransactionReceipt, H160, H256, U256, U64,
Block, BlockNumber, Log, Transaction, TransactionReceipt, TransactionRequest, H160, H256, U256,
U64,
};
use itertools::Itertools;
use jsonrpc_core::{Call, Id, MethodCall, Output, Params, Request, Response, Version};
Expand All @@ -23,10 +24,12 @@ pub mod http_outcall;

const ETH_CHAIN_ID_METHOD: &str = "eth_chainId";
const ETH_GET_BALANCE_METHOD: &str = "eth_getBalance";
const ETH_GET_CODE_METHOD: &str = "eth_getCode";
const ETH_GET_TRANSACTION_COUNT_METHOD: &str = "eth_getTransactionCount";
const ETH_GET_BLOCK_BY_NUMBER_METHOD: &str = "eth_getBlockByNumber";
const ETH_BLOCK_NUMBER_METHOD: &str = "eth_blockNumber";
const ETH_GET_TRANSACTION_RECEIPT_METHOD: &str = "eth_getTransactionReceipt";
const ETH_CALL_METHOD: &str = "eth_call";
const ETH_SEND_RAW_TRANSACTION_METHOD: &str = "eth_sendRawTransaction";
const ETH_GET_LOGS_METHOD: &str = "eth_getLogs";
const IC_GET_TX_EXECUTION_RESULT_BY_HASH_METHOD: &str = "ic_getExeResultByHash";
Expand Down Expand Up @@ -162,6 +165,16 @@ impl<C: Client> EthJsonRpcClient<C> {
.await
}

/// Returns code of the given contract.
pub async fn get_code(&self, address: H160, block: BlockNumber) -> anyhow::Result<String> {
self.single_request(
ETH_GET_CODE_METHOD.to_string(),
make_params_array!(address, block),
Id::Str("eth_getCode".to_string()),
)
.await
}

/// Returns transaction count of the address.
pub async fn get_transaction_count(
&self,
Expand All @@ -177,12 +190,26 @@ impl<C: Client> EthJsonRpcClient<C> {
.map(|v| v.as_u64())
}

/// Performs eth call and return the result.
pub async fn eth_call(
&self,
params: TransactionRequest,
block: BlockNumber,
) -> anyhow::Result<String> {
self.single_request(
ETH_CALL_METHOD.to_string(),
make_params_array!(params, block),
Id::Str("eth_call".to_string()),
)
.await
}

/// Sends raw transaction and returns transaction hash
pub async fn send_raw_transaction(&self, transaction: Transaction) -> anyhow::Result<H256> {
let bytes = transaction.rlp();
let transaction = format!("0x{}", hex::encode(bytes));

self.single_request::<H256>(
self.single_request(
ETH_SEND_RAW_TRANSACTION_METHOD.to_string(),
make_params_array!(transaction),
Id::Str(ETH_SEND_RAW_TRANSACTION_METHOD.to_string()),
Expand Down
135 changes: 130 additions & 5 deletions src/ethereum-json-rpc-client/tests/reqwest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ethereum_json_rpc_client::reqwest::ReqwestClient;
use ethereum_json_rpc_client::{EthGetLogsParams, EthJsonRpcClient};
use ethers_core::types::{BlockNumber, Log, H256};
use ethers_core::abi::{Function, Param, ParamType, StateMutability, Token};
use ethers_core::types::{BlockNumber, Log, TransactionRequest, H160, H256};

const ETHEREUM_JSON_API_URL: &str = "https://cloudflare-eth.com/";
const MAX_BATCH_SIZE: usize = 5;
Expand All @@ -25,23 +26,94 @@ async fn should_get_block_number() {

#[tokio::test]
async fn should_get_balance() {
let erc_1820_address = "0xa990077c3205cbDf861e17Fa532eeB069cE9fF96"
let erc_1820_deployer_address = "0xa990077c3205cbDf861e17Fa532eeB069cE9fF96"
.parse()
.unwrap();
let result = reqwest_client()
.get_balance(erc_1820_address, BlockNumber::Latest)
.get_balance(erc_1820_deployer_address, BlockNumber::Latest)
.await
.unwrap();
assert_eq!(result, 1409174700000000000u64.into());
}

#[tokio::test]
async fn should_get_code() {
let erc_1820_address = "0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24"
.parse()
.unwrap();
let result = reqwest_client()
.get_code(erc_1820_address, BlockNumber::Latest)
.await
.unwrap();
assert_eq!(result, ERC_1820_EXPECTED_CODE);
}

/// Calls the funtction of ERC-1820:
///
///```solidity
/// function getManager(address _addr) public view returns(address)
///```
#[tokio::test]
async fn should_perform_eth_call() {
let erc_1820_address = "0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24"
.parse::<H160>()
.unwrap();

let caller = "0xf990077c3205cbDf861e17Fa532eeB069cE9fF96"
.parse()
.unwrap();

#[allow(deprecated)]
let func = Function {
name: "getManager".to_string(),
inputs: vec![Param {
name: "getManager".to_string(),
kind: ParamType::Address,
internal_type: None,
}],
outputs: vec![Param {
name: "".to_string(),
kind: ParamType::Address,
internal_type: None,
}],
constant: None,
state_mutability: StateMutability::View,
};

let params = TransactionRequest {
from: Some(caller),
to: Some(erc_1820_address.into()),
gas: Some(1000000u64.into()),
gas_price: None,
value: None,
data: Some(func.encode_input(&[Token::Address(caller)]).unwrap().into()),
..Default::default()
};

let result = reqwest_client()
.eth_call(params, BlockNumber::Latest)
.await
.unwrap();

let result_address = func
.decode_output(&hex::decode(result.trim_start_matches("0x")).unwrap())
.unwrap()
.first()
.cloned()
.unwrap()
.into_address()
.unwrap();

assert_eq!(result_address, caller);
}

#[tokio::test]
async fn should_get_transaction_count() {
let erc_1820_address = "0xa990077c3205cbDf861e17Fa532eeB069cE9fF96"
let erc_1820_deployer_address = "0xa990077c3205cbDf861e17Fa532eeB069cE9fF96"
.parse()
.unwrap();
let result = reqwest_client()
.get_transaction_count(erc_1820_address, BlockNumber::Latest)
.get_transaction_count(erc_1820_deployer_address, BlockNumber::Latest)
.await
.unwrap();
assert_eq!(result, 1u64);
Expand Down Expand Up @@ -195,3 +267,56 @@ async fn should_get_transaction_receipts() {
assert_eq!(receipts[0].gas_used, Some(21000.into()));
assert_eq!(receipts[1].gas_used, Some(52358.into()));
}

const ERC_1820_EXPECTED_CODE: &str = "0x608060405234801561001057600080fd5b50600436106100a557600035\
7c010000000000000000000000000000000000000000000000000000000090048063a41e7d5111610078578063a41e7d51\
146101d4578063aabbb8ca1461020a578063b705676514610236578063f712f3e814610280576100a5565b806329965a1d\
146100aa5780633d584063146100e25780635df8122f1461012457806365ba36c114610152575b600080fd5b6100e06004\
80360360608110156100c057600080fd5b50600160a060020a038135811691602081013591604090910135166102b6565b\
005b610108600480360360208110156100f857600080fd5b5035600160a060020a0316610570565b60408051600160a060\
020a039092168252519081900360200190f35b6100e06004803603604081101561013a57600080fd5b50600160a060020a\
03813581169160200135166105bc565b6101c26004803603602081101561016857600080fd5b8101906020810181356401\
0000000081111561018357600080fd5b82018360208201111561019557600080fd5b803590602001918460018302840111\
640100000000831117156101b757600080fd5b5090925090506106b3565b60408051918252519081900360200190f35b61\
00e0600480360360408110156101ea57600080fd5b508035600160a060020a03169060200135600160e060020a03191661\
06ee565b6101086004803603604081101561022057600080fd5b50600160a060020a038135169060200135610778565b61\
026c6004803603604081101561024c57600080fd5b508035600160a060020a03169060200135600160e060020a03191661\
07ef565b604080519115158252519081900360200190f35b61026c6004803603604081101561029657600080fd5b508035\
600160a060020a03169060200135600160e060020a0319166108aa565b6000600160a060020a038416156102cd57836102\
cf565b335b9050336102db82610570565b600160a060020a031614610339576040805160e560020a62461bcd0281526020\
6004820152600f60248201527f4e6f7420746865206d616e61676572000000000000000000000000000000000060448201\
5290519081900360640190fd5b6103428361092a565b15610397576040805160e560020a62461bcd028152602060048201\
52601a60248201527f4d757374206e6f7420626520616e2045524331363520686173680000000000006044820152905190\
81900360640190fd5b600160a060020a038216158015906103b85750600160a060020a0382163314155b156104ff576040\
5160200180807f455243313832305f4143434550545f4d4147494300000000000000000000000081525060140190506040\
516020818303038152906040528051906020012082600160a060020a031663249cb3fa85846040518363ffffffff167c01\
000000000000000000000000000000000000000000000000000000000281526004018083815260200182600160a060020a\
0316600160a060020a031681526020019250505060206040518083038186803b15801561047e57600080fd5b505afa1580\
15610492573d6000803e3d6000fd5b505050506040513d60208110156104a857600080fd5b5051146104ff576040805160\
e560020a62461bcd02815260206004820181905260248201527f446f6573206e6f7420696d706c656d656e742074686520\
696e74657266616365604482015290519081900360640190fd5b600160a060020a03818116600081815260208181526040\
808320888452909152808220805473ffffffffffffffffffffffffffffffffffffffff1916948716948517905551869291\
7f93baa6efbd2244243bfee6ce4cfdd1d04fc4c0e9a786abd3a41313bd352db15391a450505050565b600160a060020a03\
818116600090815260016020526040812054909116151561059a5750806105b7565b50600160a060020a03808216600090\
815260016020526040902054165b919050565b336105c683610570565b600160a060020a031614610624576040805160e5\
60020a62461bcd02815260206004820152600f60248201527f4e6f7420746865206d616e61676572000000000000000000\
0000000000000000604482015290519081900360640190fd5b81600160a060020a031681600160a060020a031614610643\
5780610646565b60005b600160a060020a03838116600081815260016020526040808220805473ffffffffffffffffffff\
ffffffffffffffffffff19169585169590951790945592519184169290917f605c2dbf762e5f7d60a546d42e7205dcb1b0\
11ebc62a61736a57c9089d3a43509190a35050565b60008282604051602001808383808284378083019250505092505050\
6040516020818303038152906040528051906020012090505b92915050565b6106f882826107ef565b6107035760006107\
05565b815b600160a060020a03928316600081815260208181526040808320600160e060020a0319969096168084529582\
52808320805473ffffffffffffffffffffffffffffffffffffffff19169590971694909417909555908152600284528181\
209281529190925220805460ff19166001179055565b600080600160a060020a038416156107905783610792565b335b90\
5061079d8361092a565b156107c357826107ad82826108aa565b6107b85760006107ba565b815b925050506106e8565b60\
0160a060020a0390811660009081526020818152604080832086845290915290205416905092915050565b600080806108\
1d857f01ffc9a70000000000000000000000000000000000000000000000000000000061094c565b909250905081158061\
082d575080155b1561083d576000925050506106e8565b61084f85600160e060020a031961094c565b9092509050811580\
61086057508015155b15610870576000925050506106e8565b61087a858561094c565b909250905060018214801561088f\
5750806001145b1561089f576001925050506106e8565b506000949350505050565b600160a060020a0382166000908152\
600260209081526040808320600160e060020a03198516845290915281205460ff1615156108f2576108eb83836107ef56\
5b90506106e8565b50600160a060020a03808316600081815260208181526040808320600160e060020a03198716845290\
91529020549091161492915050565b7bffffffffffffffffffffffffffffffffffffffffffffffffffffffff161590565b\
6040517f01ffc9a70000000000000000000000000000000000000000000000000000000080825260048201839052600091\
82919060208160248189617530fa90519096909550935050505056fea165627a7a72305820377f4a2d4301ede9949f163f\
319021a6e9c687c292a5e2b2c4734c126b524e6c0029";
77 changes: 11 additions & 66 deletions src/minter-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use candid::{Nat, Principal};
use candid::Principal;
use did::build::BuildData;
use did::H160;
use ic_canister_client::{CanisterClient, CanisterClientResult};
use minter_did::error::Result as McResult;
use minter_did::id256::Id256;
use minter_did::init::OperationPricing;
use minter_did::order::SignedMintOrder;
use minter_did::reason::Icrc2Burn;

Expand Down Expand Up @@ -84,40 +83,10 @@ impl<C: CanisterClient> MinterCanisterClient<C> {
.await
}

/// Returns operation points number of the user.
pub async fn get_user_operation_points(
&self,
user: Option<Principal>,
) -> CanisterClientResult<u32> {
self.client
.query("get_user_operation_points", (user,))
.await
}

/// Returns operations pricing.
/// This method is available for canister owner only.
pub async fn set_operation_pricing(
&mut self,
pricing: OperationPricing,
) -> CanisterClientResult<McResult<()>> {
self.client
.update("set_operation_pricing", (pricing,))
.await
}

/// Returns operation pricing.
pub async fn get_operation_pricing(&self) -> CanisterClientResult<OperationPricing> {
self.client.query("get_operation_pricing", ()).await
}

/// Creates ERC-20 mint order for ICRC-2 tokens burning.
pub async fn create_erc_20_mint_order(
&self,
reason: Icrc2Burn,
) -> CanisterClientResult<McResult<SignedMintOrder>> {
self.client
.update("create_erc_20_mint_order", (reason,))
.await
/// Creates ERC-20 mint order for ICRC-2 tokens burning and sends it to the BFTBridge.
/// Returns operation id.
pub async fn burn_icrc2(&self, reason: Icrc2Burn) -> CanisterClientResult<McResult<u32>> {
self.client.update("burn_icrc2", (reason,)).await
}

/// Returns `(nonce, mint_order)` pairs for the given sender id.
Expand All @@ -131,39 +100,15 @@ impl<C: CanisterClient> MinterCanisterClient<C> {
.await
}

/// Approves ICRC-2 token transfer from minter canister to recipient.
/// Returns approved amount.
///
/// # Arguments
/// - `user` is an address of wallet which has been used for Wrapped token burning.
/// - `operation_id` is an ID retuned by `BFTBridge::burn()` operation.
pub async fn start_icrc2_mint(
&self,
user: &H160,
operation_id: u32,
) -> CanisterClientResult<McResult<Nat>> {
self.client
.update("start_icrc2_mint", (user, operation_id))
.await
}

/// Transfers ICRC-2 tokens from minter canister to recipient.
///
/// Before it can be used, ICRC-2 token must be approved by `start_icrc2_mint` which approves the transfer.
/// After the approval, user should finalize Wrapped token burning, using `BFTBridge::finish_burn()`.
pub async fn finish_icrc2_mint(
/// Returns mint order for the given parameters.
pub async fn get_mint_order(
&self,
sender: Id256,
src_token: Id256,
operation_id: u32,
address: &H160,
icrc2_token: Principal,
recipient: Principal,
amount: Nat,
) -> CanisterClientResult<McResult<Nat>> {
) -> CanisterClientResult<Option<SignedMintOrder>> {
self.client
.update(
"finish_icrc2_mint",
(operation_id, address, icrc2_token, recipient, amount),
)
.query("get_mint_order", (sender, src_token, operation_id))
.await
}

Expand Down
Loading

0 comments on commit 881d3dd

Please sign in to comment.