Skip to content

Commit

Permalink
feat: Add fn query_asset_info_balances (#20)
Browse files Browse the repository at this point in the history
* feat: Add fn query_asset_info_balances

* style: clippy fmt

* ci: Delete coverage job and switch lint to stable

* feat: impl to_asset for assetinfo
  • Loading branch information
apollo-sturdy authored Aug 11, 2023
1 parent 9ac15cf commit 6610ac4
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 44 deletions.
38 changes: 0 additions & 38 deletions .github/workflows/coverage.yml

This file was deleted.

8 changes: 4 additions & 4 deletions .github/workflows/lint-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ jobs:
- name: Checkout sources
uses: actions/checkout@v3

- name: Install nightly toolchain
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
toolchain: stable
components: rustfmt, clippy

- name: Install cargo-machete
Expand All @@ -30,14 +30,14 @@ jobs:
- name: Run cargo clippy
uses: actions-rs/cargo@v1
with:
toolchain: nightly
toolchain: stable
command: clippy
args: --all-features -- -D warnings

- name: Run cargo fmt
uses: actions-rs/cargo@v1
with:
toolchain: nightly
toolchain: stable
command: fmt
args: --all -- --check --verbose

Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.2] - 2023-08-11

### Added

- `fn to_asset` method on `AssetInfo` to convert to `Asset` with the specified amount.
- `fn query_asset_info_balances` method on `AssetList` to query balances of a `Vec<AssetInfo>`.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "apollo-cw-asset"
description = "Helper library for interacting with Cosmos assets (SDK coins and CW20 tokens)"
version = "0.1.1"
version = "0.1.2"
authors = ["larry <[email protected]>", "Apollo DAO Contributors <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
10 changes: 10 additions & 0 deletions src/asset_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::Asset;

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Hash, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum AssetInfoBase<T> {
Expand Down Expand Up @@ -262,6 +264,14 @@ impl AssetInfo {
pub fn is_native(&self) -> bool {
matches!(self, AssetInfo::Native(_))
}

/// Create a new asset from the `AssetInfo` with the given amount
pub fn to_asset(&self, amount: impl Into<Uint128>) -> Asset {
Asset {
info: self.clone(),
amount: amount.into(),
}
}
}

#[cfg(test)]
Expand Down
19 changes: 19 additions & 0 deletions src/asset_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,25 @@ impl AssetList {
.collect::<StdResult<Vec<Asset>>>()
.map(Into::into)
}

/// Queries balances for all `AssetInfo` objects in the given vec for the
/// given address and return a new `AssetList`
pub fn query_asset_info_balances(
asset_infos: Vec<AssetInfo>,
querier: &QuerierWrapper,
addr: &Addr,
) -> StdResult<AssetList> {
asset_infos
.into_iter()
.map(|asset_info| {
Ok(Asset::new(
asset_info.clone(),
asset_info.query_balance(querier, addr)?,
))
})
.collect::<StdResult<Vec<Asset>>>()
.map(Into::into)
}
}

#[cfg(test)]
Expand Down

0 comments on commit 6610ac4

Please sign in to comment.