@ledgerhq/coin-tezos
Version:
152 lines • 6.08 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const craftTransaction_1 = require("./craftTransaction");
const tezosToolkit_1 = require("./tezosToolkit");
const config_1 = __importDefault(require("../config"));
const rpc_1 = require("@taquito/rpc");
const taquito_1 = require("@taquito/taquito");
jest.mock("./tezosToolkit");
jest.mock("../config", () => ({
getCoinConfig: jest.fn(),
}));
describe("craftTransaction", () => {
const mockTezosToolkit = {
rpc: {
getContract: jest.fn(),
getBlock: jest.fn(),
forgeOperations: jest.fn(),
},
estimate: {
reveal: jest.fn(),
},
};
beforeEach(() => {
jest.clearAllMocks();
tezosToolkit_1.getTezosToolkit.mockReturnValue(mockTezosToolkit);
});
it("should craft a send transaction", async () => {
mockTezosToolkit.rpc.getContract.mockResolvedValue({ counter: "1" });
const account = { address: "tz1..." };
const transaction = {
type: "send",
recipient: "tz2...",
amount: BigInt(1000),
fee: { fees: "100", gasLimit: "200", storageLimit: "300" },
};
const result = await (0, craftTransaction_1.craftTransaction)(account, transaction);
expect(result.type).toBe("OUT");
expect(result.contents).toEqual([
{
kind: rpc_1.OpKind.TRANSACTION,
amount: "1000",
destination: transaction.recipient,
source: account.address,
counter: "2",
fee: "100",
gas_limit: "200",
storage_limit: "300",
},
]);
});
it("should craft a delegate transaction", async () => {
mockTezosToolkit.rpc.getContract.mockResolvedValue({ counter: "1" });
const account = { address: "tz1..." };
const transaction = {
type: "delegate",
recipient: "tz2...",
amount: BigInt(0),
fee: { fees: "100", gasLimit: "200", storageLimit: "300" },
};
const result = await (0, craftTransaction_1.craftTransaction)(account, transaction);
expect(result.type).toBe("DELEGATE");
expect(result.contents).toEqual([
{
kind: rpc_1.OpKind.DELEGATION,
source: account.address,
counter: "2",
delegate: transaction.recipient,
fee: "100",
gas_limit: "200",
storage_limit: "300",
},
]);
});
it("should craft an undelegate transaction", async () => {
mockTezosToolkit.rpc.getContract.mockResolvedValue({ counter: "1" });
const account = { address: "tz1..." };
const transaction = {
type: "undelegate",
recipient: "",
amount: BigInt(0),
fee: { fees: "100", gasLimit: "200", storageLimit: "300" },
};
const result = await (0, craftTransaction_1.craftTransaction)(account, transaction);
expect(result.type).toBe("UNDELEGATE");
expect(result.contents).toEqual([
{
kind: rpc_1.OpKind.DELEGATION,
source: account.address,
counter: "2",
fee: "100",
gas_limit: "200",
storage_limit: "300",
},
]);
});
it("should craft a transaction with reveal operation", async () => {
mockTezosToolkit.rpc.getContract.mockResolvedValue({ counter: "1" });
mockTezosToolkit.estimate.reveal.mockResolvedValue({ gasLimit: 100, storageLimit: 0 });
config_1.default.getCoinConfig.mockReturnValue({ fees: { minRevealGasLimit: 100 } });
const account = { address: "tz1..." };
const transaction = {
type: "send",
recipient: "tz2...",
amount: BigInt(1000),
fee: { fees: "100", gasLimit: "200", storageLimit: "300" },
};
const publicKey = { publicKey: "publicKey", publicKeyHash: "publicKeyHash" };
const result = await (0, craftTransaction_1.craftTransaction)(account, transaction, publicKey);
expect(result.contents).toEqual([
{
kind: rpc_1.OpKind.REVEAL,
fee: taquito_1.DEFAULT_FEE.REVEAL.toString(),
gas_limit: "100",
storage_limit: "0",
source: publicKey.publicKeyHash,
counter: "2",
public_key: publicKey.publicKey,
},
{
kind: rpc_1.OpKind.TRANSACTION,
amount: "1000",
destination: transaction.recipient,
source: account.address,
counter: "3",
fee: "100",
gas_limit: "200",
storage_limit: "300",
},
]);
});
it("should throw an error for unsupported transaction type", async () => {
const account = { address: "tz1..." };
const transaction = {
type: "invalid",
recipient: "tz2...",
amount: BigInt(1000),
fee: { fees: "100", gasLimit: "200", storageLimit: "300" },
};
await expect((0, craftTransaction_1.craftTransaction)(account, transaction)).rejects.toThrow("unsupported mode");
});
it("should include leading watermark byte when using rawEncode", async () => {
mockTezosToolkit.rpc.getBlock.mockResolvedValue({ hash: "aaaa" });
mockTezosToolkit.rpc.forgeOperations.mockResolvedValue("deadcafe");
const rawTx = await (0, craftTransaction_1.rawEncode)([]);
// 0x03 is a conventional prefix (aka a watermark) for tezos transactions
expect(rawTx).toEqual("03deadcafe");
});
});
//# sourceMappingURL=craftTransaction.test.js.map