Skip to content

Commit

Permalink
Remove deposit-direct
Browse files Browse the repository at this point in the history
  • Loading branch information
ffakenz committed May 2, 2024
1 parent 9d0d496 commit 15436c0
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 143 deletions.
9 changes: 2 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 0 additions & 1 deletion bounty/bounty.did
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ service : (principal, int32) -> {
"healthcheck": () -> (text);
"accept": (Contributor) -> ();
"deposit": () -> (DepositReceipt);
"deposit_direct": (amount: nat64) -> (DepositReceipt);
}

56 changes: 5 additions & 51 deletions bounty/src/api/deposit.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand Down Expand Up @@ -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),
Expand All @@ -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<Tokens, DepositErr> {
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(),
});
}
54 changes: 0 additions & 54 deletions bounty/src/api/icrc1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockIndex, TransferError>;

#[derive(Debug, Serialize, Deserialize, CandidType, PartialEq)]
pub enum TransferFromError {
BadFee { expected_fee: Tokens },
Expand Down Expand Up @@ -97,16 +60,6 @@ pub struct Account {
pub subaccount: Option<Subaccount>,
}

#[derive(Debug, Serialize, Deserialize, CandidType)]
pub struct TransferArg {
pub from_subaccount: Option<Subaccount>,
pub to: Account,
pub amount: Tokens,
pub fee: Option<Tokens>,
pub memo: Option<Memo>,
pub created_at_time: Option<Timestamp>,
}

#[derive(Debug, Serialize, Deserialize, CandidType)]
pub struct TransferFromArgs {
pub spender_subaccount: Option<Subaccount>,
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 1 addition & 6 deletions bounty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down
24 changes: 0 additions & 24 deletions make/test/deposit_direct.sh

This file was deleted.

0 comments on commit 15436c0

Please sign in to comment.