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

Support/fix casper history #6413

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/large-needles-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/live-common": patch
---

Fix sometimes missing ops for accounts on casper
8 changes: 5 additions & 3 deletions libs/ledger-live-common/src/families/casper/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ export interface ITxnHistoryData {
};
target: {
parsed: string;
cl_type: {
ByteArray: number;
};
cl_type:
| {
ByteArray: number;
}
| string;
};
};
amount: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { casperAccountHashFromPublicKey, casperGetCLPublicKey, isAddressValid }
import { ITxnHistoryData } from "../../api/types";
import { getEstimatedFees } from "./fee";
import { CasperOperation } from "../../types";
import invariant from "invariant";

export const getUnit = (): Unit => getCryptoCurrencyById("casper").units[0];

Expand All @@ -19,59 +20,72 @@ export function mapTxToOps(
fees = getEstimatedFees(),
): (tx: ITxnHistoryData) => CasperOperation[] {
return (tx: ITxnHistoryData): CasperOperation[] => {
const ops: CasperOperation[] = [];
const { timestamp, caller_public_key, args: txArgs, deploy_hash, error_message } = tx;
const fromAccount = casperAccountHashFromPublicKey(caller_public_key);
const toAccount = txArgs.target.parsed;
try {
const ops: CasperOperation[] = [];
const { timestamp, caller_public_key, args: txArgs, deploy_hash, error_message } = tx;
const fromAccount = casperAccountHashFromPublicKey(caller_public_key);
let toAccount;

const date = new Date(timestamp);
const value = new BigNumber(txArgs.amount.parsed);
const feeToUse = fees;
if (txArgs.target.cl_type === "PublicKey") {
toAccount = casperAccountHashFromPublicKey(txArgs.target.parsed);
} else {
toAccount = txArgs.target.parsed;
}
invariant(toAccount, "toAccount is required");
invariant(fromAccount, "fromAccount is required");

const isSending = addressHash.toLowerCase() === fromAccount.toLowerCase();
const isReceiving = addressHash.toLowerCase() === toAccount.toLowerCase();
const date = new Date(timestamp);
const value = new BigNumber(txArgs.amount.parsed);
const feeToUse = fees;

if (isSending) {
ops.push({
id: encodeOperationId(accountId, deploy_hash, "OUT"),
hash: deploy_hash,
type: "OUT",
value: value.plus(feeToUse),
fee: feeToUse,
blockHeight: 1,
hasFailed: error_message ? true : false,
blockHash: null,
accountId,
senders: [fromAccount],
recipients: [toAccount],
date,
extra: {
transferId: txArgs.id.parsed?.toString(),
},
});
}
const isSending = addressHash.toLowerCase() === fromAccount.toLowerCase();
const isReceiving = addressHash.toLowerCase() === toAccount.toLowerCase();

if (isReceiving) {
ops.push({
id: encodeOperationId(accountId, deploy_hash, "IN"),
hash: deploy_hash,
type: "IN",
value,
fee: feeToUse,
blockHeight: 1,
blockHash: null,
hasFailed: error_message ? true : false,
accountId,
senders: [fromAccount],
recipients: [toAccount],
date,
extra: {
transferId: txArgs.id.parsed?.toString(),
},
});
}
if (isSending) {
ops.push({
id: encodeOperationId(accountId, deploy_hash, "OUT"),
hash: deploy_hash,
type: "OUT",
value: value.plus(feeToUse),
fee: feeToUse,
blockHeight: 1,
hasFailed: error_message ? true : false,
blockHash: null,
accountId,
senders: [fromAccount],
recipients: [toAccount],
date,
extra: {
transferId: txArgs.id.parsed?.toString(),
},
});
}

return ops;
if (isReceiving) {
ops.push({
id: encodeOperationId(accountId, deploy_hash, "IN"),
hash: deploy_hash,
type: "IN",
value,
fee: feeToUse,
blockHeight: 1,
blockHash: null,
hasFailed: error_message ? true : false,
accountId,
senders: [fromAccount],
recipients: [toAccount],
date,
extra: {
transferId: txArgs.id.parsed?.toString(),
},
});
}

return ops;
} catch (err) {
log("warn", `mapTxToOps failed for casper, skipping operation`, err);
return [];
}
};
}

Expand Down
Loading