In this document we are going to explain the concepts of creating Transactions on Injective (on a higher level), signing them using a privateKey (wallet) and broadcasting them to the node.
Every transaction on Injective follows the same flow. The flow consists of three steps: preparing, signing and broadcasting the transaction. Let's dive into each step separately and explain the process in-depth (including examples) so we can understand the whole transaction flow.
First of, we need to prepare the transaction for signing. There are two different ways to do this - depending on the type of the wallet you want to use for signing.
To use Ethereum native wallets, we have to convert the transaction to EIP712 typed data and use the wallet to sign this typed data. There are two approaches we can take to achieve this:
Using the web3-gateway API which accepts a Message (and some other details) and returns EIP712 TypedData that can be signed,
So, what is the web3-gateway? The web3-gateway is a microservice which is part of the indexer-api and allows developers to introduce fee delegation service for their end-users (meaning that the end-users will not pay gas when they submit transactions, this gas will be covered by the owner of the web3-gateway microservice) and converting the transaction into a EIP712 TypedData that can be signed using Ethereum native wallets. Full Example
Using our custom abstraction for the Messages which allows the developer to get EIP712 TypedData straight from the proto file of the particular message (experimental at this point but the default and the proper way).
With this approach we have to prepare everything about the transaction on the client side, including the Transaction context (fees, accountDetails, etc). Full Example
At this point you can't use some online abstractions that provide a quick way to prepare the transaction for you based on the provided Message and the signer (ex. using the @cosmjs/stargate package). The reason why is that these packages don't support Injective's publicKey typeUrl, so we have to do the preparation of the address on the client side.
Worry not, we have provided functions that will prepare the txRaw transaction for you in our sdks. txRaw is the transaction interface used in Cosmos that contains details about the transaction and the signer itself. Full Example
Once you get the txRaw transaction use any Cosmos native wallet to sign (ex: Keplr),
You can also use our @injectivelabs/wallet-ts package to get out-of-the-box wallet provides that will give you abstracted methods which you can use to sign transaction. Refer to the documentation of the package, its really simple to setup and use.
Once we have the signature ready, we need to broadcast the transaction to the Injective chain itself. Same as preparing the transaction, depending on the wallet (and the approach we want to take) broadcasting the transaction takes a different form.
Using the web3-gateway API which accepts the response from the prepareTx and the signature and broadcasts the transaction to the node,
Using the client side approach where we make a native cosmos transaction, include the signature we got from the wallet provider and broadcast it to the chain
As we already should have prepared the txRaw before in the preparing of the transaction step, we just include the signature in the txRaw and broadcast it to the chain.