Sending RPC Requests

Communication with the wallet is sent via RPC requests. This guide will show you how to send an RPC request and how to interpret responses.

Obtain a chain provider

With the steps mentioned in Provider Activation, get a chain provider for the networks you want to interact with. In the following examples we will use both ethereum and constellation providers.

const dagProvider = window.stargazer.getProvider("constellation");
const ethProvider = window.stargazer.getProvider("ethereum");

List active accountarrow-up-right

For listing the active accounts in the wallet you can send the following calls to dag_accounts RPC method and eth_accounts RPC method.

circle-info

Important

The account returned will always be the active account in Stargazer. Both for Constellation and Ethereum providers.

const dagAccounts = await dagProvider.request({ method: "dag_accounts" });
console.log(dagAccounts);
// ["DAG88C9WDSKH451sisyEP3hAkgCKn5DN72fuwjfX"]

const ethAccounts = await ethProvider.request({ method: "eth_accounts" });
console.log(eth_accounts);
// ["0x567d0382442c5178105fC03bd52b8Db6Afb4fE40"]

Read more about dag_accounts RPC method and eth_accounts RPC method.

Send an ETH contract callarrow-up-right

For interaction with ethereum smart contracts you can use the eth_call RPC method and the eth_sendTransaction RPC method, respectively for read and write operations. In the following example we will be using the ethersarrow-up-right package, and a demo contractarrow-up-right from the Stargazer Demosarrow-up-right. The ethersarrow-up-right package will help us encode method parameters based on the contract's ABI. It is encouraged to use external libraries to encode contract call parameters.

circle-info

Important

Interaction with smart contracts is done through an ABI (Application Binary Interface), you can read more about it in the Contract ABI Specificationarrow-up-right article from the solidity docsarrow-up-right.

You can think about an ABI as any other programming interface, where you have defined method signatures and interaction abstractions without the actual implementation.

Send an ETH read callarrow-up-right

In the next example we will use the greet method from the StargazerGreeterarrow-up-right contract. It reads a greet string saved in the network state. For interacting with the contract we will create an ethers Contractarrow-up-right instance, and therefore an ethers Web3Providerarrow-up-right. In the background the ethersarrow-up-right package will call eth_call for us.

Send an ETH contract write callarrow-up-right

In the next example we will use the setGreeting method from the StargazerGreeterarrow-up-right contract. It sets a greet string in the network state. For interacting with the contract we will create an ethers Contractarrow-up-right instance, and therefore an ethers Web3Providerarrow-up-right. In the background the ethersarrow-up-right package will call eth_sendTransaction for us.

circle-info

Important

Write calls need to be confirmed by the user. Read more here.

Send ETH Transactionsarrow-up-right

As the ethereum chain reveals the eth_sendTransaction RPC method you can send any kind of transaction you need (Token Transfer, Contract Interaction, ETH Transfers, etc.).

Transfer ERC20 Tokensarrow-up-right

You can send ERC20 tokens using the transfer method from any ERC20 contract. For interacting with the contract we will create an ethers Contractarrow-up-right instance, and therefore an ethers Web3Providerarrow-up-right. In the background the ethersarrow-up-right package will call eth_sendTransaction for us.

Approve ERC20 token Spendarrow-up-right

You can approve spend of ERC20 tokens to external contracts using the approve method from any ERC20 contract. For interacting with the contract we will create an ethers Contractarrow-up-right instance, and therefore an ethers Web3Providerarrow-up-right. In the background the ethersarrow-up-right package will call eth_sendTransactionarrow-up-right for us.

TypeScript

You can send ETH (The ethereum's native currency) sending a simple transaction to the network. For interacting with the network we will create an ethers Web3Providerarrow-up-right and an ethers Signerarrow-up-right. In the background the ethersarrow-up-right package will call eth_sendTransaction for us.

TypeScript

Last updated

Was this helpful?