Injective
The following is an explanation of how to initiate a transfer transaction in Injective network by invoking the through dapp.request
. We recommend utilizing a dedicated library rather than accessing the service directly if you want a greater degree of abstraction than the official API provides.
To send a transaction in Injective network, it needs to be followed the steps below.
- Detecting of Universal Provider (
window.dapp
) - Detecting the Injective network to which the user is linked
- Import the Injective account of the user
The WELLDONE Wallet finds and imports networks associated with that wallet address. Before submitting a transaction, you should evaluate whether to transmit it to the mainnet or the testnet. The following format can be used to transmit the transaction:
const response = await dapp.request('injective', {
method: 'dapp:signAndSendTransaction',
params: [JSON.stringify(transactionParameters)],
});
1. Returns
This method returns the transaction hash value as a Promise
object of type string because you can send multiple transactions as well as one transaction.
Promise<string[]>;
2. Params
The dapp:signAndSendTransaction
method takes the transaction as HEX string type HEX_STRING_TX_DATA
. However, Injective networks can take the transaction parameters as JSON string type.
interface TransactionParameters {
signerData: {
accountNumber: string;
sequence: string;
chainId: string;
};
fee: {
amount: [
{
denom: string;
amount: string;
},
];
gas: string;
};
memo: string;
msgs: [
{
typeUrl: '/cosmos.bank.v1beta1.MsgSend';
value: {
fromAddress: string;
toAddress: string;
amount: [{ denom: string; amount: string }];
};
},
];
sequence: string;
}
- The
value
depends on the type oftypeUrl
and the method of the contract you want to execute. The above parameters are examples of transaction types that send coins to other accounts.
3. Example
const sendTransaction = async () => {
// get accounts first
const accounts = await dapp.request('injective', { method: 'dapp:accounts' });
const chainId = 'injective-888';
const lcdClient = new ChainRestAuthApi(network.rest);
const fetchAccount = await lcdClient.fetchAccount(accounts[CHAIN_NAME].address);
const sequence = fetchAccount.account.base_account.sequence;
const accountNumber = fetchAccount.account.base_account.account_number;
// creating a transaction
const const transactionParameters = {
signerData: {
accountNumber,
sequence,
chainId,
},
fee: {
amount: [
{
denom: 'inj',
amount: '90000000000000',
},
],
gas: '180000', // 180k
},
memo: '',
msgs: [
{
typeUrl: '/cosmos.bank.v1beta1.MsgSend',
value: {
fromAddress: accounts,
toAddress: accounts, //allthatnode
amount: [{ denom: 'inj', amount: '10000' }],
},
},
],
sequence: `${sequence}`,
};
// sending a transaction
try {
const response = await dapp.request('injective', {
method: 'dapp:signAndSendTranssaction',
params: [JSON.stringify(transactionParameters)],
});
const txHash = response[0];
} catch (error) {
/*
{
message: 'User denied transaction signature',
code: 4001,
}
*/
}
};
To complete the transaction, follow the steps outlined below. The testnet token is required to transmit a transaction. You can request faucet through the FAUCET tab in the wallet.