From 227a937bdf4b6ba55f4438e637bfc4eeac63060b Mon Sep 17 00:00:00 2001 From: hal3e Date: Wed, 19 Oct 2022 15:31:30 +0200 Subject: [PATCH] add tests --- packages/fuels-signers/src/lib.rs | 5 +- packages/fuels-signers/src/provider.rs | 276 ++++++++++++++++++------- packages/fuels-signers/src/wallet.rs | 9 +- 3 files changed, 205 insertions(+), 85 deletions(-) diff --git a/packages/fuels-signers/src/lib.rs b/packages/fuels-signers/src/lib.rs index cbe74ef6ce..c3fc6cbcb9 100644 --- a/packages/fuels-signers/src/lib.rs +++ b/packages/fuels-signers/src/lib.rs @@ -182,10 +182,7 @@ mod tests { .await?; // Assert that the transaction was properly configured. - let res = wallet_1 - .get_provider()? - .get_transaction_by_id(&tx_id) - .await?; + let res = wallet_1.get_provider()?.get_transaction(&tx_id).await?; assert_eq!(res.transaction.gas_limit(), gas_limit); assert_eq!(res.transaction.gas_price(), gas_price); diff --git a/packages/fuels-signers/src/provider.rs b/packages/fuels-signers/src/provider.rs index 0e81a75be9..bada804f92 100644 --- a/packages/fuels-signers/src/provider.rs +++ b/packages/fuels-signers/src/provider.rs @@ -10,7 +10,7 @@ use fuel_gql_client::{ contract::ContractBalance, message::Message, node_info::NodeInfo, resource::Resource, }, types::{TransactionResponse, TransactionStatus}, - FuelClient, PageDirection, PaginatedResult, PaginationRequest, + FuelClient, PageDirection, PaginationRequest, }, fuel_tx::{Receipt, Transaction, TransactionFee, UtxoId}, fuel_types::{AssetId, ContractId}, @@ -266,6 +266,7 @@ impl Provider { /// Get all the spendable balances of all assets for address `address`. This is different from /// getting the coins because we are only returning the numbers (the sum of UTXOs coins amount /// for each asset id) and not the UTXOs coins themselves + //TODO: check if we need to change String to AssetId pub async fn get_balances( &self, address: &Bech32Address, @@ -296,6 +297,7 @@ impl Provider { } /// Get all balances of all assets for the contract with id `contract_id`. + //TODO: check if we need to change String to AssetId pub async fn get_contract_balances( &self, contract_id: &Bech32ContractId, @@ -334,32 +336,64 @@ impl Provider { self.client.contract(&hex_id).await.map_err(Into::into) } - /// Get transaction by id. - pub async fn get_transaction_by_id( - &self, - tx_id: &str, - ) -> Result { + pub async fn get_transaction(&self, tx_id: &str) -> Result { Ok(self.client.transaction(tx_id).await.unwrap().unwrap()) } - // - Get transaction(s) - pub async fn get_transactions( - &self, - request: PaginationRequest, - ) -> Result, ProviderError> { - self.client.transactions(request).await.map_err(Into::into) + pub async fn get_transactions(&self) -> Result, ProviderError> { + let mut transaction_responses: Vec = vec![]; + + let mut cursor = None; + + loop { + let res = self + .client + .transactions(PaginationRequest { + cursor: cursor.clone(), + results: 100, + direction: PageDirection::Forward, + }) + .await?; + + if res.results.is_empty() { + break; + } + transaction_responses.extend(res.results); + cursor = res.cursor; + } + + Ok(transaction_responses) } - // Get transaction(s) by owner pub async fn get_transactions_by_owner( &self, owner: &Bech32Address, - request: PaginationRequest, - ) -> Result, ProviderError> { - self.client - .transactions_by_owner(&owner.hash().to_string(), request) - .await - .map_err(Into::into) + ) -> Result, ProviderError> { + let mut transaction_responses: Vec = vec![]; + + let mut cursor = None; + + loop { + let res = self + .client + .transactions_by_owner( + &owner.hash().to_string(), + PaginationRequest { + cursor: cursor.clone(), + results: 100, + direction: PageDirection::Forward, + }, + ) + .await?; + + if res.results.is_empty() { + break; + } + transaction_responses.extend(res.results); + cursor = res.cursor; + } + + Ok(transaction_responses) } pub async fn latest_block_height(&self) -> Result { @@ -450,11 +484,16 @@ impl Provider { self.client.block(block_id).await.map_err(Into::into) } - pub async fn get_blocks( - &self, - request: PaginationRequest, - ) -> Result, ProviderError> { - self.client.blocks(request).await.map_err(Into::into) + pub async fn get_blocks(&self) -> Result, ProviderError> { + let res = self + .client + .blocks(PaginationRequest { + cursor: None, + results: 999, + direction: PageDirection::Forward, + }) + .await?; + Ok(res.results) } } @@ -463,6 +502,7 @@ mod tests { #[cfg(feature = "test-helpers")] use fuel_core::model::Coin; + use fuel_gql_client::client::types::TransactionStatus; use fuel_gql_client::fuel_tx::UtxoId; use fuels::prelude::*; @@ -543,47 +583,74 @@ mod tests { } #[tokio::test] - async fn test_balance_api() -> Result<(), Error> { + async fn test_asset_balance_api() -> Result<(), Error> { let (wallet, (coins, asset_ids), provider) = setup_provider_api_test().await; let asset_id = &asset_ids[0]; - let hex_asset_id = format!("{:#x}", asset_id); - let wallet_balance_asset_id: u64 = coins + let balance_of_coins_with_asset_id: u64 = coins .iter() .filter(|c| c.1.asset_id == *asset_id) .map(|c| c.1.amount) .sum(); - let wallet_balances = provider.get_balances(wallet.address()).await?; - let expected_asset_balance = wallet_balances - .get(&hex_asset_id) - .expect("could not get balance for asset id"); + let expected_balance = provider + .get_asset_balance(wallet.address(), asset_id) + .await?; - assert_eq!(*expected_asset_balance, wallet_balance_asset_id); + assert_eq!(balance_of_coins_with_asset_id, expected_balance); Ok(()) } #[tokio::test] - async fn test_asset_balance_api() -> Result<(), Error> { + async fn test_contract_asset_balance_api() -> Result<(), Error> { + let (wallet, (_, asset_ids), provider) = setup_provider_api_test().await; + let asset_id = &asset_ids[0]; + + let contract_id = Contract::deploy( + "../fuels/tests/contracts/contract_test/out/debug/contract_test.bin", + &wallet, + TxParameters::default(), + StorageConfiguration::default(), + ) + .await?; + + let amount = 18; + let _receipts = wallet + .force_transfer_to_contract(&contract_id, amount, *asset_id, TxParameters::default()) + .await?; + + let expected_contract_balance = provider + .get_contract_asset_balance(&contract_id, asset_id) + .await?; + + assert_eq!(expected_contract_balance, amount); + + Ok(()) + } + + #[tokio::test] + async fn test_balances_api() -> Result<(), Error> { let (wallet, (coins, asset_ids), provider) = setup_provider_api_test().await; let asset_id = &asset_ids[0]; - let balance_of_coins_with_asset_id: u64 = coins + let hex_asset_id = format!("{:#x}", asset_id); + let wallet_balance_asset_id: u64 = coins .iter() .filter(|c| c.1.asset_id == *asset_id) .map(|c| c.1.amount) .sum(); - let expected_balance = provider - .get_asset_balance(wallet.address(), asset_id) - .await?; + let wallet_balances = provider.get_balances(wallet.address()).await?; + let expected_asset_balance = wallet_balances + .get(&hex_asset_id) + .expect("could not get balance for asset id"); - assert_eq!(balance_of_coins_with_asset_id, expected_balance); + assert_eq!(*expected_asset_balance, wallet_balance_asset_id); Ok(()) } #[tokio::test] - async fn test_contract_balance_api() -> Result<(), Error> { + async fn test_contract_balances_api() -> Result<(), Error> { let (wallet, (_, asset_ids), provider) = setup_provider_api_test().await; let asset_id = &asset_ids[0]; let hex_asset_id = format!("{:#x}", asset_id); @@ -612,9 +679,8 @@ mod tests { } #[tokio::test] - async fn test_contract_asset_balance_api() -> Result<(), Error> { - let (wallet, (_, asset_ids), provider) = setup_provider_api_test().await; - let asset_id = &asset_ids[0]; + async fn test_get_contract_api() -> Result<(), Error> { + let (wallet, (_, _), provider) = setup_provider_api_test().await; let contract_id = Contract::deploy( "../fuels/tests/contracts/contract_test/out/debug/contract_test.bin", @@ -623,23 +689,20 @@ mod tests { StorageConfiguration::default(), ) .await?; + let hex_contract_id = format!("{:#x}", ContractId::from(&contract_id)); - let amount = 18; - let _receipts = wallet - .force_transfer_to_contract(&contract_id, amount, *asset_id, TxParameters::default()) - .await?; - - let expected_contract_balance = provider - .get_contract_asset_balance(&contract_id, asset_id) - .await?; + let expected_contract = provider + .get_contract(&contract_id) + .await? + .expect("could not find contract with specified id"); - assert_eq!(expected_contract_balance, amount); + assert_eq!(hex_contract_id, expected_contract.id.to_string()); Ok(()) } #[tokio::test] - async fn test_transaction_by_id_api() -> Result<(), Error> { + async fn test_transaction_api() -> Result<(), Error> { let (wallet, (_, _), provider) = setup_provider_api_test().await; let wallet2 = WalletUnlocked::new_random(Some(provider.clone())); @@ -657,7 +720,7 @@ mod tests { .transfer(wallet2.address(), 1, Default::default(), tx_params) .await?; - let expected_response = provider.get_transaction_by_id(&tx_id).await?; + let expected_response = provider.get_transaction(&tx_id).await?; assert_eq!(expected_response.transaction.gas_limit(), gas_limit); assert_eq!(expected_response.transaction.gas_price(), gas_price); @@ -667,34 +730,97 @@ mod tests { } #[tokio::test] - async fn test_get_contract_api() -> Result<(), Error> { + async fn test_transactions_api() -> Result<(), Error> { let (wallet, (_, _), provider) = setup_provider_api_test().await; - let contract_id = Contract::deploy( - "../fuels/tests/contracts/contract_test/out/debug/contract_test.bin", - &wallet, - TxParameters::default(), - StorageConfiguration::default(), - ) - .await?; - let hex_contract_id = format!("{:#x}", ContractId::from(&contract_id)); + let wallet2 = WalletUnlocked::new_random(Some(provider.clone())); - let expected_contract = provider - .get_contract(&contract_id) - .await? - .expect("could not find contract with specified id"); + // Make two transactions + let (_tx_id1, _receipts) = wallet + .transfer(wallet2.address(), 1, Default::default(), Default::default()) + .await?; + let (_tx_id2, _receipts) = wallet + .transfer(wallet2.address(), 1, Default::default(), Default::default()) + .await?; - assert_eq!(hex_contract_id, expected_contract.id.to_string()); + let expected_response = provider.get_transactions().await?; + + assert_eq!(expected_response.len(), 2); + //TODO: check if I can test it in another way Ok(()) } -} -// TODO: update get_messages -// pub async fn get_messages(&self, from: &Bech32Address) -> Result, ProviderError> { + #[tokio::test] + async fn test_transaction_by_owner_api() -> Result<(), Error> { + let (wallet, (_, _), provider) = setup_provider_api_test().await; + + let wallet2 = WalletUnlocked::new_random(Some(provider.clone())); + + // Make two transactions + let (_tx_id1, _receipts) = wallet + .transfer(wallet2.address(), 1, Default::default(), Default::default()) + .await?; + let (_tx_id2, _receipts) = wallet + .transfer(wallet2.address(), 1, Default::default(), Default::default()) + .await?; + + let expected_response = provider.get_transactions_by_owner(wallet.address()).await?; + + assert_eq!(expected_response.len(), 2); + //TODO: check if I can test it in another way + + Ok(()) + } + + #[tokio::test] + async fn test_block_api() -> Result<(), Error> { + let (wallet, (_, _), provider) = setup_provider_api_test().await; + + let wallet2 = WalletUnlocked::new_random(Some(provider.clone())); + + let (tx_id, _receipts) = wallet + .transfer(wallet2.address(), 1, Default::default(), Default::default()) + .await?; + + if let TransactionStatus::Success { block_id, time, .. } = + provider.get_transaction(&tx_id).await?.status + { + let expected_block = provider + .get_block(&block_id) + .await? + .expect("could not find block with specified id"); + + assert_eq!(block_id, expected_block.id.to_string()); + assert_eq!(expected_block.time, time); + + return Ok(()); + } + + Err(Error::ProviderError( + "Transaction was not successfull".into(), + )) + } + + #[tokio::test] + async fn test_blocks_api() -> Result<(), Error> { + let (wallet, (_, _), provider) = setup_provider_api_test().await; + + let wallet2 = WalletUnlocked::new_random(Some(provider.clone())); + + // Make two transactions + let (_tx_id1, _receipts) = wallet + .transfer(wallet2.address(), 1, Default::default(), Default::default()) + .await?; + let (_tx_id2, _receipts) = wallet + .transfer(wallet2.address(), 1, Default::default(), Default::default()) + .await?; -// TODO: make tests -// pub async fn get_transactions -// pub async fn get_transactions_by_owner -// pub async fn get_block -// pub async fn get_blocks + let expected_blocks = provider.get_blocks().await?; + + assert_eq!(expected_blocks.len(), 2); + //TODO: check if I can test it in another way + + Ok(()) + } +} diff --git a/packages/fuels-signers/src/wallet.rs b/packages/fuels-signers/src/wallet.rs index ee6934ba0f..a376a0c38d 100644 --- a/packages/fuels-signers/src/wallet.rs +++ b/packages/fuels-signers/src/wallet.rs @@ -7,7 +7,7 @@ use fuel_crypto::{Message, PublicKey, SecretKey, Signature}; use fuel_gql_client::client::schema; use fuel_gql_client::fuel_vm::prelude::GTFArgs; use fuel_gql_client::{ - client::{schema::coin::Coin, types::TransactionResponse, PaginatedResult, PaginationRequest}, + client::{schema::coin::Coin, types::TransactionResponse}, fuel_tx::{ AssetId, Bytes32, ContractId, Input, Output, Receipt, Transaction, TransactionFee, TxPointer, UtxoId, Witness, @@ -126,12 +126,9 @@ impl Wallet { &self.address } - pub async fn get_transactions( - &self, - request: PaginationRequest, - ) -> Result, Error> { + pub async fn get_transactions(&self) -> Result, Error> { self.get_provider()? - .get_transactions_by_owner(&self.address, request) + .get_transactions_by_owner(&self.address) .await .map_err(Into::into) }