Skip to content

Commit

Permalink
Minor formatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ffakenz committed May 2, 2024
1 parent 3c60b52 commit fbcd17c
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 130 deletions.
2 changes: 1 addition & 1 deletion bounty/src/api/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub fn accept_impl(contributor: Contributor) -> () {
#[cfg(test)]
mod test_accept {
use super::*;
use candid::Principal;
use crate::api::init::init_impl;
use candid::Principal;

#[test]
fn test_accept() {
Expand Down
24 changes: 8 additions & 16 deletions bounty/src/api/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
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 super::*;

use icrc1::{
Account, AllowanceArgs, Tokens, TransferArg, TransferFromArgs, ICRC1,
MAINNET_ICRC1_LEDGER_CANISTER_ID,
};

use candid::{CandidType, Principal};

use std::convert::From;

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, CandidType, PartialEq)]
pub enum DepositErr {
TransferFailure { reason: String },
Expand Down Expand Up @@ -51,7 +46,7 @@ async fn deposit_icrc1(
};
let allowance = icrc1_token.allowance(allowance_args).await;

let available = allowance.allowance - icrc1_token_fee.clone();
let available = allowance.allowance.clone() - icrc1_token_fee.clone();

let transfer_from_args = TransferFromArgs {
// TODO check or FIXME
Expand All @@ -75,11 +70,8 @@ async fn deposit_icrc1(
.transfer_from(transfer_from_args)
.await
.map(|_| available)
.map_err(|error| {
print!("PEPE");
DepositErr::TransferFailure {
reason: error.to_string(),
}
.map_err(|error| DepositErr::TransferFailure {
reason: error.to_string(),
});
}

Expand All @@ -102,7 +94,7 @@ async fn deposit_direct_icrc1(

let bounty_canister_id = id();

let available = amount - icrc1_token_fee.clone();
let available = amount.clone() - icrc1_token_fee.clone();

let transfer_args = TransferArg {
// TODO check or FIXME
Expand Down
166 changes: 85 additions & 81 deletions bounty/src/api/icrc1.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
use candid::{CandidType, Nat, Principal};
use ic_ledger_types::{Memo, Subaccount, Timestamp};
use serde::{Deserialize, Serialize};
use candid::{CandidType, Nat, Principal};
use ic_cdk::api::call::call;
use ic_ledger_types::{Memo, Subaccount, Timestamp};

type BlockIndex = Nat;

// pub type Blob = Vec<u8>;

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 },
BadFee { expected_fee: Tokens },
BadBurn { min_burn_amount: Tokens },
InsufficientFunds { balance: Tokens },
TooOld,
CreatedInFuture { ledger_time : Timestamp },
CreatedInFuture { ledger_time: Timestamp },
TemporarilyUnavailable,
Duplicate { duplicate_of : BlockIndex },
GenericError { error_code : Nat, message : String },
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)
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),
}
}
}
Expand All @@ -48,81 +46,88 @@ pub type TransferResult = Result<BlockIndex, TransferError>;

#[derive(Debug, Serialize, Deserialize, CandidType, PartialEq)]
pub enum TransferFromError {
BadFee { expected_fee : Tokens },
BadBurn { min_burn_amount : Tokens },
InsufficientFunds { balance : Tokens },
InsufficientAllowance { allowance : Tokens },
BadFee { expected_fee: Tokens },
BadBurn { min_burn_amount: Tokens },
InsufficientFunds { balance: Tokens },
InsufficientAllowance { allowance: Tokens },
TooOld,
CreatedInFuture { ledger_time : Timestamp },
CreatedInFuture { ledger_time: Timestamp },
TemporarilyUnavailable,
Duplicate { duplicate_of : BlockIndex },
GenericError { error_code : Nat, message : String },
Duplicate { duplicate_of: BlockIndex },
GenericError { error_code: Nat, message: String },
}

impl TransferFromError {
pub fn to_string(&self) -> String {
match self {
TransferFromError::BadFee { expected_fee } =>
format!("Bad fee: {}", expected_fee),
TransferFromError::BadBurn { min_burn_amount } =>
format!("Bad burn: {}", min_burn_amount),
TransferFromError::InsufficientFunds { balance } =>
format!("Insufficient funds: {}", balance),
TransferFromError::InsufficientAllowance { allowance } =>
format!("Insufficient allowance: {}", allowance),
TransferFromError::TooOld =>
String::from("Transaction too old"),
TransferFromError::CreatedInFuture { ledger_time } =>
format!("Created in the future: {}", ledger_time.timestamp_nanos.to_string()),
TransferFromError::TemporarilyUnavailable =>
String::from("Ledger temporarily unavailable"),
TransferFromError::Duplicate { duplicate_of } =>
format!("Duplicate of: {}", duplicate_of),
TransferFromError::GenericError { error_code, message } =>
format!("Generic error code {}: {}", error_code, message)
TransferFromError::BadFee { expected_fee } => format!("Bad fee: {}", expected_fee),
TransferFromError::BadBurn { min_burn_amount } => {
format!("Bad burn: {}", min_burn_amount)
}
TransferFromError::InsufficientFunds { balance } => {
format!("Insufficient funds: {}", balance)
}
TransferFromError::InsufficientAllowance { allowance } => {
format!("Insufficient allowance: {}", allowance)
}
TransferFromError::TooOld => String::from("Transaction too old"),
TransferFromError::CreatedInFuture { ledger_time } => format!(
"Created in the future: {}",
ledger_time.timestamp_nanos.to_string()
),
TransferFromError::TemporarilyUnavailable => {
String::from("Ledger temporarily unavailable")
}
TransferFromError::Duplicate { duplicate_of } => {
format!("Duplicate of: {}", duplicate_of)
}
TransferFromError::GenericError {
error_code,
message,
} => format!("Generic error code {}: {}", error_code, message),
}
}
}

pub type TransferFromResult = Result<BlockIndex, TransferFromError>;

#[derive(Debug, Serialize, Deserialize, CandidType)]
pub struct Account {
pub owner : Principal,
pub subaccount : Option<Subaccount>
pub struct Account {
pub owner: Principal,
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>
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>,
pub from : Account,
pub to : Account,
pub amount : Tokens,
pub fee : Option<Tokens>,
pub memo : Option<Memo>,
pub created_at_time: Option<Timestamp>
pub spender_subaccount: Option<Subaccount>,
pub from: Account,
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 AllowanceArgs {
pub account : Account,
pub spender : Account
pub struct AllowanceArgs {
pub account: Account,
pub spender: Account,
}

#[derive(Debug, Serialize, Deserialize, CandidType)]
pub struct Allowance {
pub allowance : Nat,
pub expires_at : Option<Timestamp>
pub struct Allowance {
pub allowance: Nat,
pub expires_at: Option<Timestamp>,
}

pub const MAINNET_ICRC1_LEDGER_CANISTER_ID: &str = "mxzaz-hqaaa-aaaar-qaada-cai";
Expand All @@ -139,28 +144,27 @@ impl ICRC1 {
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

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;

return call_result.unwrap().0
return call_result.unwrap().0;
}

pub async fn allowance(&self, args: AllowanceArgs) -> Allowance {
let call_result: Result<(Allowance, ), _> =
let call_result: Result<(Allowance,), _> =
call(self.principal, "icrc2_allowance", (args,)).await;

return call_result.unwrap().0;
}

pub async fn get_fee(&self) -> Nat {
let call_result: Result<(Nat,), _> =
call(self.principal, "icrc1_fee", ()).await;
let call_result: Result<(Nat,), _> = call(self.principal, "icrc1_fee", ()).await;

return call_result.unwrap().0
return call_result.unwrap().0;
}
}
}
4 changes: 2 additions & 2 deletions bounty/src/api/init.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use candid::Principal;
use super::state::{BountyState, BOUNTY_STATE};
use candid::Principal;

pub fn init_impl(authority: Principal, github_issue_id: i32) -> () {
BOUNTY_STATE.with(|state| {
*state.borrow_mut() = Some(BountyState {
authority,
github_issue_id,
interested_contributors: Vec::new(),
claimed: false
claimed: false,
});
});
}
Expand Down
3 changes: 1 addition & 2 deletions bounty/src/api/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use candid::{CandidType, Principal};
use serde::{Deserialize, Serialize};

Expand All @@ -7,7 +6,7 @@ pub struct BountyState {
pub authority: Principal,
pub github_issue_id: i32,
pub interested_contributors: Vec<Contributor>,
pub claimed: bool
pub claimed: bool,
}

#[derive(Debug, Serialize, Deserialize, CandidType)]
Expand Down
10 changes: 5 additions & 5 deletions bounty/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use candid::Principal;

mod api {
pub mod state;
pub mod init;
pub mod accept;
pub mod deposit;
pub mod icrc1;
pub mod init;
pub mod state;
}

use api::state::Contributor;
use api::init::init_impl;
use api::accept::accept_impl;
use api::deposit::{deposit_impl, deposit_direct_impl, DepositReceipt};
use api::deposit::{deposit_direct_impl, deposit_impl, DepositReceipt};
use api::init::init_impl;
use api::state::Contributor;

#[ic_cdk::init]
fn init(authority: Principal, github_issue_id: i32) -> () {
Expand Down
7 changes: 5 additions & 2 deletions provider/github/src/api/get_fixed_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ pub async fn get_fixed_by_impl(owner: String, repo: String, issue_nbr: i32) -> S
}
set
});

let result = fixed_by_lines.into_iter().collect::<Vec<String>>().join(", ");

let result = fixed_by_lines
.into_iter()
.collect::<Vec<String>>()
.join(", ");

if let Some(pull_request) = extract_pull_request(&result) {
return pull_request;
Expand Down
4 changes: 1 addition & 3 deletions provider/github/src/api/get_is_merged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub async fn get_is_merged_impl(owner: String, repo: String, pr_nbr: i32) -> Str
pr_nbr
);

// Create the request argument
// Create the request argument
let request = CanisterHttpRequestArgument {
url: url.to_string(),
method: HttpMethod::GET,
Expand All @@ -41,5 +41,3 @@ pub async fn get_is_merged_impl(owner: String, repo: String, pr_nbr: i32) -> Str
}
}
}


2 changes: 1 addition & 1 deletion provider/github/src/api/get_issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn transform_response(raw_response: HttpResponse) -> IssueResponse {
id,
milestone_state,
closed_at,
reason
reason,
})
})
.unwrap_or_else(|| panic!("Failed to extract fields from parsed response"));
Expand Down
Loading

0 comments on commit fbcd17c

Please sign in to comment.