Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Add join() to fetchPayments to get payment parent transaction (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
quietbits authored Apr 27, 2020
1 parent 49f73c0 commit 279e380
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 62 deletions.
2 changes: 2 additions & 0 deletions playground/src/components/Payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class Payments extends Component {
{payment.isRecipient ? "From" : "To"}{" "}
{payment.otherAccount.publicKey}
</li>
<li>Memo: {payment.memo}</li>
<li>Memo type: {payment.memoType}</li>
</ul>
{/* <Json src={payment} /> */}
</li>
Expand Down
3 changes: 2 additions & 1 deletion src/data/DataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export class DataProvider {
.limit(params.limit || 10)
.order(params.order || "desc")
.cursor(params.cursor || "")
.join("transactions")
.call();

return this._processPayments(payments);
Expand Down Expand Up @@ -358,7 +359,7 @@ export class DataProvider {
return {
next: () => payments.next().then((res) => this._processPayments(res)),
prev: () => payments.prev().then((res) => this._processPayments(res)),
records: makeDisplayablePayments(
records: await makeDisplayablePayments(
{ publicKey: this.accountKey },
payments.records,
),
Expand Down
4 changes: 2 additions & 2 deletions src/data/makeDisplayablePayments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { Payments } from "../fixtures/PaymentsResponse";
import { parseResponse } from "../testUtils";
import { makeDisplayablePayments } from "./makeDisplayablePayments";

it("Makes payments", () => {
it("Makes payments", async () => {
const paymentResponse = parseResponse(Payments);
const payments = makeDisplayablePayments(
const payments = await makeDisplayablePayments(
{ publicKey: "PHYREXIA" },
paymentResponse.records,
);
Expand Down
133 changes: 75 additions & 58 deletions src/data/makeDisplayablePayments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,78 +20,95 @@ function isPathPayment(obj: any): obj is ServerApi.PathPaymentOperationRecord {
);
}

export function makeDisplayablePayments(
export async function makeDisplayablePayments(
subjectAccount: Account,
payments: Array<
| ServerApi.CreateAccountOperationRecord
| ServerApi.PaymentOperationRecord
| ServerApi.PathPaymentOperationRecord
>,
): Payment[] {
return payments.map(
(
payment:
| ServerApi.CreateAccountOperationRecord
| ServerApi.PaymentOperationRecord
| ServerApi.PathPaymentOperationRecord,
): Payment => {
const isRecipient = payment.source_account !== subjectAccount.publicKey;
): Promise<Payment[]> {
return Promise.all(
payments.map(
async (
payment:
| ServerApi.CreateAccountOperationRecord
| ServerApi.PaymentOperationRecord
| ServerApi.PathPaymentOperationRecord,
): Promise<Payment> => {
const isRecipient = payment.source_account !== subjectAccount.publicKey;

let otherAccount: Account;
let otherAccount: Account;

if (isCreateAccount(payment)) {
otherAccount = {
publicKey: isRecipient
? payment.source_account
: payment.source_account,
};
} else {
otherAccount = { publicKey: isRecipient ? payment.from : payment.to };
}

const token: Token = isCreateAccount(payment)
? {
type: "native" as AssetType,
code: "XLM",
}
: {
type: payment.asset_type as AssetType,
code: payment.asset_code || "XLM",
issuer:
payment.asset_type === "native"
? undefined
: {
key: payment.asset_issuer as string,
},
if (isCreateAccount(payment)) {
otherAccount = {
publicKey: isRecipient
? payment.source_account
: payment.source_account,
};
} else {
otherAccount = { publicKey: isRecipient ? payment.from : payment.to };
}

return {
id: payment.id,
isInitialFunding: isCreateAccount(payment),
isRecipient,
token,
amount: new BigNumber(
isCreateAccount(payment) ? payment.starting_balance : payment.amount,
),
timestamp: Math.floor(new Date(payment.created_at).getTime() / 1000),
otherAccount,
sourceToken: isPathPayment(payment)
const token: Token = isCreateAccount(payment)
? {
type: payment.source_asset_type as AssetType,
code: payment.source_asset_code || "XLM",
type: "native" as AssetType,
code: "XLM",
}
: {
type: payment.asset_type as AssetType,
code: payment.asset_code || "XLM",
issuer:
payment.source_asset_type === "native"
payment.asset_type === "native"
? undefined
: {
key: payment.source_asset_issuer as string,
key: payment.asset_issuer as string,
},
}
: undefined,
sourceAmount: isPathPayment(payment)
? new BigNumber(payment.source_amount)
: undefined,
transactionId: payment.transaction_hash,
};
},
};

let transaction: ServerApi.TransactionRecord | undefined;
try {
transaction = await payment.transaction();
} catch (e) {
// do nothing
}

return {
id: payment.id,
isInitialFunding: isCreateAccount(payment),
isRecipient,
token,
amount: new BigNumber(
isCreateAccount(payment)
? payment.starting_balance
: payment.amount,
),
timestamp: Math.floor(new Date(payment.created_at).getTime() / 1000),
otherAccount,
sourceToken: isPathPayment(payment)
? {
type: payment.source_asset_type as AssetType,
code: payment.source_asset_code || "XLM",
issuer:
payment.source_asset_type === "native"
? undefined
: {
key: payment.source_asset_issuer as string,
},
}
: undefined,
sourceAmount: isPathPayment(payment)
? new BigNumber(payment.source_amount)
: undefined,
transactionId: payment.transaction_hash,
...(transaction
? {
memo: transaction.memo,
memoType: transaction.memo_type,
}
: {}),
};
},
),
);
}
5 changes: 4 additions & 1 deletion src/types/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BigNumber from "bignumber.js";
import { AssetType } from "stellar-base";
import { AssetType, Memo, MemoType } from "stellar-base";
import {
BadRequestError,
Horizon,
Expand Down Expand Up @@ -127,6 +127,9 @@ export interface Payment {
sourceAmount?: BigNumber;

transactionId: string;

memo?: Memo | string;
memoType?: MemoType;
}

export interface Balance {
Expand Down

0 comments on commit 279e380

Please sign in to comment.