-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
81 lines (64 loc) · 2.19 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//Uning cosmwasm::std Example of creating pools
use cosmwasm::{Coin, coins, to_binary};
use cosmwasm::std::{Coin as StdCoin, StdResult, StdError};
// This struct represents a pool of tokens
struct Pool {
// The total amount of tokens in the pool
total: Coin,
}
// This trait defines the interface for interacting with the pool
trait PoolInterface {
// Initializes the pool with a given amount of tokens
fn init(&mut self, initial_amount: Coin) -> StdResult<()>;
// Adds a given amount of tokens to the pool
fn add_to_pool(&mut self, amount: Coin) -> StdResult<()>;
// Removes a given amount of tokens from the pool
fn remove_from_pool(&mut self, amount: Coin) -> StdResult<()>;
// Returns the total amount of tokens in the pool
fn get_pool_total(&self) -> StdResult<Coin>;
}
// This implementation provides the functionality for the Pool trait
impl PoolInterface for Pool {
fn init(&mut self, initial_amount: Coin) -> StdResult<()> {
self.total = initial_amount;
Ok(())
}
fn add_to_pool(&mut self, amount: Coin) -> StdResult<()> {
self.total = self.total.add(amount)?;
Ok(())
}
fn remove_from_pool(&mut self, amount: Coin) -> StdResult<()> {
if amount > self.total {
return Err(StdError::generic_err("Not enough tokens in pool"));
}
self.total = self.total.sub(amount)?;
Ok(())
}
fn get_pool_total(&self) -> StdResult<Coin> {
Ok(self.total)
}
}
// This function creates a new pool with a given amount of tokens
fn create_pool(initial_amount: Coin) -> StdResult<Pool> {
let pool = Pool { total: initial_amount };
Ok(pool)
}
// This function demonstrates how to use the Pool trait to interact with a pool
fn demo() -> StdResult<()> {
// Create a new pool with 100 tokens
let mut pool = create_pool(Coin {
amount: 100,
denom: "token".to_string(),
})?;
// Add 50 tokens to the pool
pool.add_to_pool(Coin {
amount: 50,
denom: "token".to_string(),
})?;
// Remove 75 tokens from the pool
pool.remove_from_pool(Coin {
amount: 75,
denom: "token".to_string(),
})?;
// Get
Try again