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

Fix: withdraw pending balance #64

Merged
merged 27 commits into from
Nov 10, 2022
Merged

Fix: withdraw pending balance #64

merged 27 commits into from
Nov 10, 2022

Conversation

fcavazzoli
Copy link
Contributor

@fcavazzoli fcavazzoli commented Sep 22, 2022

Description

  • Add magic_bride call in dip20_proxy to get token canister id
  • add ClaimableMessage if send message is success for both eth and dpi20 proxy

Changes

  • New attributes definition for Withdraw withdraw(eth_contract_as_principal: TokendId, eth_addr: EthereumAddr, _amount: Nat,)
  • New struct pub struct WithdrawableBalance(pub Vec<(String, String, Nat)>)

@fcavazzoli fcavazzoli changed the title Fix/withdraw payload Fix: withdraw payload Sep 22, 2022
@fcavazzoli fcavazzoli marked this pull request as ready for review September 22, 2022 12:01
@fcavazzoli fcavazzoli force-pushed the fix/withdraw_payload branch 3 times, most recently from e931607 to 1f31b75 Compare September 27, 2022 15:03
@fcavazzoli fcavazzoli changed the title Fix: withdraw payload Fix: withdraw pending balance Sep 27, 2022
@fcavazzoli fcavazzoli linked an issue Sep 27, 2022 that may be closed by this pull request
Copy link
Contributor

@b0xtch b0xtch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments regarding data structures and one spelling mistake across the project TokendId -> TokenId

@@ -77,7 +80,7 @@ pub struct ProxyState {
/// store incoming messages against status locks
pub incoming_messages: RefCell<HashMap<MessageHash, MessageStatus>>,
/// user balances
pub balances: RefCell<HashMap<Principal, HashMap<TokendId, Nat>>>,
pub balances: RefCell<HashMap<Principal, Vec<(Principal, Nat)>>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub balances: RefCell<HashMap<Principal, Vec<(Principal, Nat)>>>,
pub balances: RefCell<HashMap<EthereumAddr, Nat>>,

Could we narrow it down to this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we need caller then ignore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need the caller

@@ -91,7 +94,7 @@ pub struct StableProxyState {
/// store incoming messages against status locks
pub incoming_messages: HashMap<MessageHash, MessageStatus>,
/// user balances
pub balances: HashMap<Principal, HashMap<Principal, Nat>>,
pub balances: Option<HashMap<Principal, Vec<(Principal, Nat)>>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub balances: Option<HashMap<Principal, Vec<(Principal, Nat)>>>,
pub balances: RefCell<HashMap<EthereumAddr, Nat>>,

The same usage can be applied here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also Is Optional being committed or only required for upgrades?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is required for the upgrades

Comment on lines 69 to 76
let mut binding = self.balances.borrow_mut();
let balance = binding.entry(caller).or_default();
match balance.into_iter().find(|tx| tx.0 == to) {
Some(value) => value.1 += amount,
None => {
balance.push((to, amount));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using a hashmap in the original structure could simplify this a lot

RefCell<HashMap<Principal, HashMap<EthereumAddr, Nat>>>

Comment on lines 80 to 90
pub fn update_balance(&self, caller: Principal, to: Principal, amount: Nat) {
let mut binding = self.balances.borrow_mut();
let user_balances = binding.get_mut(&caller).unwrap();
// if new amount is zero, we remove the tx.
if amount == 0 {
let index = user_balances.into_iter().position(|tx| tx.0 == to).unwrap();
user_balances.remove(index);
} else {
let mut balance = user_balances.iter_mut().find(|tx| tx.0 == to).unwrap();
balance.1 = amount;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, simplifying our structure

@@ -78,7 +81,7 @@ pub struct ProxyState {
/// store incoming messages against status locks
pub incoming_messages: RefCell<HashMap<MessageHash, MessageStatus>>,
/// user balances
pub balances: RefCell<HashMap<Principal, HashMap<TokendId, Nat>>>,
pub balances: RefCell<HashMap<Principal, HashMap<TokendId, Vec<(Principal, Nat)>>>>,
Copy link
Contributor

@b0xtch b0xtch Sep 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same idea as the eth_proxy balances structure.

  • Simplify
  • and if caller is not needed truncate to:
RefCell<HashMap<EthereumAddr, HashMap<TokenId, , Nat>>>,

Some(balance) => balance.clone(),
None => Nat::from(0_u32),
})
pub fn get_balance(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be simplified, if the structure is also simplified

@fcavazzoli fcavazzoli force-pushed the fix/withdraw_payload branch 2 times, most recently from be0acdc to 748fc0a Compare October 6, 2022 14:22
@fcavazzoli fcavazzoli force-pushed the chore/use_cap_for_claimable_assets branch from 69c0b08 to 765207d Compare November 7, 2022 18:04
Base automatically changed from chore/use_cap_for_claimable_assets to master November 8, 2022 00:20
@fcavazzoli fcavazzoli merged commit 68e4c4b into master Nov 10, 2022
@fcavazzoli fcavazzoli deleted the fix/withdraw_payload branch November 10, 2022 01:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fix: withdraw pending balances
2 participants