@ledgerhq/coin-stellar
Version:
Ledger Stellar Coin integration
149 lines • 5.88 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const stellar_sdk_1 = require("@stellar/stellar-sdk");
const _1 = require(".");
/**
* Testnet scan: https://testnet.lumenscan.io/
*
* Tests are skipped for the moment due to TooManyRequest errors
*/
describe.skip("Stellar Api", () => {
let module;
const ADDRESS = "GBAUZBDXMVV7HII4JWBGFMLVKVJ6OLQAKOCGXM5E2FM4TAZB6C7JO2L7";
beforeAll(() => {
module = (0, _1.createApi)({
explorer: {
url: "https://horizon-testnet.stellar.org/",
},
});
});
describe("estimateFees", () => {
it("returns a default value", async () => {
// Given
const amount = BigInt(100_000);
// When
const result = await module.estimateFees({
asset: { type: "native" },
type: "send",
sender: ADDRESS,
recipient: "address",
amount: amount,
memo: { type: "NO_MEMO" },
});
// Then
expect(result).toEqual(BigInt(100));
});
});
describe("listOperations", () => {
let txs;
beforeAll(async () => {
[txs] = await module.listOperations(ADDRESS, { minHeight: 0 });
});
it("returns a list regarding address parameter", async () => {
expect(txs.length).toBeGreaterThanOrEqual(100);
txs.forEach(operation => {
const isSenderOrReceipt = operation.senders.includes(ADDRESS) || operation.recipients.includes(ADDRESS);
expect(isSenderOrReceipt).toBeTruthy();
});
});
it("returns all operations", async () => {
expect(txs.length).toBeGreaterThanOrEqual(100);
const checkSet = new Set(txs.map(elt => elt.tx.hash));
expect(checkSet.size).toEqual(txs.length);
});
});
describe("lastBlock", () => {
it("returns last block info", async () => {
// When
const result = await module.lastBlock();
// Then
expect(result.hash).toBeDefined();
expect(result.height).toBeDefined();
expect(result.time).toBeInstanceOf(Date);
});
});
describe("getBalance", () => {
it("returns a list regarding address parameter", async () => {
// When
const result = await module.getBalance(ADDRESS);
// Then
expect(result).toBeGreaterThan(0);
});
});
describe("craftTransaction", () => {
const TYPE = "send";
const RECIPIENT = "GD6QELUZPSKPRWVXOQ3F6GBF4OBRMCHO5PHREXH4ZRTPJAG7V5MD7JGX";
const AMOUNT = BigInt(1_000_000);
function readFees(transactionXdr) {
const transactionEnvelope = (0, _1.envelopeFromAnyXDR)(transactionXdr, "base64");
return transactionEnvelope.value().tx().fee();
}
function readMemo(transactionXdr) {
const transactionEnvelope = (0, _1.envelopeFromAnyXDR)(transactionXdr, "base64");
return transactionEnvelope.value().tx().memo();
}
it("returns a raw transaction", async () => {
const result = await module.craftTransaction({
asset: { type: "native" },
type: TYPE,
sender: ADDRESS,
recipient: RECIPIENT,
amount: AMOUNT,
memo: { type: "NO_MEMO" },
});
const envelope = (0, _1.envelopeFromAnyXDR)(result, "base64");
expect(envelope.toXDR("base64").length).toEqual(188);
});
it("should use estimated fees when user does not provide them for crafting a transaction", async () => {
const transactionXdr = await module.craftTransaction({
asset: { type: "native" },
type: TYPE,
sender: ADDRESS,
recipient: RECIPIENT,
amount: AMOUNT,
memo: { type: "NO_MEMO" },
});
const fees = readFees(transactionXdr);
expect(fees).toBeGreaterThan(0);
});
it("should use custom user fees when user provides it for crafting a transaction", async () => {
const customFees = 99n;
const transactionXdr = await module.craftTransaction({
asset: { type: "native" },
type: TYPE,
sender: ADDRESS,
recipient: RECIPIENT,
amount: AMOUNT,
memo: { type: "NO_MEMO" },
}, customFees);
const fees = readFees(transactionXdr);
expect(fees).toEqual(Number(customFees));
});
it("should have no memo when not provided by user", async () => {
const transactionXdr = await module.craftTransaction({
asset: { type: "native" },
type: TYPE,
sender: ADDRESS,
recipient: RECIPIENT,
amount: AMOUNT,
memo: { type: "NO_MEMO" },
});
expect(readMemo(transactionXdr)).toEqual(stellar_sdk_1.xdr.Memo.memoNone());
});
it("should have a memo when provided by user", async () => {
const transactionXdr = await module.craftTransaction({
asset: { type: "native" },
type: TYPE,
sender: ADDRESS,
recipient: RECIPIENT,
amount: AMOUNT,
memo: {
type: "MEMO_TEXT",
value: "test",
},
});
expect(readMemo(transactionXdr)).toEqual(stellar_sdk_1.xdr.Memo.memoText(Buffer.from("test", "ascii")));
});
});
});
//# sourceMappingURL=index.integ.test.js.map