diff --git a/.changeset/silent-poets-check.md b/.changeset/silent-poets-check.md new file mode 100644 index 000000000000..973f9e5bef0d --- /dev/null +++ b/.changeset/silent-poets-check.md @@ -0,0 +1,5 @@ +--- +"@ledgerhq/live-common": minor +--- + +fix(wallet-api): add missing cosmos walletApiAdapter diff --git a/libs/ledger-live-common/src/families/cosmos/datasets/__snapshots__/cosmos.integration.test.ts.snap b/libs/ledger-live-common/src/families/cosmos/datasets/__snapshots__/cosmos.integration.test.ts.snap index 4652b8b8d970..f5e1353643a1 100644 --- a/libs/ledger-live-common/src/families/cosmos/datasets/__snapshots__/cosmos.integration.test.ts.snap +++ b/libs/ledger-live-common/src/families/cosmos/datasets/__snapshots__/cosmos.integration.test.ts.snap @@ -3,7 +3,7 @@ exports[`cosmos currency bridge scanAccounts cosmos seed 1 1`] = ` [ { - "balance": "1612686", + "balance": "5739320", "currencyId": "cosmos", "derivationMode": "", "freshAddress": "cosmos1g84934jpu3v5de5yqukkkhxmcvsw3u2ajxvpdl", @@ -12,7 +12,7 @@ exports[`cosmos currency bridge scanAccounts cosmos seed 1 1`] = ` "index": 0, "pendingOperations": [], "seedIdentifier": "0388459b2653519948b12492f1a0b464720110c147a8155d23d423a5cc3c21d89a", - "spendableBalance": "315704", + "spendableBalance": "4442338", "swapHistory": [], "syncHash": undefined, "used": true, @@ -258,6 +258,25 @@ exports[`cosmos currency bridge scanAccounts cosmos seed 1 2`] = ` "type": "IN", "value": "1000", }, + { + "accountId": "js:2:cosmos:cosmos1g84934jpu3v5de5yqukkkhxmcvsw3u2ajxvpdl:", + "blockHash": null, + "blockHeight": 24046706, + "extra": {}, + "fee": "1729", + "hasFailed": false, + "hash": "146D033842F4637F5FA2F13AC4D7888EEA8935F1D5822B955562ED685E7E8A78", + "id": "js:2:cosmos:cosmos1g84934jpu3v5de5yqukkkhxmcvsw3u2ajxvpdl:-146D033842F4637F5FA2F13AC4D7888EEA8935F1D5822B955562ED685E7E8A78-IN", + "recipients": [ + "cosmos1g84934jpu3v5de5yqukkkhxmcvsw3u2ajxvpdl", + ], + "senders": [ + "cosmos125me68y8fx88wyqwa4mp4q2cmglrnetsa2kr68", + ], + "transactionSequenceNumber": 22364, + "type": "IN", + "value": "4639888", + }, { "accountId": "js:2:cosmos:cosmos1g84934jpu3v5de5yqukkkhxmcvsw3u2ajxvpdl:", "blockHash": null, @@ -1140,6 +1159,29 @@ exports[`cosmos currency bridge scanAccounts cosmos seed 1 2`] = ` "type": "DELEGATE", "value": "10650", }, + { + "accountId": "js:2:cosmos:cosmos1g84934jpu3v5de5yqukkkhxmcvsw3u2ajxvpdl:", + "blockHash": null, + "blockHeight": 23976580, + "extra": { + "memo": "LedgerLiveBot", + "validators": [ + { + "address": "cosmosvaloper1clpqr4nrk4khgkxj78fcwwh6dl3uw4epsluffn", + "amount": "5210", + }, + ], + }, + "fee": "17390", + "hasFailed": false, + "hash": "7706B2098FBB961FED169FE177276EFE1C85C7FCA216B61A9F9FD54DEDD6B81A", + "id": "js:2:cosmos:cosmos1g84934jpu3v5de5yqukkkhxmcvsw3u2ajxvpdl:-7706B2098FBB961FED169FE177276EFE1C85C7FCA216B61A9F9FD54DEDD6B81A-REWARD", + "recipients": [], + "senders": [], + "transactionSequenceNumber": 157, + "type": "REWARD", + "value": "5210", + }, { "accountId": "js:2:cosmos:cosmos1g84934jpu3v5de5yqukkkhxmcvsw3u2ajxvpdl:", "blockHash": null, @@ -2181,6 +2223,25 @@ exports[`cosmos currency bridge scanAccounts cosmos seed 1 2`] = ` "type": "DELEGATE", "value": "11254", }, + { + "accountId": "js:2:cosmos:cosmos1g84934jpu3v5de5yqukkkhxmcvsw3u2ajxvpdl:", + "blockHash": null, + "blockHeight": 24063617, + "extra": {}, + "fee": "1074", + "hasFailed": false, + "hash": "CEF4B772F7E12B048A8903146DDC06FB769813B0A6AB475EC455CE6BBAF9BAEB", + "id": "js:2:cosmos:cosmos1g84934jpu3v5de5yqukkkhxmcvsw3u2ajxvpdl:-CEF4B772F7E12B048A8903146DDC06FB769813B0A6AB475EC455CE6BBAF9BAEB-OUT", + "recipients": [ + "osmo10a3k4hvk37cc4hnxctw4p95fhscd2z6h2rmx0aukc6rm8u9qqx9smfsh7u", + ], + "senders": [ + "cosmos1g84934jpu3v5de5yqukkkhxmcvsw3u2ajxvpdl", + ], + "transactionSequenceNumber": 158, + "type": "OUT", + "value": "501074", + }, { "accountId": "js:2:cosmos:cosmos1g84934jpu3v5de5yqukkkhxmcvsw3u2ajxvpdl:", "blockHash": null, diff --git a/libs/ledger-live-common/src/families/cosmos/walletApiAdapter.test.ts b/libs/ledger-live-common/src/families/cosmos/walletApiAdapter.test.ts new file mode 100644 index 000000000000..d114e29d4d3a --- /dev/null +++ b/libs/ledger-live-common/src/families/cosmos/walletApiAdapter.test.ts @@ -0,0 +1,71 @@ +import { Account } from "@ledgerhq/types-live"; +import { CosmosTransaction as WalletAPICosmosTransaction } from "@ledgerhq/wallet-api-core"; +import BigNumber from "bignumber.js"; +import { Transaction } from "@ledgerhq/coin-cosmos/types/index"; +import cosmos from "./walletApiAdapter"; + +describe("getWalletAPITransactionSignFlowInfos", () => { + describe("should properly get infos for Cosmos platform tx", () => { + it("without fees provided", () => { + const cosmosPlatformTx: WalletAPICosmosTransaction = { + family: "cosmos", + amount: new BigNumber(100000), + recipient: "0xABCDEF", + mode: "send", + }; + + const expectedLiveTx: Partial = { + ...cosmosPlatformTx, + fees: null, + gas: null, + useAllAmount: false, + networkInfo: null, + memo: null, + sourceValidator: null, + validators: [], + }; + + const { canEditFees, hasFeesProvided, liveTx } = cosmos.getWalletAPITransactionSignFlowInfos({ + walletApiTransaction: cosmosPlatformTx, + account: {} as Account, + }); + + expect(canEditFees).toBe(true); + + expect(hasFeesProvided).toBe(false); + + expect(liveTx).toEqual(expectedLiveTx); + }); + + it("with fees provided", () => { + const cosmosPlatformTx: WalletAPICosmosTransaction = { + family: "cosmos", + amount: new BigNumber(100000), + recipient: "0xABCDEF", + fees: new BigNumber(300), + mode: "send", + }; + + const expectedLiveTx: Partial = { + ...cosmosPlatformTx, + gas: null, + useAllAmount: false, + networkInfo: null, + memo: null, + sourceValidator: null, + validators: [], + }; + + const { canEditFees, hasFeesProvided, liveTx } = cosmos.getWalletAPITransactionSignFlowInfos({ + walletApiTransaction: cosmosPlatformTx, + account: {} as Account, + }); + + expect(canEditFees).toBe(true); + + expect(hasFeesProvided).toBe(true); + + expect(liveTx).toEqual(expectedLiveTx); + }); + }); +}); diff --git a/libs/ledger-live-common/src/families/cosmos/walletApiAdapter.ts b/libs/ledger-live-common/src/families/cosmos/walletApiAdapter.ts new file mode 100644 index 000000000000..b47771c9ac82 --- /dev/null +++ b/libs/ledger-live-common/src/families/cosmos/walletApiAdapter.ts @@ -0,0 +1,66 @@ +import createTransaction from "@ledgerhq/coin-cosmos/createTransaction"; +import { CosmosTransaction as WalletAPICosmosTransaction } from "@ledgerhq/wallet-api-core"; +import { + AreFeesProvided, + ConvertToLiveTransaction, + GetWalletAPITransactionSignFlowInfos, +} from "../../wallet-api/types"; +import { Transaction } from "@ledgerhq/coin-cosmos/types/index"; + +const CAN_EDIT_FEES = true; + +const areFeesProvided: AreFeesProvided = tx => !!tx.fees; + +const convertToLiveTransaction: ConvertToLiveTransaction< + WalletAPICosmosTransaction, + Transaction +> = ({ account, walletApiTransaction }) => { + const liveTx: Transaction = createTransaction(account); + + if (walletApiTransaction.amount) { + liveTx.amount = walletApiTransaction.amount; + } + + if (walletApiTransaction.recipient) { + liveTx.recipient = walletApiTransaction.recipient; + } + + if (walletApiTransaction.mode) { + liveTx.mode = walletApiTransaction.mode; + } + + if (walletApiTransaction.fees) { + liveTx.fees = walletApiTransaction.fees; + } + + if (walletApiTransaction.gas) { + liveTx.gas = walletApiTransaction.gas; + } + + if (walletApiTransaction.memo) { + liveTx.memo = walletApiTransaction.memo; + } + + if (walletApiTransaction.sourceValidator) { + liveTx.sourceValidator = walletApiTransaction.sourceValidator; + } + + if (walletApiTransaction.validators) { + liveTx.validators = walletApiTransaction.validators; + } + + return liveTx; +}; + +const getWalletAPITransactionSignFlowInfos: GetWalletAPITransactionSignFlowInfos< + WalletAPICosmosTransaction, + Transaction +> = ({ walletApiTransaction, account }) => { + return { + canEditFees: CAN_EDIT_FEES, + liveTx: convertToLiveTransaction({ walletApiTransaction, account }), + hasFeesProvided: areFeesProvided(walletApiTransaction), + }; +}; + +export default { getWalletAPITransactionSignFlowInfos }; diff --git a/libs/ledger-live-common/src/generated/walletApiAdapter.ts b/libs/ledger-live-common/src/generated/walletApiAdapter.ts index 7ac7d0821085..613c0eaebdf0 100644 --- a/libs/ledger-live-common/src/generated/walletApiAdapter.ts +++ b/libs/ledger-live-common/src/generated/walletApiAdapter.ts @@ -1,10 +1,12 @@ import bitcoin from "../families/bitcoin/walletApiAdapter"; +import cosmos from "../families/cosmos/walletApiAdapter"; import evm from "../families/evm/walletApiAdapter"; import polkadot from "../families/polkadot/walletApiAdapter"; import xrp from "../families/xrp/walletApiAdapter"; export default { bitcoin, + cosmos, evm, polkadot, xrp,