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

[Alpaca] Add block hash and timestamp #8723

Merged
merged 8 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
8 changes: 8 additions & 0 deletions .changeset/quick-frogs-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@ledgerhq/coin-stellar": minor
"@ledgerhq/coin-tezos": minor
"@ledgerhq/coin-xrp": minor
"@ledgerhq/coin-framework": minor
---

Add block informations to an operation (hash, time and height of a block)
1 change: 1 addition & 0 deletions libs/coin-framework/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type Operation = {
value: bigint;
fee: bigint;
blockHeight: number;
block?: BlockInfo;
Salim-belkhir marked this conversation as resolved.
Show resolved Hide resolved
senders: string[];
recipients: string[];
date: Date;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export async function buildOptimisticOperation(
transactionSequenceNumber: transactionSequenceNumber?.plus(1).toNumber(),
extra: {
ledgerOpType: type,
blockTime: new Date(),
Salim-belkhir marked this conversation as resolved.
Show resolved Hide resolved
},
};

Expand Down
10 changes: 10 additions & 0 deletions libs/coin-modules/coin-stellar/src/logic/listOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export type Operation = {
value: bigint;
fee: bigint;
blockHeight: number;
block?: {
hash: string;
time: Date;
height: number;
};
senders: string[];
recipients: string[];
date: Date;
Expand Down Expand Up @@ -42,6 +47,11 @@ const convertToCoreOperation = (address: string) => (operation: StellarOperation
value: BigInt(operation.value.toString()),
fee: BigInt(operation.fee.toString()),
blockHeight: operation.blockHeight!,
block: {
hash: operation.blockHash!,
time: operation.extra.blockTime,
height: operation.blockHeight!,
},
senders: operation.senders,
recipients: operation.recipients,
date: operation.date,
Expand Down
16 changes: 13 additions & 3 deletions libs/coin-modules/coin-stellar/src/network/horizon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,24 @@ Horizon.AxiosClient.interceptors.response.use(response => {
// FIXME: workaround for the Stellar SDK not using the correct URL: the "next" URL
// included in server responses points to the node itself instead of our reverse proxy...
// (https://github.com/stellar/js-stellar-sdk/issues/637)

function fixURL(url: string): string {
Salim-belkhir marked this conversation as resolved.
Show resolved Hide resolved
const u = new URL(url);
u.host = new URL(coinConfig.getCoinConfig().explorer.url).host;
return u.toString();
}

const next_href = response?.data?._links?.next?.href;

if (next_href) {
const next = new URL(next_href);
next.host = new URL(coinConfig.getCoinConfig().explorer.url).host;
response.data._links.next.href = next.toString();
response.data._links.next.href = fixURL(next_href);
}

response?.data?._embedded?.records?.forEach((r: any) => {
const href = r.transaction?._links?.ledger?.href;
if (href) r.transaction._links.ledger.href = fixURL(href);
});

return response;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ async function formatOperation(
addr: string,
): Promise<StellarOperation> {
const transaction = await rawOperation.transaction();
const { hash: blockHash, closed_at: blockTime } = await transaction.ledger();
const type = getOperationType(rawOperation, addr);
const value = getValue(rawOperation, transaction, type);
const recipients = getRecipients(rawOperation);
Expand All @@ -103,9 +104,10 @@ async function formatOperation(
recipients,
transactionSequenceNumber: Number(transaction.source_account_sequence),
hasFailed: !rawOperation.transaction_successful,
blockHash: null,
blockHash: blockHash,
extra: {
ledgerOpType: type,
blockTime: new Date(blockTime),
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function createFixtureOperation(operation?: Partial<StellarOperation>): S
let extra: StellarOperationExtra = {
assetAmount: operation?.extra?.assetAmount || undefined,
ledgerOpType: operation?.extra?.ledgerOpType || "IN",
blockTime: operation?.extra?.blockTime || faker.date.past(),
};
if (operation?.extra?.pagingToken) {
extra = {
Expand Down
1 change: 1 addition & 0 deletions libs/coin-modules/coin-stellar/src/types/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export type StellarOperationExtra = {
assetAmount?: string | undefined;
ledgerOpType: OperationType;
memo?: string;
blockTime: Date;
};

export type StellarAccount = Account;
1 change: 1 addition & 0 deletions libs/coin-modules/coin-tezos/src/api/index.integ.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe("Tezos Api", () => {
const isSenderOrReceipt =
operation.senders.includes(address) || operation.recipients.includes(address);
expect(isSenderOrReceipt).toBeTruthy();
expect(operation.block).toBeDefined();
});
});

Expand Down
25 changes: 20 additions & 5 deletions libs/coin-modules/coin-tezos/src/logic/listOperations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { tzkt } from "../network";
import {
APIBlock,
type APIDelegationType,
type APITransactionType,
isAPIDelegationType,
Expand All @@ -13,6 +14,11 @@ export type Operation = {
value: bigint;
fee: bigint;
blockHeight: number;
block?: {
hash: string;
height: number;
time: Date;
};
senders: string[];
recipients: string[];
date: Date;
Expand All @@ -24,17 +30,21 @@ export async function listOperations(
{ lastId, limit }: { lastId?: number; limit?: number },
): Promise<[Operation[], number]> {
const operations = await tzkt.getAccountOperations(address, { lastId, limit });
return [
const operationswithBlock = await Promise.all(
operations
.filter(op => isAPITransactionType(op) || isAPIDelegationType(op))
.reduce((acc, op) => acc.concat(convertOperation(address, op)), [] as Operation[]),
operations.slice(-1)[0].id,
];
.map(async op => {
const block = await tzkt.getBlockByHash(op.block);
Salim-belkhir marked this conversation as resolved.
Show resolved Hide resolved
return convertOperation(address, op, block);
}),
);
return [operationswithBlock, operations.slice(-1)[0].id];
}

function convertOperation(
address: string,
operation: APITransactionType | APIDelegationType,
block: APIBlock,
): Operation {
const { amount, hash, storageFee, sender, timestamp, type, counter } = operation;
let targetAddress = "";
Expand All @@ -48,7 +58,12 @@ function convertOperation(
value: BigInt(amount),
// storageFee for transaction is always present
fee: BigInt(storageFee ?? 0),
blockHeight: 0, // operation.block is a string
blockHeight: block.level,
block: {
hash: block.hash,
Salim-belkhir marked this conversation as resolved.
Show resolved Hide resolved
height: block.level,
time: new Date(block.timestamp),
},
Salim-belkhir marked this conversation as resolved.
Show resolved Hide resolved
senders: [sender?.address ?? ""],
recipients: [targetAddress],
date: new Date(timestamp),
Expand Down
6 changes: 6 additions & 0 deletions libs/coin-modules/coin-tezos/src/network/tzkt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ const api = {
date: new Date(data[0].timestamp),
};
},
async getBlockByHash(hash: string): Promise<APIBlock> {
const { data } = await network<APIBlock>({
url: `${getExplorerUrl()}/v1/blocks/${hash}`,
});
return data;
},
async getAccountByAddress(address: string): Promise<APIAccount> {
const { data } = await network<APIAccount>({
url: `${getExplorerUrl()}/v1/accounts/${address}`,
Expand Down
39 changes: 30 additions & 9 deletions libs/coin-modules/coin-xrp/src/api/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,29 @@ describe("listOperations", () => {
const fee = 10;
mockGetTransactions.mockResolvedValue([
{
ledger_hash: "HASH_VALUE_BLOCK",
hash: "HASH_VALUE",
close_time_iso: "2000-01-01T00:00:01Z",
meta: { delivered_amount: deliveredAmount.toString() },
tx: {
tx_json: {
TransactionType: "Payment",
Fee: fee.toString(),
hash: "HASH_VALUE",
inLedger: 1,
ledger_index: 1,
date: 1000,
Account: opSender,
Destination: opDestination,
Sequence: 1,
},
},
{
ledger_hash: "HASH_VALUE_BLOCK",
hash: "HASH_VALUE",
close_time_iso: "2000-01-01T00:00:01Z",
meta: { delivered_amount: deliveredAmount.toString() },
tx: {
tx_json: {
TransactionType: "Payment",
Fee: fee.toString(),
hash: "HASH_VALUE",
inLedger: 1,
ledger_index: 1,
date: 1000,
Account: opSender,
Destination: opDestination,
Expand All @@ -68,12 +72,14 @@ describe("listOperations", () => {
},
},
{
ledger_hash: "HASH_VALUE_BLOCK",
hash: "HASH_VALUE",
close_time_iso: "2000-01-01T00:00:01Z",
meta: { delivered_amount: deliveredAmount.toString() },
tx: {
tx_json: {
TransactionType: "Payment",
Fee: fee.toString(),
hash: "HASH_VALUE",
inLedger: 1,
ledger_index: 1,
date: 1000,
Account: opSender,
Destination: opDestination,
Expand Down Expand Up @@ -107,6 +113,11 @@ describe("listOperations", () => {
value: expectedValue,
fee: BigInt(10),
blockHeight: 1,
block: {
hash: "HASH_VALUE_BLOCK",
height: 1,
time: new Date("2000-01-01T00:00:01Z"),
},
senders: [opSender],
recipients: [opDestination],
date: new Date(1000000 + RIPPLE_EPOCH * 1000),
Expand All @@ -119,6 +130,11 @@ describe("listOperations", () => {
value: expectedValue,
fee: BigInt(10),
blockHeight: 1,
block: {
hash: "HASH_VALUE_BLOCK",
height: 1,
time: new Date("2000-01-01T00:00:01Z"),
},
senders: [opSender],
recipients: [opDestination],
date: new Date(1000000 + RIPPLE_EPOCH * 1000),
Expand All @@ -134,6 +150,11 @@ describe("listOperations", () => {
value: expectedValue,
fee: BigInt(10),
blockHeight: 1,
block: {
hash: "HASH_VALUE_BLOCK",
height: 1,
time: new Date("2000-01-01T00:00:01Z"),
},
senders: [opSender],
recipients: [opDestination],
date: new Date(1000000 + RIPPLE_EPOCH * 1000),
Expand Down
13 changes: 10 additions & 3 deletions libs/coin-modules/coin-xrp/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,18 @@ async function operations(
address: string,
{ limit, start }: Pagination,
): Promise<[Operation[], number]> {
const [ops, index] = await listOperations(address, { limit, mostRecentIndex: start });
const [ops, index] = await listOperations(address, { limit, startAt: start ?? 0 });
return [
ops.map(op => {
const { simpleType, ...rest } = op;
return { ...rest } satisfies Operation;
const { simpleType, blockHash, blockTime, ...rest } = op;
return {
...rest,
block: {
height: rest.blockHeight,
hash: blockHash,
time: blockTime,
},
} satisfies Operation;
}),
index,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ describe("getAccountShape", () => {
});
mockGetTransactions.mockResolvedValue([
{
ledger_hash: "HASH_VALUE_BLOCK",
hash: "HASH_VALUE",
meta: { delivered_amount: "100" },
tx: {
tx_json: {
TransactionType: "Payment",
Fee: "10",
hash: "HASH_VALUE",
inLedger: 1,
ledger_index: 1,
date: 1000,
Account: "account_addr",
Destination: "destination_addr",
Expand Down Expand Up @@ -117,7 +118,7 @@ describe("getAccountShape", () => {
operations: [
{
accountId: "js:2:ripple:address:",
blockHash: null,
blockHash: "HASH_VALUE_BLOCK",
blockHeight: 1,
date: new Date("2000-01-01T00:16:40.000Z"),
hash: "HASH_VALUE",
Expand Down
2 changes: 1 addition & 1 deletion libs/coin-modules/coin-xrp/src/bridge/synchronization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async function filterOperations(
type: op.simpleType,
value: new BigNumber(op.value.toString()),
fee: new BigNumber(op.fee.toString()),
blockHash: null,
blockHash: op.blockHash,
blockHeight: op.blockHeight,
senders: op.senders,
recipients: op.recipients,
Expand Down
Loading
Loading