Skip to content

Commit

Permalink
rename to store_by_key and make it a store of a one length array
Browse files Browse the repository at this point in the history
Co-authored-by: Richard Pringle <richard.pringle@avalabs.org>
Signed-off-by: Franfran <51274081+iFrostizz@users.noreply.github.com>
  • Loading branch information
iFrostizz and richardpringle committed Jul 5, 2024
1 parent 7dfad96 commit 6ab3bc0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 35 deletions.
37 changes: 14 additions & 23 deletions x/programs/rust/examples/automated-market-maker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@ pub fn add_liquidity(context: Context<StateKeys>, amount_x: u64, amount_y: u64)

program
.state()
.store(StateKeys::ReserveX, &(reserve_x + amount_x))
.unwrap();
program
.state()
.store(StateKeys::ReserveY, &(reserve_y + amount_y))
.unwrap();
program
.state()
.store(StateKeys::TotalySupply, &(total_supply + minted))
.store([
(StateKeys::ReserveX, &(reserve_x + amount_x)),
(StateKeys::ReserveY, &(reserve_y + amount_y)),
(StateKeys::TotalySupply, &(total_supply + minted)),
])
.unwrap();

minted
Expand All @@ -57,15 +53,11 @@ pub fn remove_liquidity(context: Context<StateKeys>, shares: u64) -> (u64, u64)

program
.state()
.store(StateKeys::ReserveX, &(reserve_x - amount_x))
.unwrap();
program
.state()
.store(StateKeys::ReserveY, &(reserve_y - amount_y))
.unwrap();
program
.state()
.store(StateKeys::TotalySupply, &(total_supply - shares))
.store([
(StateKeys::ReserveX, &(reserve_x - amount_x)),
(StateKeys::ReserveY, &(reserve_y - amount_y)),
(StateKeys::TotalySupply, &(total_supply - shares)),
])
.unwrap();

(amount_x, amount_y)
Expand Down Expand Up @@ -95,11 +87,10 @@ pub fn swap(context: Context<StateKeys>, amount_in: u64, x_to_y: bool) -> u64 {

program
.state()
.store(StateKeys::ReserveX, &reserve_x)
.unwrap();
program
.state()
.store(StateKeys::ReserveY, &reserve_y)
.store([
(StateKeys::ReserveX, &reserve_x),
(StateKeys::ReserveY, &reserve_y),
])
.unwrap();

out
Expand Down
2 changes: 1 addition & 1 deletion x/programs/rust/examples/counter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn inc(context: Context<StateKeys>, to: Address, amount: Count) -> bool {
context
.program()
.state()
.store_single(StateKeys::Counter(to), &counter)
.store_by_key(StateKeys::Counter(to), &counter)
.expect("failed to store counter");

true
Expand Down
8 changes: 4 additions & 4 deletions x/programs/rust/examples/token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ pub fn init(context: Context<StateKeys>) {
// set total supply
program
.state()
.store_single(StateKeys::TotalSupply, &INITIAL_SUPPLY)
.store_by_key(StateKeys::TotalSupply, &INITIAL_SUPPLY)
.expect("failed to store total supply");

// set token name
program
.state()
.store_single(StateKeys::Name, b"WasmCoin")
.store_by_key(StateKeys::Name, b"WasmCoin")
.expect("failed to store coin name");

// set token symbol
program
.state()
.store_single(StateKeys::Symbol, b"WACK")
.store_by_key(StateKeys::Symbol, b"WACK")
.expect("failed to store symbol");
}

Expand Down Expand Up @@ -76,7 +76,7 @@ fn mint_to_internal(

program
.state()
.store_single(StateKeys::Balance(recipient), &(balance + amount))
.store_by_key(StateKeys::Balance(recipient), &(balance + amount))
.expect("failed to store balance");

context
Expand Down
18 changes: 12 additions & 6 deletions x/programs/rust/wasmlanche-sdk/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,16 @@ impl<'a, K: Key> State<'a, K> {
pairs: Pairs,
) -> Result<(), Error> {
let cache = &mut self.cache.borrow_mut();
for (key, value) in pairs {
Self::store_internal(cache, key, value)?;
}

pairs
.into_iter()
.map(|(k, v)| borsh::to_vec(&v).map(|bytes| (k, Some(bytes))))
.try_for_each(|result| {
result.map(|(k, v)| {
cache.insert(k, v);
})
})
.map_err(|_| StateError::Serialization)?;

Ok(())
}
Expand All @@ -63,9 +70,8 @@ impl<'a, K: Key> State<'a, K> {
/// # Errors
/// Returns an [`Error`] if the key or value cannot be
/// serialized or if the host fails to handle the operation.
pub fn store_single<V: BorshSerialize>(self, key: K, value: &V) -> Result<(), Error> {
let cache = &mut self.cache.borrow_mut();
Self::store_internal(cache, key, value)
pub fn store_by_key<V: BorshSerialize>(self, key: K, value: &V) -> Result<(), Error> {
self.store([(key, value)])
}

fn store_internal<V: BorshSerialize>(
Expand Down
2 changes: 1 addition & 1 deletion x/programs/test/programs/state_access/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn put(context: Context<StateKeys>, value: i64) {
context
.program()
.state()
.store_single(StateKeys::State, &value)
.store_by_key(StateKeys::State, &value)
.expect("failed to store state");
}

Expand Down

0 comments on commit 6ab3bc0

Please sign in to comment.