# 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*](https://docs.constellationnetwork.io/stargazer-wallet/guide/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.

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

### List active account[​](https://docs.constellationnetwork.io/stargazer/Guide/sendingRPCRequests#list-active-account) <a href="#list-active-account" id="list-active-account"></a>

For listing the active accounts in the wallet you can send the following calls to [`dag_accounts`](https://docs.constellationnetwork.io/stargazer-wallet/constellation-rpc-api/dag_accounts) RPC method and [`eth_accounts`](https://docs.constellationnetwork.io/stargazer-wallet/ethereum-rpc-api/eth_accounts) RPC method.

{% hint style="info" %}
**Important**

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

```typescript
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*](https://docs.constellationnetwork.io/stargazer-wallet/constellation-rpc-api/dag_accounts) *and* [*`eth_accounts` RPC method*](https://docs.constellationnetwork.io/stargazer-wallet/ethereum-rpc-api/eth_accounts)*.*

### Send an ETH contract call[​](https://docs.constellationnetwork.io/stargazer/Guide/sendingRPCRequests#send-an-eth-contract-call) <a href="#send-an-eth-contract-call" id="send-an-eth-contract-call"></a>

For interaction with ethereum smart contracts you can use the [`eth_call`](https://docs.constellationnetwork.io/stargazer-wallet/ethereum-rpc-api/eth_call) RPC method and the [`eth_sendTransaction`](https://docs.constellationnetwork.io/stargazer-wallet/ethereum-rpc-api/eth_sendtransaction) RPC method, respectively for read and write operations. In the following example we will be using the [ethers](https://www.npmjs.com/package/ethers) package, and a [demo contract](https://sepolia.etherscan.io/address/0x74299a718b2c44483a27325d7725f0b2646de3b1#code) from the [Stargazer Demos](https://github.com/StardustCollective/stargazer-wallet-demos). The [ethers](https://www.npmjs.com/package/ethers) 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.

{% hint style="info" %}
**Important**

Interaction with smart contracts is done through an ABI (Application Binary Interface), you can read more about it in the [Contract ABI Specification](https://docs.soliditylang.org/en/v0.6.0/abi-spec.html) article from the [solidity docs](https://docs.soliditylang.org/en/v0.6.0/index.html).

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

#### Send an ETH read call[​](https://docs.constellationnetwork.io/stargazer/Guide/sendingRPCRequests#send-an-eth-read-call) <a href="#send-an-eth-read-call" id="send-an-eth-read-call"></a>

In the next example we will use the `greet` method from the [StargazerGreeter](https://sepolia.etherscan.io/address/0x74299a718b2c44483a27325d7725f0b2646de3b1#code) contract. It reads a greet string saved in the network state. For interacting with the contract we will create an ethers [`Contract`](https://docs.ethers.io/v5/api/contract/contract/#Contract--creating) instance, and therefore an ethers [`Web3Provider`](https://docs.ethers.io/v5/api/providers/other/#Web3Provider). In the background the [ethers](https://www.npmjs.com/package/ethers) package will call [`eth_call`](https://docs.constellationnetwork.io/stargazer-wallet/ethereum-rpc-api/eth_call) for us.

```typescript
import * as ethers from "ethers";

const ethersProvider = new ethers.providers.Web3Provider(ethProvider);

const StargazerGreeterAddress = "0x74299a718b2c44483a27325d7725f0b2646de3b1";
const StargazerGreeterABI = [...[]]; // You can get StargazerGreeter's ABI from https://sepolia.etherscan.io/address/0x74299a718b2c44483a27325d7725f0b2646de3b1#code;

const contract = new ethers.Contract(
  StargazerGreeterAddress,
  StargazerGreeterABI,
  ethersProvider
);

await contract.greet();
// "Bon Matin!"
```

#### Send an ETH contract write call[​](https://docs.constellationnetwork.io/stargazer/Guide/sendingRPCRequests#send-an-eth-contract-write-call) <a href="#send-an-eth-contract-write-call" id="send-an-eth-contract-write-call"></a>

In the next example we will use the `setGreeting` method from the [StargazerGreeter](https://sepolia.etherscan.io/address/0x74299a718b2c44483a27325d7725f0b2646de3b1#code) contract. It sets a greet string in the network state. For interacting with the contract we will create an ethers [`Contract`](https://docs.ethers.io/v5/api/contract/contract/#Contract--creating) instance, and therefore an ethers [`Web3Provider`](https://docs.ethers.io/v5/api/providers/other/#Web3Provider). In the background the [ethers](https://www.npmjs.com/package/ethers) package will call [`eth_sendTransaction`](https://docs.constellationnetwork.io/stargazer-wallet/ethereum-rpc-api/eth_sendtransaction) for us.

{% hint style="info" %}
**Important**

Write calls need to be confirmed by the user. Read more [here](#send-an-eth-contract-call).
{% endhint %}

```typescript
import * as ethers from "ethers";

const ethersProvider = new ethers.providers.Web3Provider(ethProvider);

const signer = ethersProvider.getSigner();

const StargazerGreeterAddress = "0x74299a718b2c44483a27325d7725f0b2646de3b1";
const StargazerGreeterABI = [...[]]; // You can get StargazerGreeter's ABI from https://sepolia.etherscan.io/address/0x74299a718b2c44483a27325d7725f0b2646de3b1#code;

const contract = new ethers.Contract(
  StargazerGreeterAddress,
  StargazerGreeterABI,
  signer
);

const greetingId = 1; // Bon Matin!

// We send a transaction to the network
const trxResponse = await contract.setGreeting(greetingId);

// We wait for confirmation
const trxReceipt = await library.waitForTransaction(trxResponse.hash);

console.log(trxReceipt.blockNumber);
// 12415408
```

### Send ETH Transactions[​](https://docs.constellationnetwork.io/stargazer/Guide/sendingRPCRequests#send-eth-transactions) <a href="#send-eth-transactions" id="send-eth-transactions"></a>

As the ethereum chain reveals the [`eth_sendTransaction`](https://docs.constellationnetwork.io/stargazer-wallet/ethereum-rpc-api/eth_sendtransaction) RPC method you can send any kind of transaction you need (Token Transfer, Contract Interaction, ETH Transfers, etc.).

#### Transfer ERC20 Tokens[​](https://docs.constellationnetwork.io/stargazer/Guide/sendingRPCRequests#transfer-erc20-tokens) <a href="#transfer-erc20-tokens" id="transfer-erc20-tokens"></a>

You can send ERC20 tokens using the `transfer` method from any ERC20 contract. For interacting with the contract we will create an ethers [`Contract`](https://docs.ethers.io/v5/api/contract/contract/#Contract--creating) instance, and therefore an ethers [`Web3Provider`](https://docs.ethers.io/v5/api/providers/other/#Web3Provider). In the background the [ethers](https://www.npmjs.com/package/ethers) package will call [`eth_sendTransaction`](https://docs.constellationnetwork.io/stargazer-wallet/ethereum-rpc-api/eth_sendtransaction) for us.

```typescript
import * as ethers from "ethers";

const ethersProvider = new ethers.providers.Web3Provider(ethProvider);

const signer = ethersProvider.getSigner();

const StargazerSampleTokenAddress =
  "0xfe9885baff18074846aaa2d5541581adf068731d";
const StargazerSampleTokenABI = [...[]]; // You can get StargazerSampleToken's ABI from https://sepolia.etherscan.io/address/0xfe9885baff18074846aaa2d5541581adf068731d#code;

const contract = new ethers.Contract(
  StargazerSampleTokenAddress,
  StargazerSampleTokenABI,
  signer
);

const receiverAddress = "0x....";
const receiveValue = ethers.utils.parseUnits("10", 18).toHexString(); // 10 SST

// We send a transaction to the network
const trxResponse = await contract.transfer(receiverAddress, receiveValue);

// We wait for confirmation
const trxReceipt = await library.waitForTransaction(trxResponse.hash);

console.log(trxReceipt.blockNumber);
// 12415408
```

#### Approve ERC20 token Spend[​](https://docs.constellationnetwork.io/stargazer/Guide/sendingRPCRequests#approve-erc20-token-spend) <a href="#approve-erc20-token-spend" id="approve-erc20-token-spend"></a>

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 [`Contract`](https://docs.ethers.io/v5/api/contract/contract/#Contract--creating) instance, and therefore an ethers [`Web3Provider`](https://docs.ethers.io/v5/api/providers/other/#Web3Provider). In the background the [ethers](https://www.npmjs.com/package/ethers) package will call [`eth_sendTransaction`](https://docs.constellationnetwork.io/stargazer/APIReference/ethereumRPCAPI/eth_sendTransaction) for us.

TypeScript

```typescript
import * as ethers from "ethers";

const ethersProvider = new ethers.providers.Web3Provider(ethProvider);

const signer = ethersProvider.getSigner();

const StargazerSampleTokenAddress =
  "0xfe9885baff18074846aaa2d5541581adf068731d";
const StargazerSampleTokenABI = [...[]]; // You can get StargazerSampleToken's ABI from https://sepolia.etherscan.io/address/0xfe9885baff18074846aaa2d5541581adf068731d#code;

const contract = new ethers.Contract(
  StargazerSampleTokenAddress,
  StargazerSampleTokenABI,
  signer
);

const spenderAddress = "0x....";
const spendValue = ethers.utils.parseUnits("10", 18).toHexString(); // 10 SST

// We send a transaction to the network
const trxResponse = await contract.approve(spenderAddress, spendValue);

// We wait for confirmation
const trxReceipt = await library.waitForTransaction(trxResponse.hash);

console.log(trxReceipt.blockNumber);
// 12415408
```

#### Send ETH[​](https://docs.constellationnetwork.io/stargazer/Guide/sendingRPCRequests#send-eth) <a href="#send-eth" id="send-eth"></a>

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 [`Web3Provider`](https://docs.ethers.io/v5/api/providers/other/#Web3Provider) and an ethers [`Signer`](https://docs.ethers.io/v5/api/signer/#Signer). In the background the [ethers](https://www.npmjs.com/package/ethers) package will call [`eth_sendTransaction`](https://docs.constellationnetwork.io/stargazer-wallet/ethereum-rpc-api/eth_sendtransaction) for us.

TypeScript

```typescript
import * as ethers from "ethers";

const ethersProvider = new ethers.providers.Web3Provider(ethProvider);

const oneGwei = ethers.BigNumber.from(1 * 1e9).toHexString();

const signer = ethersProvider.getSigner();

// We send a transaction to the network
const trxResponse = await signer.sendTransaction({
  to: "0x....",
  value: oneGwei,
});

// We wait for confirmation
const trxReceipt = await library.waitForTransaction(trxResponse.hash);

console.log(trxReceipt.blockNumber);
// 12415408
```
