Building Transactions
Get step-by-step guidance on preparing and sending transactions using the DvlClient.
In the src/instructions_data
folder, there are structures needed to fill the buffer for specific transactions, and in
the src/instructions_data/instructions.rs
file, there is a list of available smart contract instructions. However,
this is not the most convenient way to compile instructions, so the library provides constructors for instructions and
transactions.
Since this SDK is common for the smart contract, client applications, frontend, and administrative tools, you can view all transactions available on the platform. However, you can only execute custom user transactions.
Custom User Transactions
InstructionOptionTrade
Option trading or bucket-based trading. In this instruction, you need to specify the stake size in each bucket (positive number for buying, negative for selling). Optionally, you can also specify:
- Basket (from zero to 4 positions): if specified, this deal will be reflected in the portfolio on the website (option trading); if not specified, it will not be (bucket trading).
- Maximum cost of the transaction (the final price may vary slightly at the time of the actual transaction).
InstructionLpTrade
Buying or selling pool tokens (participating in the pool as a liquidity provider). The only parameter is the amount of pool tokens being bought or sold (positive number for buying, negative for selling).
InstructionPayoff
Receiving payout upon the expiration of the pool. Parameters:
worker_id
: the number of the worker account servicing the pool.pool_id
: the sequential number of the pool (expiration). One worker services consecutive pools, so it is necessary to specify the pool number as well.
InstructionTransferToken
Transferring money for trading on the platform.
mint_id
: unique number of the currency according to MintsAccount (base currency).amount
: quantity.
InstructionWithdrawToken
Withdrawing funds from the platform.
mint_id
: unique number of the currency according to MintsAccount (base currency).amount
: quantity.
Instruction Constructor (Data Fields Only)
Similar to reading an account, the library provides an abstract factory for constructing the data buffer of the required instruction, requesting parameters specific to that instruction. There is no need to fill all the data fields yourself - just prepare the specific instruction parameters and call the factory:
Transaction Constructor
Once you have prepared the instruction data, you can either use them to compose the instruction yourself by serializing
them using the built-in method to_vec_le
:
Preparing the Transaction
After which, you should prepare the transaction.
However, the recommended method is using the as_transaction_instruction
. Let’s consider a complete basic example of
constructing a transaction from the moment of creating a DvlClient
instance to getting a transaction ready for
sending:
Using the as_transaction_instruction
Method
The as_transaction_instruction
method is available on any object derived from DvlInstruction
. It takes DvlClient
as an argument to specify the transaction with the correct account keys, as these keys are PDAs (Program Derived
Addresses) and are linked to a specific branch of the smart contract.
Sending the Transaction
Once you have the instruction, you need to add it to the final transaction and send it via the RPC client (you can use
the one passed to DvlClient
). If desired, you can use the built-in method for sending transactions, but it is
provided “as is,” and most likely, you will want to write your own: