Skip to content

Commit

Permalink
add PoolDetailInfo view functions (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
MagicGordon authored Sep 27, 2024
1 parent eda132f commit 9d853b0
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 3 deletions.
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 ref-exchange/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ref-exchange"
version = "1.9.5"
version = "1.9.6"
authors = ["Illia Polosukhin <[email protected]>"]
edition = "2018"
publish = false
Expand Down
6 changes: 6 additions & 0 deletions ref-exchange/release_notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes

### Version 1.9.6
```
2Yo8qJ5S3biFJbnBdb4ZNcGhN1RhHJq34cGF4g7Yigcw
```
1. add PoolDetailInfo view functions

### Version 1.9.5
```
3hF1UzsT5mzxbJLMA8BD7gmCH1BL8cWjvFmSZYREpZXK
Expand Down
89 changes: 89 additions & 0 deletions ref-exchange/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,70 @@ impl From<Pool> for PoolInfo {
}
}

#[derive(Serialize, Deserialize)]
#[serde(crate = "near_sdk::serde")]
#[cfg_attr(not(target_arch = "wasm32"), derive(Debug, PartialEq))]
pub enum PoolDetailInfo {
SimplePoolInfo(SimplePoolInfo),
StablePoolInfo(StablePoolInfo),
RatedPoolInfo(RatedPoolInfo),
DegenPoolInfo(DegenPoolInfo),
}

impl From<SimplePoolInfo> for PoolDetailInfo {
fn from(pool: SimplePoolInfo) -> Self {
PoolDetailInfo::SimplePoolInfo(pool)
}
}

impl From<StablePoolInfo> for PoolDetailInfo {
fn from(pool: StablePoolInfo) -> Self {
PoolDetailInfo::StablePoolInfo(pool)
}
}

impl From<RatedPoolInfo> for PoolDetailInfo {
fn from(pool: RatedPoolInfo) -> Self {
PoolDetailInfo::RatedPoolInfo(pool)
}
}

impl From<DegenPoolInfo> for PoolDetailInfo {
fn from(pool: DegenPoolInfo) -> Self {
PoolDetailInfo::DegenPoolInfo(pool)
}
}

#[derive(Serialize, Deserialize)]
#[serde(crate = "near_sdk::serde")]
#[cfg_attr(not(target_arch = "wasm32"), derive(Debug, PartialEq))]
pub struct SimplePoolInfo {
/// List of tokens in the pool.
pub token_account_ids: Vec<AccountId>,
/// How much NEAR this contract has.
pub amounts: Vec<U128>,
/// Fee charged for swap.
pub total_fee: u32,
/// Total number of shares.
pub shares_total_supply: U128,
}

impl From<Pool> for SimplePoolInfo {
fn from(pool: Pool) -> Self {
match pool {
Pool::SimplePool(pool) => Self {
token_account_ids: pool.token_account_ids,
amounts: pool.amounts.into_iter().map(|a| U128(a)).collect(),
total_fee: pool.total_fee,
shares_total_supply: U128(pool.shares_total_supply),
},
Pool::StableSwapPool(_) => unimplemented!(),
Pool::RatedSwapPool(_) => unimplemented!(),
Pool::DegenSwapPool(_) => unimplemented!(),
}
}
}

#[derive(Serialize, Deserialize)]
#[serde(crate = "near_sdk::serde")]
#[cfg_attr(not(target_arch = "wasm32"), derive(Debug, PartialEq))]
Expand Down Expand Up @@ -326,6 +390,31 @@ impl Contract {
.collect()
}

/// Returns list of pool detail infos of given length from given start index.
pub fn get_pool_detail_infos(&self, from_index: u64, limit: u64) -> Vec<PoolDetailInfo> {
(from_index..std::cmp::min(from_index + limit, self.pools.len()))
.map(|index| self.get_pool_detail_info(index))
.collect()
}

/// Returns pool detail info about specified pool.
pub fn get_pool_detail_info(&self, pool_id: u64) -> PoolDetailInfo {
let pool = self.pools.get(pool_id).expect(ERR85_NO_POOL);
match &pool {
Pool::SimplePool(_) => <Pool as Into<SimplePoolInfo>>::into(pool).into(),
Pool::StableSwapPool(_) => <Pool as Into<StablePoolInfo>>::into(pool).into(),
Pool::RatedSwapPool(_) => <Pool as Into<RatedPoolInfo>>::into(pool).into(),
Pool::DegenSwapPool(_) => <Pool as Into<DegenPoolInfo>>::into(pool).into(),
}
}

/// Returns list of pool detail infos of given pool ids.
pub fn get_pool_detail_info_by_ids(&self, pool_ids: Vec<u64>) -> Vec<PoolDetailInfo> {
pool_ids.into_iter()
.map(|index| self.get_pool_detail_info(index))
.collect()
}

/// Returns stable pool information about specified pool.
pub fn get_stable_pool(&self, pool_id: u64) -> StablePoolInfo {
self.pools.get(pool_id).expect(ERR85_NO_POOL).into()
Expand Down
2 changes: 1 addition & 1 deletion ref-exchange/tests/test_migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn test_upgrade() {
.assert_success();
let metadata = get_metadata(&pool);
// println!("{:#?}", metadata);
assert_eq!(metadata.version, "1.9.5".to_string());
assert_eq!(metadata.version, "1.9.6".to_string());
assert_eq!(metadata.admin_fee_bps, 5);
assert_eq!(metadata.boost_farm_id, "boost_farm".to_string());
assert_eq!(metadata.burrowland_id, "burrowland".to_string());
Expand Down
Binary file modified releases/ref_exchange_release.wasm
Binary file not shown.
Binary file added releases/ref_exchange_release_v195.wasm
Binary file not shown.

0 comments on commit 9d853b0

Please sign in to comment.