Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 30 verify donation input #41

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 83 additions & 15 deletions backend/donation_tracker_canister/src/Main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ actor class DonationTracker(_donation_canister_id : Text) {
stable var donationsStable : [Donation] = [];

// Map each principal to a list of donation indices (DTIs)
private var donationsByPrincipal = HashMap.HashMap<Principal, Buffer.Buffer<DTI>>(0, Principal.equal, Principal.hash);
//TODO: stable var donationsByPrincipalStable : [(Principal, Buffer.Buffer<DTI>)] = [];
// Alternative: use [DTI] instead of Buffer.Buffer<DTI>; less efficient but straightforward to make stable
// Or: stable Buffer implementation?
private var donationsByPrincipal = HashMap.HashMap<Principal, [DTI]>(0, Principal.equal, Principal.hash);
stable var donationsByPrincipalStable : [(Principal, [DTI])] = []; // Alternative: Buffer.Buffer<DTI> instead of [DTI]; more efficient but less straightforward to make stable

// Store recipients and map each recipientId to the corresponding Recipient record
private var recipientsById = HashMap.HashMap<Types.RecipientId, Types.Recipient>(0, Text.equal, Text.hash);
Expand Down Expand Up @@ -193,9 +191,79 @@ actor class DonationTracker(_donation_canister_id : Text) {

};

private func verifyDonationInput(donationInput : Donation) : async Bool {
// Perform basic checks
// Total amount has to be a positive number
if (donationInput.totalAmount <= 0) {
return false;
};

// Verify that recipientId exists
switch (recipientsById.get(donationInput.recipientId)) {
case (null) { return false; };
case (?recipient) { };
};

// Verify valid allocation
var totalAllocated = donationInput.allocation.curriculumDesign;
totalAllocated += donationInput.allocation.teacherSupport;
totalAllocated += donationInput.allocation.schoolSupplies;
totalAllocated += donationInput.allocation.lunchAndSnacks;
if (totalAllocated != donationInput.totalAmount) {
return false;
};

// Check that personalNote is not longer than 100 characters
switch (donationInput.personalNote) {
case (null) { };
case (?note) {
if (note.size() > 100) {
return false;
};
};
};

// Elaborate check whether paymentType is valid and paymentTransactionId exists and can be used
switch (donationInput.paymentType) {
case (#BTC) {
// Verify the Bitcoin transaction
try {
let txCheckResult = await getBtcTransactionDetails({bitcoinTransactionId = donationInput.paymentTransactionId});
switch (txCheckResult) {
case (#Ok(bitcoinTransactionRecord)) {
// bitcoinTransactionId was found and has value
let bitcoinTransaction = bitcoinTransactionRecord.bitcoinTransaction;
// Check that value left on transaction is high enough to donate totalAmount
let valueLeft = bitcoinTransaction.totalValue - bitcoinTransaction.valueDonated;
if (valueLeft <= 0) {
// the transaction doesn't have any value left to donate
return false;
} else if (valueLeft < donationInput.totalAmount) {
// the transaction doesn't have enough value left to donate totalAmount
return false;
};
};
case (_) { return false; }; // bitcoinTransactionId wasn't found or doesn't have value bigger 0
};
} catch (error : Error) {
return false;
};
};
// Handle other payment types as they are added
case (_) { return false; }; // Fallback: unsupported paymentType
};

// All checks were successful and the donation input is valid
return true;
};

public shared (msg) func makeDonation(donationRecord : Types.DonationRecord) : async Types.DtiResult {
let donationInput = donationRecord.donation;
// Potential TODO: checks on inputs
let donationInputIsValid : Bool = await verifyDonationInput(donationInput);
if(not donationInputIsValid) {
return #Err(#Other("Invalid Donation input"));
};

let newDti = donations.size(); // Simply use index into donations Array as the DTI
var newDonor : Types.DonorType = #Anonymous;
Expand All @@ -215,19 +283,19 @@ actor class DonationTracker(_donation_canister_id : Text) {
donor : Types.DonorType = newDonor;
personalNote : ?Text = donationInput.personalNote; // Optional field for personal note from donor to recipient
rewardsHaveBeenClaimed : Bool = false;
hasBeenDistributed : Bool = false; // TODO: placeholder for future functionality
hasBeenDistributed : ?Bool = ?false; // TODO: placeholder for future functionality
};

let newDonationResult = donations.add(newDonation);

// Update the map for the caller's principal
if (Principal.isAnonymous(msg.caller)) {} else {
let existingDonations = switch (donationsByPrincipal.get(msg.caller)) {
case (null) { Buffer.Buffer<DTI>(0) };
let existingDonations : [DTI] = switch (donationsByPrincipal.get(msg.caller)) {
case (null) { [] };
case (?ds) { ds };
};
let addDonationResult = existingDonations.add(newDti);
donationsByPrincipal.put(msg.caller, existingDonations);
let addDonationResult : [DTI] = Array.append<DTI>(existingDonations, [newDti]);
donationsByPrincipal.put(msg.caller, addDonationResult);
};

let associatedDonations = switch (donationsByTxId.get(donationInput.paymentTransactionId)) {
Expand Down Expand Up @@ -273,12 +341,12 @@ actor class DonationTracker(_donation_canister_id : Text) {
// No donations found
return #Ok({ donations = [] });
};
case (?dtiBuffer) {
case (?dtiArray) {
// Donations found for user
let dtis : [DTI] = Buffer.toArray(dtiBuffer);
let dtis : [DTI] = dtiArray;
// Iterate over dtis, get donation for each dti
// push to return array
let userDonations : Buffer.Buffer<Donation> = Buffer.Buffer<Donation>(dtiBuffer.capacity());
let userDonations : Buffer.Buffer<Donation> = Buffer.Buffer<Donation>(dtiArray.size());
for (i : Nat in dtis.keys()) {
userDonations.add(donations.get(dtis[i]));
};
Expand Down Expand Up @@ -587,7 +655,7 @@ actor class DonationTracker(_donation_canister_id : Text) {
system func preupgrade() {
// Copy the runtime state back into the stable variable before upgrade.
donationsStable := Buffer.toArray<Donation>(donations);
//TODO: donationsByPrincipalStable := Iter.toArray(donationsByPrincipal.entries());
donationsByPrincipalStable := Iter.toArray(donationsByPrincipal.entries());
recipientsByIdStable := Iter.toArray(recipientsById.entries());
studentsBySchoolStable := Iter.toArray(studentsBySchool.entries());
donationsByTxIdStable := Iter.toArray(donationsByTxId.entries());
Expand All @@ -599,8 +667,8 @@ actor class DonationTracker(_donation_canister_id : Text) {
// After upgrade, reload the runtime state from the stable variable.
donations := Buffer.fromArray<Donation>(donationsStable);
donationsStable := [];
//TODO: donationsByPrincipal := HashMap.fromIter(Iter.fromArray(donationsByPrincipalStable), donationsByPrincipalStable.size(), Text.equal, Text.hash);
//TODO: donationsByPrincipalStable := [];
donationsByPrincipal := HashMap.fromIter(Iter.fromArray(donationsByPrincipalStable), donationsByPrincipalStable.size(), Principal.equal, Principal.hash);
donationsByPrincipalStable := [];
recipientsById := HashMap.fromIter(Iter.fromArray(recipientsByIdStable), recipientsByIdStable.size(), Text.equal, Text.hash);
recipientsByIdStable := [];
studentsBySchool := HashMap.fromIter(Iter.fromArray(studentsBySchoolStable), studentsBySchoolStable.size(), Text.equal, Text.hash);
Expand Down
2 changes: 1 addition & 1 deletion backend/donation_tracker_canister/src/Types.mo
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module Types {
donor : DonorType;
personalNote : ?Text; // Optional field for personal note from donor to recipient
rewardsHaveBeenClaimed : Bool;
hasBeenDistributed : Bool; // TODO: placeholder for future functionality
hasBeenDistributed : ?Bool; // TODO: placeholder for future functionality
};

//-------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ type Donation =
allocation: DonationCategories;
donor: DonorType;
dti: DTI;
hasBeenDistributed: bool;
hasBeenDistributed: opt bool;
paymentTransactionId: PaymentTransactionId;
paymentType: PaymentType;
personalNote: opt text;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Principal } from '@dfinity/principal';
import type { ActorMethod } from '@dfinity/agent';
import type { IDL } from '@dfinity/candid';

export type ApiError = { 'InvalidId' : null } |
{ 'ZeroAddress' : null } |
Expand Down Expand Up @@ -28,7 +27,7 @@ export interface Donation {
'dti' : DTI,
'rewardsHaveBeenClaimed' : boolean,
'paymentTransactionId' : PaymentTransactionId,
'hasBeenDistributed' : boolean,
'hasBeenDistributed' : [] | [boolean],
'totalAmount' : Satoshi,
'timestamp' : bigint,
'paymentType' : PaymentType,
Expand Down Expand Up @@ -183,5 +182,3 @@ export interface Utxo {
'outpoint' : OutPoint,
}
export interface _SERVICE extends DonationTracker {}
export declare const idlFactory: IDL.InterfaceFactory;
export declare const init: ({ IDL }: { IDL: IDL }) => IDL.Type[];
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const idlFactory = ({ IDL }) => {
'dti' : DTI,
'rewardsHaveBeenClaimed' : IDL.Bool,
'paymentTransactionId' : PaymentTransactionId,
'hasBeenDistributed' : IDL.Bool,
'hasBeenDistributed' : IDL.Opt(IDL.Bool),
'totalAmount' : Satoshi,
'timestamp' : IDL.Nat64,
'paymentType' : PaymentType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ export const createActor = (canisterId, options = {}) => {
});
};

export const donation_tracker_canister = canisterId ? createActor(canisterId) : undefined;
export const donation_tracker_canister = createActor(canisterId);
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ type DonationTracker =
getTxidstext: () -> (TxidstextResult);
getUTXOS: () -> (GetUtxosResponseResult);
initRecipients: () -> (InitRecipientsResult);
isControllerLogicOk: () -> (AuthRecordResult);
listRecipients: (RecipientFilter) -> (RecipientsResult) query;
makeDonation: (DonationRecord) -> (DtiResult);
submitSignUpForm: (SignUpFormInput) -> (text);
Expand Down Expand Up @@ -190,7 +191,7 @@ type Donation =
allocation: DonationCategories;
donor: DonorType;
dti: DTI;
hasBeenDistributed: bool;
hasBeenDistributed: opt bool;
paymentTransactionId: PaymentTransactionId;
paymentType: PaymentType;
personalNote: opt text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface Donation {
'dti' : DTI,
'rewardsHaveBeenClaimed' : boolean,
'paymentTransactionId' : PaymentTransactionId,
'hasBeenDistributed' : boolean,
'hasBeenDistributed' : [] | [boolean],
'totalAmount' : Satoshi,
'timestamp' : bigint,
'paymentType' : PaymentType,
Expand Down Expand Up @@ -87,6 +87,7 @@ export interface DonationTracker {
'getTxidstext' : ActorMethod<[], TxidstextResult>,
'getUTXOS' : ActorMethod<[], GetUtxosResponseResult>,
'initRecipients' : ActorMethod<[], InitRecipientsResult>,
'isControllerLogicOk' : ActorMethod<[], AuthRecordResult>,
'listRecipients' : ActorMethod<[RecipientFilter], RecipientsResult>,
'makeDonation' : ActorMethod<[DonationRecord], DtiResult>,
'submitSignUpForm' : ActorMethod<[SignUpFormInput], string>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const idlFactory = ({ IDL }) => {
'dti' : DTI,
'rewardsHaveBeenClaimed' : IDL.Bool,
'paymentTransactionId' : PaymentTransactionId,
'hasBeenDistributed' : IDL.Bool,
'hasBeenDistributed' : IDL.Opt(IDL.Bool),
'totalAmount' : Satoshi,
'timestamp' : IDL.Nat64,
'paymentType' : PaymentType,
Expand Down Expand Up @@ -220,6 +220,7 @@ export const idlFactory = ({ IDL }) => {
'getTxidstext' : IDL.Func([], [TxidstextResult], []),
'getUTXOS' : IDL.Func([], [GetUtxosResponseResult], []),
'initRecipients' : IDL.Func([], [InitRecipientsResult], []),
'isControllerLogicOk' : IDL.Func([], [AuthRecordResult], []),
'listRecipients' : IDL.Func(
[RecipientFilter],
[RecipientsResult],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
timestamp: 0n,
dti: 0n,
rewardsHaveBeenClaimed: false,
hasBeenDistributed: false,
hasBeenDistributed: [false],
donor: {
Anonymous: null
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</script>

<div class="text-gray-900 dark:text-gray-200">
<div>
<div class="pb-6 space-y-2">
<p>Total Amount: {donation.totalAmount} {Object.keys(donation.paymentType)[0] === "BTC" ? "Satoshi" : ""}</p>
<p>Payment Type: {Object.keys(donation.paymentType)[0]}</p>
{#if Object.keys(donation.paymentType)[0] === "BTC"}
Expand Down Expand Up @@ -44,6 +44,11 @@
{#each Object.entries(donation.allocation) as [category, categoryValues], index}
<p>{categoryNameTranslator[category]}: {categoryValues}</p>
{/each}
{#if donation.personalNote[0]}
<span class="inline-block break-all">
<p>Personal Note: {donation.personalNote[0]}</p>
</span>
{/if}
</div>
<RecipientProfile recipientId={donation.recipientId} embedded={false} />
</div>
Expand Down
Loading