TxBuilder
plu-ts/offchain
exposes the TxBuilder
class.
How to use
Its purpose is to allow you to build transactions more easly.
You could infact build transactions by calling the Tx
constructor.
However that requires a profound understanding of the structure of a Cardano transaction.
for this reason we have the TxBuilder
.
You can get your TxBuilder
by passing the ProtocolParameters
(that you'll need to query)
needed for the network you are building transactions for.
constructor definiton:
constructor(
protocolParamters: Readonly<ProtocolParameters>,
genesisInfos?: GenesisInfos
)
example:
type MyFavoriteServiceResponse = any
// you'll need to adapt the protocol parameters
// to the expected interface
function adaptServiceProtocolParams(
response: MyFavoriteServiceResponse
): ProtocolParameters
{
/* do your magic */
}
const txBuilder = new TxBuilder(
adaptServiceProtocolParams(
await fetch("my-favorite-service.com/api/protocol-parameters")
)
);
Methods
once you have your tx builder you can use all the following methods.
buildSync
runWithProvider
keepRelevant
calcLinearFee
getMinimumOutputLovelaces
setGenesisInfos
slotToPOSIX
posixToSlot
overrideTxRedeemers
buildSync
or runWithProvider
once you have a TxBuilder
instance there are 2 ways to build a transaction:
- using
buildSync
- getting a
TxBuilderRunner
by passing aPartial<IProvider>
to therunWithProvider
method
Pro and Cons
The buildSync
method gives you full controll,
you need to query the resolved utxos for the inputs,
as well any other information required for transaction validation.
Once you provide the data as specified in by ITxBuildArgs
the TxBuilder
instance will syncronously build and validate your transaction.
If you don't want to take care of querying the data, you can pass a Provider
to the runWithProvider
method,
to get a TxBuilderRunner
instance.
The runner has many method that abstract the buildSync
interface,
so that you can describe your transaction an a much higher level,
and if some that is missing it will be queried asynchronously using the Provider
that was passed.