import { BlockTxBroadcastResult , Coin , Coins , LCDClient , Msg , MsgSend , Numeric , Fee , Wallet , Tx , } from '@terra-money/terra.js' ; import { Fabricator , OmitAddress } from '../fabricators' ; import { AddressProvider } from '../address-provider' ; import { SyncTxBroadcastResult , } from '@terra-money/terra.js/dist/client/lcd/api/TxAPI' ; import { Parse } from '../utils' ; import accAddress = Parse . accAddress ; export interface OperationGasParameters { fee ? : Fee ; gasPrices ? : Coins . Input ; gasAdjustment ? : Numeric . Input ; } export interface Operation { generateWithAddress ( address ) : Msg [ ] ; generateWithWallet ( wallet ) : Msg [ ] ; creatTx ( wallet , gasParameters ) : Promise < Tx > ; execute ( wallet , gasParameters ) : Promise < SyncTxBroadcastResult > ; } export class OperationImpl < FabricatorInputType > implements Operation { private _fabricator ! : Fabricator < FabricatorInputType > ; private _option ! : OmitAddress < FabricatorInputType > ; private _addressProvider ! : AddressProvider ; constructor ( fabricator , option , addressProvider ) { this . _fabricator = fabricator ; this . _option = option ; this . _addressProvider = addressProvider ; } generateWithAddress ( address ) { return this . _fabricator ( ( { address , ... this . _option , } as unknown ) as FabricatorInputType ) ( this . _addressProvider ) ; } generateWithWallet ( wallet ) { return this . generateWithAddress ( wallet . key . accAddress ) ; } async creatTx ( wallet , { fee , gasPrices , gasAdjustment } ) { return wallet . createAndSignTx ( { fee , gasAdjustment , gasPrices , msgs : this . _fabricator ( ( { address : wallet . key . accAddress , ... this . _option , } as unknown ) as FabricatorInputType ) ( this . _addressProvider ) , } ) ; } async execute ( wallet , { fee , gasPrices , gasAdjustment } ) { return wallet . createAndSignTx ( { fee , gasAdjustment , gasPrices , msgs : this . _fabricator ( ( { address : wallet . key . accAddress , ... this . _option , } as unknown ) as FabricatorInputType ) ( this . _addressProvider ) , } ) . then ( ( tx ) => { return wallet . lcd . tx . broadcast ( tx ) ; } ) ; } } export async function sendSignedTransaction ( lcd , tx ) { return await lcd . tx . broadcastSync ( tx ) ; } export function createNativeSend ( sender , options ) { return new MsgSend ( sender , accAddress ( options . recipient ) , [ options . coin ] ) ; } export function createAndSignMsg ( wallet , { fee , gasPrices , gasAdjustment } , msgs ) { return wallet . createAndSignTx ( { fee , gasAdjustment , gasPrices , msgs : msgs , } ) ; }