UNPKG

@ledgerhq/coin-canton

Version:
66 lines (59 loc) 1.74 kB
import { AlpacaApi, Block, BlockInfo, FeeEstimation, TransactionIntent, } from "@ledgerhq/coin-framework/api/index"; import coinConfig, { type CantonConfig } from "../config"; import { broadcast, combine, craftTransaction, estimateFees, getBalance, getNextValidSequence, lastBlock, listOperations, } from "../common-logic"; import BigNumber from "bignumber.js"; export function createApi(config: CantonConfig): AlpacaApi { coinConfig.setCoinConfig(() => ({ ...config, status: { type: "active" } })); return { broadcast, combine, craftTransaction: craft, estimateFees: estimate, getBalance, lastBlock, listOperations, getBlock(_height): Promise<Block> { throw new Error("getBlock is not supported"); }, getBlockInfo(_height: number): Promise<BlockInfo> { throw new Error("getBlockInfo is not supported"); }, }; } async function craft(transactionIntent: TransactionIntent): Promise<string> { const nextSequenceNumber = await getNextValidSequence(transactionIntent.sender); const tx = await craftTransaction( { address: transactionIntent.sender, nextSequenceNumber }, { recipient: transactionIntent.recipient, amount: new BigNumber(transactionIntent.amount.toString()), }, ); return tx.serializedTransaction; } async function estimate(transactionIntent: TransactionIntent): Promise<FeeEstimation> { const { serializedTransaction } = await craftTransaction( { address: transactionIntent.sender }, { recipient: transactionIntent.recipient, amount: new BigNumber(transactionIntent.amount.toString()), }, ); const value = await estimateFees(serializedTransaction); return { value }; }