diff --git a/Makefile b/Makefile index 9cc3e1a..f8d1404 100644 --- a/Makefile +++ b/Makefile @@ -109,12 +109,7 @@ test-a: install cat $$TMP_FILE; \ rm -f $$TMP_FILE -.PHONY: test-a -.SILENT: test-a +.PHONY: test-deposit +.SILENT: test-deposit test-deposit: install $(shell make/test/deposit.sh) - -.PHONY: test-a -.SILENT: test-a -test-deposit-direct: install - $(shell make/test/deposit_direct.sh) \ No newline at end of file diff --git a/bounty/bounty.did b/bounty/bounty.did index 0888b69..b67ce12 100644 --- a/bounty/bounty.did +++ b/bounty/bounty.did @@ -16,6 +16,5 @@ service : (principal, int32) -> { "healthcheck": () -> (text); "accept": (Contributor) -> (); "deposit": () -> (DepositReceipt); - "deposit_direct": (amount: nat64) -> (DepositReceipt); } diff --git a/bounty/src/api/deposit.rs b/bounty/src/api/deposit.rs index 1d1cd42..26dcb5e 100644 --- a/bounty/src/api/deposit.rs +++ b/bounty/src/api/deposit.rs @@ -1,11 +1,10 @@ use super::*; -use std::convert::From; use serde::{Deserialize, Serialize}; use candid::{CandidType, Principal}; use ic_cdk::api::{caller, id}; -use ic_ledger_types::Memo; +use ic_ledger_types::{Memo, DEFAULT_SUBACCOUNT}; use icrc1::{ - Account, AllowanceArgs, Tokens, TransferArg, TransferFromArgs, ICRC1, + Account, AllowanceArgs, Tokens, TransferFromArgs, ICRC1, MAINNET_ICRC1_LEDGER_CANISTER_ID, }; @@ -49,15 +48,14 @@ async fn deposit_icrc1( let available = allowance.allowance.clone() - icrc1_token_fee.clone(); let transfer_from_args = TransferFromArgs { - // TODO check or FIXME - spender_subaccount: Some(From::from(caller)), + spender_subaccount: Some(DEFAULT_SUBACCOUNT), from: Account { owner: caller, - subaccount: Some(From::from(caller)), + subaccount: None, }, to: Account { owner: bounty_canister_id, - subaccount: Some(From::from(bounty_canister_id)), + subaccount: None, }, amount: available.clone(), fee: Some(icrc1_token_fee), @@ -74,47 +72,3 @@ async fn deposit_icrc1( reason: error.to_string(), }); } - -pub async fn deposit_direct_impl(amount: u64) -> DepositReceipt { - // FIXME check caller equals the owner who initialized the bounty. - let caller = caller(); - let icrc1_ledger_canister_id = Principal::from_text(MAINNET_ICRC1_LEDGER_CANISTER_ID) - .expect("Wrong MAINNET_ICRC1_LEDGER_CANISTER_ID"); - - return deposit_direct_icrc1(caller, icrc1_ledger_canister_id, amount).await; -} - -async fn deposit_direct_icrc1( - caller: Principal, - icrc1_token_canister_id: Principal, - amount: u64, -) -> Result { - let icrc1_token = ICRC1::new(icrc1_token_canister_id); - let icrc1_token_fee = icrc1_token.get_fee().await; - - let bounty_canister_id = id(); - - let available = amount.clone() - icrc1_token_fee.clone(); - - let transfer_args = TransferArg { - // TODO check or FIXME - from_subaccount: Some(From::from(caller)), - to: Account { - owner: bounty_canister_id, - subaccount: Some(From::from(bounty_canister_id)), - }, - amount: available.clone(), - fee: Some(icrc1_token_fee), - // TODO enhance memo text - memo: Some(Memo(1)), - created_at_time: None, - }; - - return icrc1_token - .transfer(transfer_args) - .await - .map(|_| available) - .map_err(|error| DepositErr::TransferFailure { - reason: error.to_string(), - }); -} diff --git a/bounty/src/api/icrc1.rs b/bounty/src/api/icrc1.rs index 1af5635..2643af9 100644 --- a/bounty/src/api/icrc1.rs +++ b/bounty/src/api/icrc1.rs @@ -7,43 +7,6 @@ type BlockIndex = Nat; pub type Tokens = Nat; -#[derive(Debug, Serialize, Deserialize, CandidType, PartialEq)] -pub enum TransferError { - BadFee { expected_fee: Tokens }, - BadBurn { min_burn_amount: Tokens }, - InsufficientFunds { balance: Tokens }, - TooOld, - CreatedInFuture { ledger_time: Timestamp }, - TemporarilyUnavailable, - Duplicate { duplicate_of: BlockIndex }, - GenericError { error_code: Nat, message: String }, -} - -impl TransferError { - pub fn to_string(&self) -> String { - match self { - TransferError::BadFee { expected_fee } => format!("Bad fee: {}", expected_fee), - TransferError::BadBurn { min_burn_amount } => format!("Bad burn: {}", min_burn_amount), - TransferError::InsufficientFunds { balance } => { - format!("Insufficient funds: {}", balance) - } - TransferError::TooOld => String::from("Transaction too old"), - TransferError::CreatedInFuture { ledger_time } => format!( - "Created in the future: {}", - ledger_time.timestamp_nanos.to_string() - ), - TransferError::TemporarilyUnavailable => String::from("Ledger temporarily unavailable"), - TransferError::Duplicate { duplicate_of } => format!("Duplicate of: {}", duplicate_of), - TransferError::GenericError { - error_code, - message, - } => format!("Generic error code {}: {}", error_code, message), - } - } -} - -pub type TransferResult = Result; - #[derive(Debug, Serialize, Deserialize, CandidType, PartialEq)] pub enum TransferFromError { BadFee { expected_fee: Tokens }, @@ -97,16 +60,6 @@ pub struct Account { pub subaccount: Option, } -#[derive(Debug, Serialize, Deserialize, CandidType)] -pub struct TransferArg { - pub from_subaccount: Option, - pub to: Account, - pub amount: Tokens, - pub fee: Option, - pub memo: Option, - pub created_at_time: Option, -} - #[derive(Debug, Serialize, Deserialize, CandidType)] pub struct TransferFromArgs { pub spender_subaccount: Option, @@ -141,13 +94,6 @@ impl ICRC1 { ICRC1 { principal } } - pub async fn transfer(&self, args: TransferArg) -> TransferResult { - let call_result: Result<(TransferResult,), _> = - call(self.principal, "icrc1_transfer", (args,)).await; - - return call_result.unwrap().0; - } - pub async fn transfer_from(&self, args: TransferFromArgs) -> TransferFromResult { let call_result: Result<(TransferFromResult,), _> = call(self.principal, "icrc2_transfer_from", (args,)).await; diff --git a/bounty/src/lib.rs b/bounty/src/lib.rs index eaff610..f128697 100644 --- a/bounty/src/lib.rs +++ b/bounty/src/lib.rs @@ -9,7 +9,7 @@ mod api { } use api::accept::accept_impl; -use api::deposit::{deposit_direct_impl, deposit_impl, DepositReceipt}; +use api::deposit::{deposit_impl, DepositReceipt}; use api::init::init_impl; use api::state::Contributor; @@ -28,11 +28,6 @@ async fn deposit() -> DepositReceipt { return deposit_impl().await; } -#[ic_cdk::update] -async fn deposit_direct(amount: u64) -> DepositReceipt { - return deposit_direct_impl(amount).await; -} - #[ic_cdk::update] async fn healthcheck() -> String { return "OK".to_string(); diff --git a/make/test/deposit_direct.sh b/make/test/deposit_direct.sh deleted file mode 100755 index effb3a7..0000000 --- a/make/test/deposit_direct.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash - -set -e - -CALLER=$(dfx identity get-principal) -BOUNTY="bd3sg-teaaa-aaaaa-qaaba-cai" - -# Call the bounty canister to deposit direct from caller and capture the output -echo "Calling deposit_direct on bounty canister..." - -# check initial balances -echo "Caller initial balance:" -dfx canister call icrc1_index icrc1_balance_of "(record{owner = principal \"${CALLER}\"; })" -echo "Bounty initial balance:" -dfx canister call icrc1_index icrc1_balance_of "(record{owner = principal \"${BOUNTY}\"; })" - -# deposit direct -dfx canister call bounty deposit_direct '(100_000,)' - -# check final balances -echo "Caller final balance:" -dfx canister call icrc1_index icrc1_balance_of "(record{owner = principal \"${CALLER}\"; })" -echo "Bounty final balance:" -dfx canister call icrc1_index icrc1_balance_of "(record{owner = principal \"${BOUNTY}\"; })"