Skip to content

Commit

Permalink
refactor: isReturn and total methods (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahin-hq authored Nov 6, 2024
1 parent 431158d commit c051445
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
11 changes: 10 additions & 1 deletion packages/ark/source/confirmed-transaction.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ export class ConfirmedTransactionData extends DTO.AbstractConfirmedTransactionDa
}

if (this.isMultiPayment()) {
return this.recipients().some(({ address }: Contracts.MultiPaymentRecipient) => address === this.sender());
let isReturn = true;

for (const recipient of this.recipients().values()) {
if (recipient.address !== this.sender()) {
isReturn = false;
break;
}
}

return isReturn;
}

return false;
Expand Down
35 changes: 33 additions & 2 deletions packages/profiles/source/signed-transaction.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,24 @@ export class ExtendedSignedTransactionData {
}

public isReturn(): boolean {
return this.isSent() && this.isReceived();
if (this.isTransfer()) {
return this.isSent() && this.isReceived();
}

if (this.isMultiPayment()) {
let isReturn = true;

for (const recipient of this.recipients().values()) {
if (recipient.address !== this.sender()) {
isReturn = false;
break;
}
}

return isReturn;
}

return false;
}

public isSent(): boolean {
Expand Down Expand Up @@ -131,11 +148,25 @@ export class ExtendedSignedTransactionData {
}

public total(): number {
if (this.isReturn()) {
return this.amount() - this.fee();
}

if (this.isSent()) {
return this.amount() + this.fee();
}

return this.amount();
let total = this.amount();

if (this.isMultiPayment()) {
for (const recipient of this.recipients()) {
if (recipient.address !== this.wallet().address()) {
total -= recipient.amount;
}
}
}

return total;
}

public convertedTotal(): number {
Expand Down
3 changes: 3 additions & 0 deletions packages/profiles/source/transaction.dto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const createSubject = (wallet, properties, klass) => {
getMeta: () => meta,
id: () => "transactionId",
inputs: () => [],
isReturn: () => false,
isSent: () => true,
memo: () => "memo",
outputs: () => [],
Expand Down Expand Up @@ -313,6 +314,7 @@ describe("ExtendedConfirmedTransactionData", ({ beforeEach, it, assert, stub, sp
amount: () => BigNumber.make(18e8, 8),
fee: () => BigNumber.make(2e8, 8),
isMultiPayment: () => false,
isReturn: () => false,
isSent: () => false,
});

Expand All @@ -324,6 +326,7 @@ describe("ExtendedConfirmedTransactionData", ({ beforeEach, it, assert, stub, sp
amount: () => BigNumber.make(18e8, 8),
fee: () => BigNumber.make(2e8, 8),
isMultiPayment: () => true,
isReturn: () => false,
isSent: () => false,
recipients: () => [
{
Expand Down
4 changes: 4 additions & 0 deletions packages/profiles/source/transaction.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ export class ExtendedConfirmedTransactionData implements Contracts.ConfirmedTran
*/

public total(): number {
if (this.isReturn()) {
return this.amount() - this.fee();
}

if (this.isSent()) {
return this.amount() + this.fee();
}
Expand Down

0 comments on commit c051445

Please sign in to comment.