From 07480af29d9a2f1eb96c4435b28bcc667b3b8309 Mon Sep 17 00:00:00 2001 From: wa-aal <93649819+wa-aal@users.noreply.github.com> Date: Fri, 25 Feb 2022 14:33:58 +0100 Subject: [PATCH] Cosmos JS (#1743) * increase gas amplifier * fix payload construction handle payload atomic construction * More accurate gas amplifier * increase gas amplifier * use same node for calculation and broadcast * fix amount payload * fix fees/gas calculation * fix signature fix public key when account is derivate * fix fees regression * More accurate pubkey selection * don't use extra.tx_bytes * fix pubkey selection * simplify hex serialization * update transaction: more strict types * many things restruct operation builder simulate now return int prepareTransaction use patch format * accuracy more accuracy int value small refactor * remove useless isPreValidation * fix strange edge effect of ledger live desktop * Update xpub during sync * temporary enable log for bot * LL-9159 cosmos node * Update js-signOperation.ts revert back test trace for bot * fix signature * fix redelegate payload * fix payload send transaction when sendmax * fix optimistic operation type * fix typo * bugfix * fix regression * update optimistic operation fix regression add operation type more consistent fee * fix prettier * more deterministic transaction parsing * clarify code readable * adjust sender and recipient * fix fees when is ibc transaction * fix redelegations data mapping * fix mixed styles * fix array cast type * fix more determinist operation data * fix could not find optimisticOperation in redelegate transaction * restore getStargateRewardsState methode * amount of the operation more close * return transaction in prepareTransaction * keep immutable paradigm for prepareTransaction Co-authored-by: Alexandre Alouit --- src/families/cosmos/js-prepareTransaction.ts | 27 ++++++++++++++------ src/families/cosmos/js-signOperation.ts | 10 +++++--- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/families/cosmos/js-prepareTransaction.ts b/src/families/cosmos/js-prepareTransaction.ts index 0bc88f1eb7..06e63cc840 100644 --- a/src/families/cosmos/js-prepareTransaction.ts +++ b/src/families/cosmos/js-prepareTransaction.ts @@ -24,13 +24,16 @@ const prepareTransaction = async ( account: Account, transaction: Transaction ): Promise => { - const patch: Partial = {}; + let memo = transaction.memo; + let fees = transaction.fees; + let gas = transaction.gas; + let amount = transaction.amount; let gasQty = new BigNumber(250000); const gasPrice = new BigNumber(getEnv("COSMOS_GAS_PRICE")); if (transaction.useAllAmount) { - patch.amount = getMaxEstimatedBalance( + amount = getMaxEstimatedBalance( account, account.balance .dividedBy(new BigNumber(getEnv("COSMOS_GAS_AMPLIFIER"))) @@ -39,12 +42,12 @@ const prepareTransaction = async ( } if (transaction.mode !== "send" && !transaction.memo) { - patch.memo = "Ledger Live"; + memo = "Ledger Live"; } const unsignedPayload = await buildTransaction(account, { ...transaction, - ...patch, + amount, }); // be sure payload is complete @@ -53,7 +56,7 @@ const prepareTransaction = async ( typeUrl: "/cosmos.tx.v1beta1.TxBody", value: { messages: unsignedPayload, - memo: transaction.memo || patch.memo || "", + memo: transaction.memo || memo || "", }, }; @@ -111,11 +114,19 @@ const prepareTransaction = async ( } } - patch.gas = gasQty; + gas = gasQty; - patch.fees = gasPrice.multipliedBy(gasQty).integerValue(); + fees = gasPrice.multipliedBy(gasQty).integerValue(); - return { ...transaction, ...patch }; + if ( + transaction.memo !== memo || + transaction.fees !== fees || + transaction.gas !== gas + ) { + return { ...transaction, memo, fees, gas }; + } + + return transaction; }; export default prepareTransaction; diff --git a/src/families/cosmos/js-signOperation.ts b/src/families/cosmos/js-signOperation.ts index df442f667b..cd19b1c01f 100644 --- a/src/families/cosmos/js-signOperation.ts +++ b/src/families/cosmos/js-signOperation.ts @@ -45,7 +45,7 @@ const signOperation = ({ let cancelled; async function main() { - const { accountNumber, sequence } = await getAccount( + const { accountNumber, sequence, spendableBalance } = await getAccount( account.freshAddress ); @@ -152,6 +152,8 @@ const signOperation = ({ const hash = ""; // resolved at broadcast time const accountId = account.id; + const fee = transaction.fees || new BigNumber(0); + const type: OperationType = transaction.mode === "undelegate" ? "UNDELEGATE" @@ -176,8 +178,10 @@ const signOperation = ({ id: encodeOperationId(accountId, hash, type), hash, type, - value: transaction.amount, - fee: transaction.fees || new BigNumber(0), + value: transaction.useAllAmount + ? spendableBalance + : transaction.amount.plus(fee), + fee, extra: {}, blockHash: null, blockHeight: null,