Skip to content

Commit

Permalink
feat(xlm): add lastBlock function
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Prohaszka <[email protected]>
  • Loading branch information
sprohaszka-ledger committed Jul 17, 2024
1 parent f9b64f6 commit 095e290
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
8 changes: 4 additions & 4 deletions libs/coin-modules/coin-stellar/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import type { Api } from "@ledgerhq/coin-framework/api/index";
import coinConfig, { type StellarConfig } from "../config";
import {
// broadcast,
broadcast,
// combine,
craftTransaction,
// estimateFees,
getBalance,
listOperations,
// lastBlock,
lastBlock,
// rawEncode,
} from "../logic";

export function createApi(config: StellarConfig): Api {
coinConfig.setCoinConfig(() => ({ ...config, status: { type: "active" } }));

return {
broadcast: () => Promise.reject(new Error("Method not supported")),
broadcast,
combine: () => {
throw new Error("Method not supported");
},
craftTransaction: craft,
estimateFees: () => Promise.reject(new Error("Method not supported")),
getBalance,
lastBlock: () => Promise.reject(new Error("Method not supported")),
lastBlock,
listOperations,
};
}
Expand Down
1 change: 1 addition & 0 deletions libs/coin-modules/coin-stellar/src/logic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export { broadcast } from "./broadcast";
export { craftTransaction } from "./craftTransaction";
// export { estimateFees } from "./estimateFees";
export { getBalance } from "./getBalance";
export { lastBlock } from "./lastBlock";
export { listOperations } from "./listOperations";
6 changes: 6 additions & 0 deletions libs/coin-modules/coin-stellar/src/logic/lastBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { BlockInfo } from "@ledgerhq/coin-framework/api/index";
import { getLastBlock } from "../network";

export async function lastBlock(): Promise<BlockInfo> {
return await getLastBlock();
}
23 changes: 13 additions & 10 deletions libs/coin-modules/coin-stellar/src/network/horizon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@ import type { Account, Operation } from "@ledgerhq/types-live";
import {
// @ts-expect-error stellar-sdk ts definition missing?
AccountRecord,
Asset,
BASE_FEE,
Horizon,
NetworkError,
Networks,
NotFoundError,
Account as StellarSdkAccount,
Operation as StellarSdkOperation,
Transaction as StellarSdkTransaction,
TransactionBuilder,
StrKey,
MuxedAccount,
// @ts-expect-error stellar-sdk ts definition missing?
AccountRecord,
} from "@stellar/stellar-sdk";
import { BigNumber } from "bignumber.js";
import {
Expand Down Expand Up @@ -88,10 +82,6 @@ Horizon.AxiosClient.interceptors.response.use(response => {
return response;
});

function getFormattedAmount(amount: BigNumber) {
return amount.div(new BigNumber(10).pow(currency.units[0].magnitude)).toString(10);
}

export async function fetchBaseFee(): Promise<{
baseFee: number;
recommendedFee: number;
Expand Down Expand Up @@ -318,6 +308,19 @@ export async function loadAccount(addr: string): Promise<AccountRecord | null> {
}
}

export async function getLastBlock(): Promise<{
height: number;
hash: string;
time: Date;
}> {
const ledger = await getServer().ledgers().order("desc").limit(1).call();
return {
height: ledger.records[0].sequence,
hash: ledger.records[0].hash,
time: new Date(ledger.records[0].closed_at),
};
}

export const getRecipientAccount: CacheRes<
Array<{
recipient: string;
Expand Down
1 change: 1 addition & 0 deletions libs/coin-modules/coin-stellar/src/network/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
fetchSequence,
fetchSigners,
fetchAccountNetworkInfo,
getLastBlock,
getRecipientAccount,
loadAccount,
BASE_RESERVE,
Expand Down

0 comments on commit 095e290

Please sign in to comment.