// SPDX-FileCopyrightText: © 2026 LEDGER SAS // SPDX-License-Identifier: Apache-2.0 import type { Unit } from '@ledgerhq/coin-module-framework/api/types' import type { Account as StellarSdkAccount } from '@stellar/stellar-sdk' import { Operation as StellarSdkOperation, TransactionBuilder, Networks, Asset, } from '@stellar/stellar-sdk' export function buildTransactionBuilder(source: StellarSdkAccount, fee: bigint) { const formattedFee = fee.toString() return new TransactionBuilder(source, { fee: formattedFee, networkPassphrase: Networks.PUBLIC, }) } export function buildChangeTrustOperation(assetCode: string, assetIssuer: string) { return StellarSdkOperation.changeTrust({ asset: new Asset(assetCode, assetIssuer), }) } export function buildCreateAccountOperation(unit: Unit, destination: string, amount: bigint) { const formattedAmount = getFormattedAmount(unit, amount) return StellarSdkOperation.createAccount({ destination: destination, startingBalance: formattedAmount, }) } export function buildPaymentOperation({ unit, destination, amount, assetCode, assetIssuer, }: { unit: Unit destination: string amount: bigint assetCode: string | undefined assetIssuer: string | undefined }) { const formattedAmount = getFormattedAmount(unit, amount) // Non-native assets should always have asset code and asset issuer. If an // asset doesn't have both, we assume it is native asset. const asset = assetCode && assetIssuer ? new Asset(assetCode, assetIssuer) : Asset.native() return StellarSdkOperation.payment({ destination: destination, amount: formattedAmount, asset, }) } function getFormattedAmount(unit: Unit, amount: bigint) { const div = 10 ** unit.magnitude // BigInt division is always an integer, never a float. We need to convert first to a Number. return (Number(amount) / div).toString() }