Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new provider api and tests #637

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/wallets/checking-balances-and-coins.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ If you want to query all the balances (i.e., get the balance for each asset ID i
{{#include ../../../examples/wallets/src/lib.rs:get_balances}}
```

The return type is a `HashMap`, where the key is the _asset ID's_ hex string, and the value is the corresponding balance. For example, we can get the base asset balance with:
The return type is a `HashMap`, where the key is an `AssetId`, and the value is the corresponding balance. For example, we can get the base asset balance with:

```rust,ignore
{{#include ../../../examples/wallets/src/lib.rs:get_balance_hashmap}}
Expand Down
5 changes: 1 addition & 4 deletions examples/cookbook/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ mod tests {
async fn transfer_multiple() -> Result<(), Error> {
// ANCHOR: transfer_multiple
use fuels::prelude::*;
use std::str::FromStr;

// ANCHOR: transfer_multiple_setup
let mut wallet_1 = WalletUnlocked::new_random(None);
Expand All @@ -140,9 +139,7 @@ mod tests {

let mut inputs = vec![];
let mut outputs = vec![];
for (id_string, amount) in balances {
let id = AssetId::from_str(&id_string).unwrap();

for (id, amount) in balances {
// leave the base asset to cover transaction fees
if id == BASE_ASSET_ID {
continue;
Expand Down
6 changes: 3 additions & 3 deletions examples/predicates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ mod tests {
.await?;

let predicate_balance = provider
.get_asset_balance(predicate.address(), asset_id)
.get_asset_balance(predicate.address(), &asset_id)
.await?;
assert_eq!(predicate_balance, amount_to_predicate);
// ANCHOR_END: predicate_send
Expand Down Expand Up @@ -107,12 +107,12 @@ mod tests {
.await?;

let receiver_balance_after = provider
.get_asset_balance(receiver.address(), asset_id)
.get_asset_balance(receiver.address(), &asset_id)
.await?;
assert_eq!(amount_to_predicate, receiver_balance_after);

let predicate_balance = provider
.get_asset_balance(predicate.address(), asset_id)
.get_asset_balance(predicate.address(), &asset_id)
.await?;
assert_eq!(predicate_balance, 0);
// ANCHOR_END: predicate_spend
Expand Down
4 changes: 2 additions & 2 deletions examples/providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ mod tests {
// ANCHOR_END: setup_test_blockchain

// ANCHOR: get_coins
let coins = provider.get_coins(wallet.address(), BASE_ASSET_ID).await?;
let coins = provider.get_coins(wallet.address(), &BASE_ASSET_ID).await?;
assert_eq!(coins.len(), 1);
// ANCHOR_END: get_coins

// ANCHOR: get_spendable_coins
let spendable_coins = provider
.get_spendable_coins(wallet.address(), BASE_ASSET_ID, 1)
.get_spendable_coins(wallet.address(), &BASE_ASSET_ID, 1)
.await?;
assert_eq!(spendable_coins.len(), 1);
// ANCHOR_END: get_spendable_coins
Expand Down
9 changes: 4 additions & 5 deletions examples/wallets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ mod tests {
.await?;
assert_eq!(contract_balances.len(), 1);

let random_asset_id_key = format!("{:#x}", random_asset_id);
let random_asset_balance = contract_balances.get(&random_asset_id_key).unwrap();
let random_asset_balance = contract_balances.get(&random_asset_id).unwrap();
assert_eq!(*random_asset_balance, 300);
// ANCHOR_END: wallet_contract_transfer

Expand Down Expand Up @@ -318,12 +317,12 @@ mod tests {
let balance: u64 = wallet.get_asset_balance(&asset_id).await?;
// ANCHOR_END: get_asset_balance
// ANCHOR: get_balances
let balances: HashMap<String, u64> = wallet.get_balances().await?;
let balances: HashMap<AssetId, u64> = wallet.get_balances().await?;
// ANCHOR_END: get_balances

// ANCHOR: get_balance_hashmap
let asset_id_key = format!("{:#x}", asset_id);
let asset_balance = balances.get(&asset_id_key).unwrap();
let asset_id: AssetId = BASE_ASSET_ID;
let asset_balance = balances.get(&asset_id).unwrap();
// ANCHOR_END: get_balance_hashmap

assert_eq!(*asset_balance, DEFAULT_COIN_AMOUNT * DEFAULT_NUM_COINS);
Expand Down
2 changes: 1 addition & 1 deletion packages/fuels-contract/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Script {
required_asset_amounts: &[(AssetId, u64)],
) -> Result<Vec<Coin>, Error> {
stream::iter(required_asset_amounts)
.map(|(asset_id, amount)| wallet.get_spendable_coins(*asset_id, *amount))
.map(|(asset_id, amount)| wallet.get_spendable_coins(asset_id, *amount))
.buffer_unordered(10)
.collect::<Vec<_>>()
.await
Expand Down
4 changes: 2 additions & 2 deletions packages/fuels-core/src/code_gen/abigen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Abigen {
use fuels::core::code_gen::function_selector::resolve_fn_selector;
use fuels::core::types::*;
use fuels::signers::WalletUnlocked;
use fuels::tx::{ContractId, Address, Receipt};
use fuels::tx::{ContractId, Address, Receipt, AssetId};
use fuels::types::bech32::Bech32ContractId;
use fuels::types::ResolvedLog;
use fuels::types::errors::Error as SDKError;
Expand Down Expand Up @@ -147,7 +147,7 @@ impl Abigen {
Ok(Self { contract_id: self.contract_id.clone(), wallet: wallet, logs_lookup: self.logs_lookup.clone() })
}

pub async fn get_balances(&self) -> Result<HashMap<String, u64>, SDKError> {
pub async fn get_balances(&self) -> Result<HashMap<AssetId, u64>, SDKError> {
self.wallet.get_provider()?.get_contract_balances(&self.contract_id).await.map_err(Into::into)
}

Expand Down
7 changes: 4 additions & 3 deletions packages/fuels-signers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,15 @@ mod tests {
// Assert that the transaction was properly configured.
let res = wallet_1
.get_provider()?
.get_transaction_by_id(&tx_id)
.await?;
.get_transaction(&tx_id)
.await?
.expect("could not find transaction with specified id");

assert_eq!(res.transaction.gas_limit(), gas_limit);
assert_eq!(res.transaction.gas_price(), gas_price);
assert_eq!(res.transaction.maturity(), maturity);

let wallet_1_spendable_coins = wallet_1.get_spendable_coins(BASE_ASSET_ID, 0).await?;
let wallet_1_spendable_coins = wallet_1.get_spendable_coins(&BASE_ASSET_ID, 0).await?;
let wallet_1_all_coins = wallet_1.get_coins(BASE_ASSET_ID).await?;
let wallet_2_all_coins = wallet_2.get_coins(BASE_ASSET_ID).await?;

Expand Down
Loading