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/stacks] Optimistic operation disappears for long running transactions #6793

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curly-donkeys-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/live-common": patch
---

Add pending operations to stacks account object
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {

import { decodeAccountId } from "../../../../account";
import { fetchFullMempoolTxs, fetchNonce } from "../../bridge/utils/api";
import { StacksNetwork, TransactionResponse } from "./api.types";
import { MempoolTransaction, StacksNetwork, TransactionResponse } from "./api.types";
import { getCryptoCurrencyById } from "../../../../currencies";
import { encodeOperationId, encodeSubOperationId } from "../../../../operation";
import { StacksOperation } from "../../types";
Expand Down Expand Up @@ -57,6 +57,66 @@ export const getAddress = (
derivationPath: string;
} => ({ address: account.freshAddress, derivationPath: account.freshAddressPath });

const getMemo = (memoHex?: string) => {
if (memoHex?.substring(0, 2) === "0x") {
return Buffer.from(memoHex.substring(2), "hex").toString().replaceAll("\x00", "");
}

return "";
};

export const mapPendingTxToOps =
(accountID: string, address: string) =>
(tx: MempoolTransaction): StacksOperation[] => {
const { sender_address, receipt_time, fee_rate, tx_id, token_transfer, tx_status, nonce } = tx;
if (tx.tx_type !== "token_transfer" || tx_status !== "pending") {
return [];
}

const memo = getMemo(token_transfer.memo);
const feeToUse = new BigNumber(fee_rate || "0");

const date = new Date(receipt_time * 1000);

const operationCommons = {
hash: tx_id,
fee: feeToUse,
accountId: accountID,
senders: [sender_address],
recipients: [token_transfer.recipient_address],
transactionSequenceNumber: nonce,
value: new BigNumber(token_transfer.amount).plus(feeToUse),
date,
extra: {
memo,
},
blockHeight: null,
blockHash: null,
};

const isSending = address === sender_address;
const isReceiving = token_transfer.recipient_address === address;

const ops: StacksOperation[] = [];
if (isSending) {
const type: OperationType = "OUT";
ops.push({
...operationCommons,
id: encodeOperationId(accountID, tx_id, type),
type,
});
} else if (isReceiving) {
const type: OperationType = "IN";
ops.push({
...operationCommons,
id: encodeOperationId(accountID, tx_id, type),
type,
});
}

return ops;
};

export const mapTxToOps =
(accountID: string) =>
(tx: TransactionResponse): StacksOperation[] => {
Expand All @@ -76,10 +136,7 @@ export const mapTxToOps =
const recipients = allRecipients.length === 1 ? [allRecipients[0]] : [];

const memoHex = tx.tx.token_transfer?.memo;
let memo: string = "";
if (memoHex?.substring(0, 2) === "0x") {
memo = Buffer.from(memoHex.substring(2), "hex").toString().replaceAll("\x00", "");
}
const memo: string = getMemo(memoHex);

const ops: StacksOperation[] = [];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import flatMap from "lodash/flatMap";
import { mapTxToOps } from "./misc";
import { mapTxToOps, mapPendingTxToOps } from "./misc";
import { encodeAccountId } from "../../../../account";
import { StacksOperation } from "../../types";
import { TransactionResponse } from "./api.types";
import { MempoolTransaction, TransactionResponse } from "./api.types";

const sendManyTransfer = {
tx: {
Expand Down Expand Up @@ -88,6 +88,26 @@ const sendManyTransfer = {
nft_transfers: [],
};

const mempoolTransfer = {
tx_id: "0x628369d2c9b49f0ec95531fe1e6a39141573117f3dd047fca65ff5e4058fbc55",
nonce: 32,
fee_rate: "487",
sender_address: "SPNX9YY3T4GR4XDSNRVWB2MDQVCTJMP3BGT7VCZA",
sponsored: false,
post_condition_mode: "deny",
post_conditions: [],
anchor_mode: "any",
tx_status: "pending",
receipt_time: 1714554733,
receipt_time_iso: "2024-05-01T09:12:13.000Z",
tx_type: "token_transfer",
token_transfer: {
recipient_address: "SP26AZ1JSFZQ82VH5W2NJSB2QW15EW5YKT6WMD69J",
amount: "9523",
memo: "0x00000000000000000000000000000000000000000000000000000000000000000000",
},
};

const basicTransfer = {
tx: {
tx_id: "0x445e42f706602f2f10a956894a61d7f558cd6e5bc88cb4b708ea72e00419d7b0",
Expand Down Expand Up @@ -175,3 +195,29 @@ describe("operation building from raw", () => {
expect(opBasic.recipients).toHaveLength(1);
});
});

describe("operation building from mempool raw", () => {
test("map raw mempool transaction to op", async () => {
const accountId = encodeAccountId({
type: "js",
version: "2",
currencyId: "stacks",
xpubOrAddress: "",
derivationMode: "stacks_wallet",
});

const address = "SPNX9YY3T4GR4XDSNRVWB2MDQVCTJMP3BGT7VCZA";

// Contains operations for txn of type token_transfer
const operations = flatMap<MempoolTransaction, StacksOperation>(
[mempoolTransfer],
mapPendingTxToOps(accountId, address),
);

expect(operations.length).toBe(1);
expect(operations[0].type).toBe("OUT");
expect(operations[0].internalOperations).toBeUndefined();
expect(operations[0].senders).toHaveLength(1);
expect(operations[0].recipients).toHaveLength(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import BigNumber from "bignumber.js";
import { Account } from "@ledgerhq/types-live";
import { getAddressFromPublicKey } from "@stacks/transactions";
import { encodeAccountId } from "@ledgerhq/coin-framework/account/index";
import { mapTxToOps, reconciliatePublicKey } from "./bridge/utils/misc";
import { mapPendingTxToOps, mapTxToOps, reconciliatePublicKey } from "./bridge/utils/misc";
import { GetAccountShape, makeSync } from "../../bridge/jsHelpers";
import {
fetchBalances,
Expand Down Expand Up @@ -41,14 +41,14 @@ export const getAccountShape: GetAccountShape = async info => {
.minus(new BigNumber(tx.fee_rate))
.minus(new BigNumber(tx.token_transfer.amount));
}

const pendingOperations = mempoolTxs.flatMap(mapPendingTxToOps(accountId, address));
const result: Partial<Account> = {
id: accountId,
xpub: pubKey,
freshAddress: address,
balance,
spendableBalance,
operations: rawTxs.flatMap(mapTxToOps(accountId)),
operations: pendingOperations.concat(rawTxs.flatMap(mapTxToOps(accountId))),
blockHeight: blockHeight.chain_tip.block_height,
};

Expand Down