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

refactor: isReturn and total methods #106

Merged
merged 14 commits into from
Nov 6, 2024
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change ensures that isReturn will return true only when all of the recipients are the same as sender.

}

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;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as above but for the signed transactions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shahin-hq I think here we could use the this.isReturn() from the signed dto in the coins, or do you see any issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that before but couldn't because I couldn't find a way to access given wallet in coin class.

return [this.#wallet.address(), this.#wallet.publicKey()].includes(this.sender());

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();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a transaction is a returning the fee should be subtracted


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;
}
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added total calculation for multipayment signed transactions. The same as confirmed transaction:

if (this.isMultiPayment()) {

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
Loading