Skip to content

Instantly share code, notes, and snippets.

@SuddenlyHazel
Last active June 1, 2021 03:35
Show Gist options
  • Save SuddenlyHazel/8a482f38e3e3a21811e266d32ddb83d2 to your computer and use it in GitHub Desktop.
Save SuddenlyHazel/8a482f38e3e3a21811e266d32ddb83d2 to your computer and use it in GitHub Desktop.
Events with Method References
#[derive(CandidType, Deserialize)]
struct NotifyArgs {
pub callback: candid::Func,
pub transaction_id: candid::Nat,
pub metadata: String,
}
#[derive(CandidType, Deserialize)]
enum TransferError {
InsufficientBalance,
AmountTooLarge,
CallFailed,
Unknown
}
#[derive(CandidType, Deserialize)]
enum TransferResult {
Ok(candid::Nat),
Err(TransferError),
}
#[derive(CandidType, Deserialize)]
struct TransferRequest {
pub from: Option<candid::Principal>,
pub to: candid::Principal,
pub amount: candid::Nat,
pub notify: Option<NotifyArgs>,
}
#[update]
async fn transfer(transfer_request : TransferRequest) -> TransferResult {
// Transfer logic above...
let a = transfer_request.notify.borrow();
ic_cdk::print(a.is_some().to_string());
match transfer_request.notify {
Some(v) => {
ic_cdk::print("Here");
let _ = call::<(String,),()>(v.callback.principal, v.callback.method.as_str(), (v.metadata, )).await;
}
_ => {}
}
return TransferResult::Ok(candid::Nat::from(0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment