From fbcd17c6f17a7ef1a746b5648cf3f13fdcf97b1a Mon Sep 17 00:00:00 2001 From: Franco Testagrossa Date: Fri, 3 May 2024 00:10:29 +0200 Subject: [PATCH] Minor formatting changes --- bounty/src/api/accept.rs | 2 +- bounty/src/api/deposit.rs | 24 ++-- bounty/src/api/icrc1.rs | 166 ++++++++++++----------- bounty/src/api/init.rs | 4 +- bounty/src/api/state.rs | 3 +- bounty/src/lib.rs | 10 +- provider/github/src/api/get_fixed_by.rs | 7 +- provider/github/src/api/get_is_merged.rs | 4 +- provider/github/src/api/get_issue.rs | 2 +- provider/github/src/client.rs | 18 +-- provider/github/src/utils.rs | 4 +- 11 files changed, 114 insertions(+), 130 deletions(-) diff --git a/bounty/src/api/accept.rs b/bounty/src/api/accept.rs index beaad81..5ee4ced 100644 --- a/bounty/src/api/accept.rs +++ b/bounty/src/api/accept.rs @@ -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() { diff --git a/bounty/src/api/deposit.rs b/bounty/src/api/deposit.rs index 323ab60..e028a39 100644 --- a/bounty/src/api/deposit.rs +++ b/bounty/src/api/deposit.rs @@ -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 }, @@ -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 @@ -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(), }); } @@ -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 diff --git a/bounty/src/api/icrc1.rs b/bounty/src/api/icrc1.rs index 2536a84..1af5635 100644 --- a/bounty/src/api/icrc1.rs +++ b/bounty/src/api/icrc1.rs @@ -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; - 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), } } } @@ -48,38 +46,45 @@ pub type TransferResult = Result; #[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), } } } @@ -87,42 +92,42 @@ impl TransferFromError { pub type TransferFromResult = Result; #[derive(Debug, Serialize, Deserialize, CandidType)] -pub struct Account { - pub owner : Principal, - pub subaccount : Option +pub struct Account { + pub owner: Principal, + 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 + 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, - pub from : Account, - pub to : Account, - pub amount : Tokens, - pub fee : Option, - pub memo : Option, - pub created_at_time: Option + pub spender_subaccount: Option, + pub from: Account, + pub to: Account, + pub amount: Tokens, + pub fee: Option, + pub memo: Option, + pub created_at_time: Option, } #[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 +pub struct Allowance { + pub allowance: Nat, + pub expires_at: Option, } pub const MAINNET_ICRC1_LEDGER_CANISTER_ID: &str = "mxzaz-hqaaa-aaaar-qaada-cai"; @@ -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; } -} \ No newline at end of file +} diff --git a/bounty/src/api/init.rs b/bounty/src/api/init.rs index 301df18..2bc87bf 100644 --- a/bounty/src/api/init.rs +++ b/bounty/src/api/init.rs @@ -1,5 +1,5 @@ -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| { @@ -7,7 +7,7 @@ pub fn init_impl(authority: Principal, github_issue_id: i32) -> () { authority, github_issue_id, interested_contributors: Vec::new(), - claimed: false + claimed: false, }); }); } diff --git a/bounty/src/api/state.rs b/bounty/src/api/state.rs index 8a91c01..b95615b 100644 --- a/bounty/src/api/state.rs +++ b/bounty/src/api/state.rs @@ -1,4 +1,3 @@ - use candid::{CandidType, Principal}; use serde::{Deserialize, Serialize}; @@ -7,7 +6,7 @@ pub struct BountyState { pub authority: Principal, pub github_issue_id: i32, pub interested_contributors: Vec, - pub claimed: bool + pub claimed: bool, } #[derive(Debug, Serialize, Deserialize, CandidType)] diff --git a/bounty/src/lib.rs b/bounty/src/lib.rs index 764d6e9..eaff610 100644 --- a/bounty/src/lib.rs +++ b/bounty/src/lib.rs @@ -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) -> () { diff --git a/provider/github/src/api/get_fixed_by.rs b/provider/github/src/api/get_fixed_by.rs index f05e9ec..c2ce1d0 100644 --- a/provider/github/src/api/get_fixed_by.rs +++ b/provider/github/src/api/get_fixed_by.rs @@ -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::>().join(", "); + + let result = fixed_by_lines + .into_iter() + .collect::>() + .join(", "); if let Some(pull_request) = extract_pull_request(&result) { return pull_request; diff --git a/provider/github/src/api/get_is_merged.rs b/provider/github/src/api/get_is_merged.rs index f9c3f92..3c32440 100644 --- a/provider/github/src/api/get_is_merged.rs +++ b/provider/github/src/api/get_is_merged.rs @@ -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, @@ -41,5 +41,3 @@ pub async fn get_is_merged_impl(owner: String, repo: String, pr_nbr: i32) -> Str } } } - - diff --git a/provider/github/src/api/get_issue.rs b/provider/github/src/api/get_issue.rs index 04e0a88..00eef82 100644 --- a/provider/github/src/api/get_issue.rs +++ b/provider/github/src/api/get_issue.rs @@ -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")); diff --git a/provider/github/src/client.rs b/provider/github/src/client.rs index 0254d2c..47601fd 100644 --- a/provider/github/src/client.rs +++ b/provider/github/src/client.rs @@ -1,6 +1,6 @@ use super::api::get_fixed_by::get_fixed_by_impl; -use super::api::get_issue::{get_issue_impl, IssueResponse}; use super::api::get_is_merged::get_is_merged_impl; +use super::api::get_issue::{get_issue_impl, IssueResponse}; use super::api::get_merged_details::{get_merge_details_impl, PrDetailsResponse}; pub struct GithubClient { @@ -20,20 +20,10 @@ impl GithubClient { .await } pub async fn get_fixed_by(&self, issue_nbr: i32) -> String { - get_fixed_by_impl( - self.owner.clone(), - self.repo.clone(), - issue_nbr, - ) - .await + get_fixed_by_impl(self.owner.clone(), self.repo.clone(), issue_nbr).await } pub async fn get_is_merged(&self, pr_nbr: i32) -> String { - get_is_merged_impl( - self.owner.clone(), - self.repo.clone(), - pr_nbr, - ) - .await + get_is_merged_impl(self.owner.clone(), self.repo.clone(), pr_nbr).await } pub async fn get_merged_details(&self, pr_nbr: i32) -> PrDetailsResponse { get_merge_details_impl( @@ -44,6 +34,4 @@ impl GithubClient { ) .await } - - } diff --git a/provider/github/src/utils.rs b/provider/github/src/utils.rs index 3803fcd..fbc2d85 100644 --- a/provider/github/src/utils.rs +++ b/provider/github/src/utils.rs @@ -1,11 +1,11 @@ use ic_cdk::api::management_canister::http_request::HttpHeader; pub fn github_api_host() -> String { - return "api.github.com".to_string(); + return "api.github.com".to_string(); } pub fn github_host() -> String { - return "github.com".to_string(); + return "github.com".to_string(); } pub fn mk_request_headers(github_token: String) -> Vec {