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

Feat: retry handle message #72

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6b969be
add cap as storage service to eth_proxy
Sep 9, 2022
b609899
add cap storage service for claimable message on dip20_proxy
Sep 20, 2022
05942ac
upgrade cap_cdk and use insert_sync on eth_proxy
Sep 21, 2022
0d16aac
upgrade cap_cdk and use insert_sync on dip20_proxy
Sep 21, 2022
d618c7d
rename trait ToEvent to ToCapEvent
Sep 21, 2022
d769dba
remove claimable_assets candid methods from dip20_proxy
Sep 21, 2022
f9450c4
use archive() fn on upgrade
Sep 23, 2022
db17315
use archive() fn on upgrade eth_proxy
Sep 23, 2022
c19a962
eth_proxy: add caller to claimable message
Oct 18, 2022
69c0b08
dip20_proxy: add caller to claimable message
Oct 18, 2022
f6cc64c
add eth_contract_address to withdraw payload
Sep 22, 2022
bb5b837
add claimable_asset in withdraw for dip20_proxy
Sep 22, 2022
3281179
add claimable_asset in withdraw for eth_proxy
Sep 22, 2022
cebc5e8
use eth address as principal to keep balance
Sep 23, 2022
230b663
return Ok(balance) on eth_proxy.withdraw
Sep 23, 2022
b7fa4ce
add withdrawable struct to eth_proxy
Sep 27, 2022
b56a20c
add withdrawable struct to dip20_proxy
Sep 27, 2022
9f82039
remove token_id from withdrawable balance on eth_proxy
Sep 27, 2022
eca1bfa
Fix typo TokendId -> TokenId
Sep 28, 2022
5af4f7e
dip20_proxy: replace Vec for hashmap to store usertx
Sep 29, 2022
a810f52
eth_proxy: replace Vec for hashmap to store usertx
Sep 29, 2022
1889726
dip20_proxy: add operation failure on burn and withdraw
Oct 6, 2022
c933397
eth_proxy: add operation failure on burn and withdraw
Oct 6, 2022
ec846e3
dip20_proxy: refactor pending balance logic
Oct 12, 2022
c312ecf
eth_proxy: refactor pending balance logic
Oct 12, 2022
44d66f9
add caller to claimable message on withdraw
Oct 18, 2022
8f32e39
chore: out going message store (#38)
fcavazzoli Oct 18, 2022
0f3e67e
add account contract (#68)
fcavazzoli Oct 31, 2022
a2d95dc
starknet: increase max fee (#70)
fcavazzoli Nov 3, 2022
8342d37
core: add nonce_exist candid method
Nov 4, 2022
f674f32
dip20_proxy: add message_exist candid method query
Nov 4, 2022
756fbf7
dip20_proxy: update candid file
Nov 4, 2022
0b896dd
solve master conflicts
Nov 10, 2022
91b35b8
fix upgrade with Option<>
Nov 10, 2022
fbc0b7f
resolve merge conflict
Nov 11, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
upgrade cap_cdk and use insert_sync on dip20_proxy
  • Loading branch information
Federico authored and Federico committed Sep 21, 2022
commit 0d16aac6efb9443a1c3b2c658bc856be5109426c
66 changes: 58 additions & 8 deletions magic_bridge/ic/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 magic_bridge/ic/src/dip20_proxy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -19,4 +19,4 @@ serde = "1.0.130"
serde_bytes = "0.11.5"
num-bigint = "0.4.3"
cap-std = { git = "https://github.com/Psychedelic/cap", branch = "main", package="cap-standards", features = ["alpha-dip20", "cap-sdk", "sdk-impls"] }
cap-sdk = { git = "https://github.com/Psychedelic/cap.git", branch = "main" }
cap-sdk = "0.2.4"
18 changes: 8 additions & 10 deletions magic_bridge/ic/src/dip20_proxy/src/api/burn.rs
Original file line number Diff line number Diff line change
@@ -94,17 +94,15 @@ async fn burn(
s.remove_user_flag(caller, token_id)
});

ic_kit::ic::spawn(async move {
insert_claimable_asset(ClaimableMessage {
owner: eth_addr.clone(),
msg_hash: outgoing_message.msg_hash.clone(),
msg_key: outgoing_message.msg_key.clone(),
token_name: token_name_str,
token: token_id.clone(),
amount: amount.clone(),
})
.await
insert_claimable_asset(ClaimableMessage {
owner: eth_addr.clone(),
msg_hash: outgoing_message.msg_hash.clone(),
msg_key: outgoing_message.msg_key.clone(),
token_name: token_name_str,
token: token_id.clone(),
amount: amount.clone(),
});

// All correct
return Ok(burn_txn_id);
}
17 changes: 16 additions & 1 deletion magic_bridge/ic/src/dip20_proxy/src/api/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use crate::proxy::ToEvent;
use cap_sdk::{pending_transactions, restore_pending_transactions};
use ic_kit::ic;
use ic_kit::macros::{post_upgrade, pre_upgrade};

use crate::common::types::StableProxyState;
use crate::common::types::{ClaimableMessage, StableProxyState};
use crate::proxy::STATE;

#[pre_upgrade]
fn pre_upgrade() {
let cap_pending_tx = pending_transactions();
let _ = cap_pending_tx
.iter()
.map(|tx| STATE.with(|s| s.add_claimable_message(ClaimableMessage::from(tx.to_owned()))));

let stable_magic_state = STATE.with(|s| s.take_all());

ic::stable_store((stable_magic_state,)).expect("failed to messsage state");
@@ -19,4 +26,12 @@ fn post_upgrade() {
ic::stable_restore().expect("failed to restore stable messsage state");

STATE.with(|s| s.replace_all(stable_message_state));

let pending_cap_tx = STATE.with(|s| s.get_all_claimable_messages());
let events = pending_cap_tx
.iter()
.map(|m| m.to_owned().to_cap_event())
.collect();

restore_pending_transactions(events)
}
35 changes: 6 additions & 29 deletions magic_bridge/ic/src/dip20_proxy/src/common/cap.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,12 @@
use cap_sdk::{insert, IndefiniteEvent};
use cap_sdk::insert_sync;

use crate::proxy::{ToEvent, STATE};
use crate::proxy::ToEvent;

use super::types::ClaimableMessage;

pub async fn insert_claimable_asset(message: ClaimableMessage) {
// add message to STATE claimable messages queue.
STATE.with(|s| {
s.add_claimable_message(message.clone());
});

// flush STATE claimable messages queue
register_messages().await
}

pub async fn register_messages() {
let mut pending_registrations = STATE.with(|s| s.get_all_claimable_messages());
let mut i = 0;
while i < pending_registrations.len() {
if insert_into_cap(pending_registrations[i].to_cap_event()).await {
STATE.with(|s| s.remove_claimable_message(pending_registrations[i].clone()));
pending_registrations.swap_remove(i);
} else {
i += 1;
}
}
}

async fn insert_into_cap(event: IndefiniteEvent) -> bool {
match insert(event.clone()).await {
Ok(_nat) => true,
Err(_error) => false,
pub fn insert_claimable_asset(message: ClaimableMessage) {
pub fn insert_claimable_asset(message: ClaimableMessage) {
let event = message.to_cap_event();
insert_sync(event);
}
}
32 changes: 26 additions & 6 deletions magic_bridge/ic/src/dip20_proxy/src/proxy.rs
Original file line number Diff line number Diff line change
@@ -283,6 +283,7 @@ impl ToEvent for ClaimableMessage {
.insert("msgHash", self.msg_hash.clone())
.insert("msgHashKey", self.msg_key.to_nat())
.insert("amount", self.amount.clone())
.insert("name", self.token_name.clone())
.build();

IndefiniteEventBuilder::new()
@@ -294,6 +295,25 @@ impl ToEvent for ClaimableMessage {
}
}

impl From<IndefiniteEvent> for ClaimableMessage {
fn from(event: IndefiniteEvent) -> Self {
let msg_key: Nat = event.details[3].1.clone().try_into().unwrap();
let msg_hash: String = event.details[2].1.clone().try_into().unwrap();
let token: Principal = event.details[1].1.clone().try_into().unwrap();
let amount: Nat = event.details[4].1.clone().try_into().unwrap();
let name: String = event.details[5].1.clone().try_into().unwrap();

ClaimableMessage {
owner: event.caller,
msg_key: msg_key.to_nonce_bytes(),
msg_hash: msg_hash,
token: token,
amount: amount,
token_name: name,
}
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;
@@ -613,23 +633,23 @@ mod tests {
};

// add first msg
STATE.with(|s| s.add_claimable_message(message_1));
STATE.with(|s| s.add_claimable_message(message_1.clone()));
// add second msg
STATE.with(|s| s.add_claimable_message(message_2));
STATE.with(|s| s.add_claimable_message(message_2.clone()));

// check if both messages are in the claimable messages list for eth_addr_1
let mut claimable_messages = STATE.with(|s| s.get_claimable_messages(eth_addr_1.clone()));
assert_eq!(claimable_messages.len(), 2);

// remove one msg -> the one with amount_2 (both are the same token, but different amount)
let result_remove_1 = STATE.with(|s| s.remove_claimable_message(message_1.clone()));
// remove one msg -> the one with amount_1 (both are the same token, but different amount)
let _ = STATE.with(|s| s.remove_claimable_message(message_1.clone()));

// check if only one message is in the claimable messages list for eth_addr_1
claimable_messages = STATE.with(|s| s.get_claimable_messages(eth_addr_1.clone()));
assert_eq!(claimable_messages.len(), 1);

// the message that is left is the one with amount_1
assert_eq!(claimable_messages[0].amount, amount_1);
// the message that is left is the one with amount_2
assert_eq!(claimable_messages[0].amount, amount_2);
}

#[test]