-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
libs/ledger-live-common/src/families/aptos/js-prepareTransaction.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import prepareTransaction from "./js-prepareTransaction"; | ||
import { AptosAPI } from "./api"; | ||
import { getEstimatedGas } from "./js-getFeesForTransaction"; | ||
import { getMaxSendBalance } from "./logic"; | ||
import BigNumber from "bignumber.js"; | ||
import type { Account } from "@ledgerhq/types-live"; | ||
import type { Transaction } from "./types"; | ||
|
||
jest.mock("./api"); | ||
jest.mock("./js-getFeesForTransaction"); | ||
jest.mock("./logic"); | ||
|
||
describe("Aptos prepareTransaction", () => { | ||
describe("prepareTransaction", () => { | ||
let account: Account; | ||
let transaction: Transaction; | ||
|
||
beforeEach(() => { | ||
account = { | ||
id: "test-account-id", | ||
name: "Test Account", | ||
currency: { | ||
id: "aptos", | ||
name: "Aptos", | ||
ticker: "APT", | ||
units: [{ name: "Aptos", code: "APT", magnitude: 6 }], | ||
}, | ||
spendableBalance: new BigNumber(1000), | ||
balance: new BigNumber(1000), | ||
blockHeight: 0, | ||
operations: [], | ||
pendingOperations: [], | ||
unit: { code: "APT", name: "Aptos", magnitude: 6 }, | ||
lastSyncDate: new Date(), | ||
subAccounts: [], | ||
} as unknown as Account; | ||
|
||
transaction = { | ||
amount: new BigNumber(0), | ||
recipient: "", | ||
useAllAmount: false, | ||
fees: new BigNumber(0), | ||
firstEmulation: true, | ||
options: {}, | ||
} as Transaction; | ||
}); | ||
|
||
it("should return the transaction if recipient is not set", async () => { | ||
const result = await prepareTransaction(account, transaction); | ||
expect(result).toEqual(transaction); | ||
}); | ||
|
||
it("should return the transaction with zero fees if amount is zero and useAllAmount is false", async () => { | ||
transaction.recipient = "test-recipient"; | ||
const result = await prepareTransaction(account, transaction); | ||
expect(result.fees?.isZero()).toBe(true); | ||
}); | ||
|
||
it("should set the amount to max sendable balance if useAllAmount is true", async () => { | ||
transaction.recipient = "test-recipient"; | ||
transaction.useAllAmount = true; | ||
(getMaxSendBalance as jest.Mock).mockReturnValue(new BigNumber(900)); | ||
(getEstimatedGas as jest.Mock).mockResolvedValue({ | ||
fees: new BigNumber(2000), | ||
estimate: { maxGasAmount: new BigNumber(200), gasUnitPrice: new BigNumber(10) }, | ||
errors: {}, | ||
}); | ||
|
||
const result = await prepareTransaction(account, transaction); | ||
expect(result.amount.isEqualTo(new BigNumber(900))).toBe(true); | ||
expect(result.fees?.isEqualTo(new BigNumber(2000))).toBe(true); | ||
expect(new BigNumber(result.estimate.maxGasAmount).isEqualTo(new BigNumber(200))).toBe(true); | ||
expect(result.errors).toEqual({}); | ||
}); | ||
|
||
it("should call getEstimatedGas and set the transaction fees, estimate, and errors", async () => { | ||
transaction.recipient = "test-recipient"; | ||
transaction.amount = new BigNumber(100); | ||
(getEstimatedGas as jest.Mock).mockResolvedValue({ | ||
fees: new BigNumber(10), | ||
estimate: { maxGasAmount: new BigNumber(200) }, | ||
errors: {}, | ||
}); | ||
|
||
const result = await prepareTransaction(account, transaction); | ||
expect(getEstimatedGas).toHaveBeenCalledWith(account, transaction, expect.any(AptosAPI)); | ||
expect(result.fees?.isEqualTo(new BigNumber(10))).toBe(true); | ||
expect(new BigNumber(result.estimate.maxGasAmount).isEqualTo(new BigNumber(200))).toBe(true); | ||
expect(result.errors).toEqual({}); | ||
}); | ||
|
||
it("should set firstEmulation to false after the first call", async () => { | ||
transaction.recipient = "test-recipient"; | ||
transaction.amount = new BigNumber(100); | ||
(getEstimatedGas as jest.Mock).mockResolvedValue({ | ||
fees: new BigNumber(10), | ||
estimate: { maxGasAmount: new BigNumber(200) }, | ||
errors: {}, | ||
}); | ||
|
||
const result = await prepareTransaction(account, transaction); | ||
expect(result.firstEmulation).toBe(false); | ||
}); | ||
|
||
//-------------------------------------------------------------------------------- | ||
it("should return the transaction with updated fees and estimate if recipient is set and amount is not zero", async () => { | ||
transaction.recipient = "test-recipient"; | ||
transaction.amount = new BigNumber(100); | ||
(getEstimatedGas as jest.Mock).mockResolvedValue({ | ||
fees: new BigNumber(2000), | ||
estimate: { maxGasAmount: new BigNumber(200), gasUnitPrice: new BigNumber(10) }, | ||
errors: {}, | ||
}); | ||
|
||
const result = await prepareTransaction(account, transaction); | ||
expect(result.fees?.isEqualTo(new BigNumber(2000))).toBe(true); | ||
expect(new BigNumber(result.estimate.maxGasAmount).isEqualTo(new BigNumber(200))).toBe(true); | ||
expect(result.errors).toEqual({}); | ||
}); | ||
|
||
it("should set maxGasAmount in options if firstEmulation is true", async () => { | ||
transaction.recipient = "test-recipient"; | ||
transaction.amount = new BigNumber(100); | ||
transaction.firstEmulation = true; | ||
(getEstimatedGas as jest.Mock).mockResolvedValue({ | ||
fees: new BigNumber(2000), | ||
estimate: { maxGasAmount: new BigNumber(200), gasUnitPrice: new BigNumber(10) }, | ||
errors: {}, | ||
}); | ||
|
||
const result = await prepareTransaction(account, transaction); | ||
expect(new BigNumber(result.estimate.maxGasAmount).isEqualTo(new BigNumber(200))).toBe(true); | ||
expect(result.firstEmulation).toBe(false); | ||
}); | ||
|
||
it("should not change transaction.options.maxGasAmount if firstEmulation is false", async () => { | ||
transaction.recipient = "test-recipient"; | ||
transaction.amount = new BigNumber(100); | ||
transaction.firstEmulation = false; | ||
(getEstimatedGas as jest.Mock).mockResolvedValue({ | ||
fees: new BigNumber(2000), | ||
estimate: { maxGasAmount: new BigNumber(200), gasUnitPrice: new BigNumber(10) }, | ||
errors: {}, | ||
}); | ||
|
||
const result = await prepareTransaction(account, transaction); | ||
expect(result.options.maxGasAmount).toBeUndefined(); | ||
expect(result.firstEmulation).toBe(false); | ||
}); | ||
}); | ||
}); |