> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sei.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Example Usage

> Learn how to interact with Sei precompiles using ethers.js to query and execute CosmWasm contracts from the EVM.

<Info>
  Per [Proposal 115](https://www.mintscan.io/sei/proposals/115), no new CosmWasm contracts can be deployed on Sei. The examples below apply to **pre-existing CosmWasm contracts** only — `instantiate()` will revert. Separately, [Proposal 116](https://www.mintscan.io/sei/proposals/116) disables inbound IBC transfers as part of the [SIP-03](/learn/sip-03-migration) migration.
</Info>

The Sei precompiles can be used like any standard smart contract on the EVM. For
example, using [ethers.js](https://docs.ethers.org/v6/), you can query and
execute actions on an existing CosmWasm contract as follows:

## Setup

To install `ethers`, run the following command in your project directory
terminal:

```bash theme={"dark"}
npm install ethers
npm install @sei-js/evm
```

Next, you'll need to use one of the precompiles in `EVM Precompiles` section. In
this example, we're going to be using the [wasmd precompile](/evm/precompiles/cosmwasm-precompiles/cosmwasm):

```typescript theme={"dark"}
// Import Wasm precompile address and ABI
// View the entire ABI here: https://github.com/sei-protocol/sei-chain/tree/evm/precompiles/wasmd
import { WASM_PRECOMPILE_ABI, WASM_PRECOMPILE_ADDRESS } from '@sei-js/evm';
```

## Using the contract

Next, we'll set up a provider and contract to interact with the blockchain:

```typescript theme={"dark"}
import { ethers, toUtf8Bytes, toUtf8String } from 'ethers';

// Using MetaMask as the signer and provider
const provider = new ethers.BrowserProvider(window.ethereum);
const signer = await provider.getSigner();

// Create a contract with the signer
const contract = new ethers.Contract(WASM_PRECOMPILE_ADDRESS, WASM_PRECOMPILE_ABI, signer);
```

#### Querying & Executing a CosmWasm Contract

Once you have the contract, you can query and execute messages to any CosmWasm
smart contract. In the below example we assume there is a counter contract with a `get_count` function

```typescript theme={"dark"}
// Replace with your contract
const COUNTER_CONTRACT_ADDRESS = 'sei1eyf...xff';

// Query to get the count on the counter contract (replace with function from your ABI)
const queryMsg = { get_count: {} };
const queryResponse = await contract.query(COUNTER_CONTRACT_ADDRESS, toUtf8Bytes(JSON.stringify(queryMsg)));
console.log(toUtf8String(queryResponse));

// Execute message to increment the count on the contract. (Replace with any function from your ABI)
// No funds are attached since the increment method does not require sei. (Your ABI may require funds)
const executeMsg = { increment: {} };
const executeResponse = await contract.execute(
  COUNTER_CONTRACT_ADDRESS,
  toUtf8Bytes(JSON.stringify(executeMsg)),
  toUtf8Bytes(JSON.stringify([])) // Used for sending funds if needed
);

// Wait for the transaction to be confirmed
await executeResponse.wait();
console.log(executeResponse);
```

### Executing a payable function

In this example, we execute the 'donate' method on our contract. This is similar
to the `increment` method, but also receives funds from the user and stores it
in the contract.

```typescript theme={"dark"}
// Execute a message to donate to the contract.
const executeMsg = { donate: {} };

// Funds are attached via overrides. This example is specific to ethers.js
const overrides = {
  value: ethers.parseEther('3.2') // Sending 3.2 sei
};
const executeResponse = await contract.execute(
  COUNTER_CONTRACT_ADDRESS,
  toUtf8Bytes(JSON.stringify(executeMsg)),
  toUtf8Bytes(JSON.stringify([{ denom: 'uusdc', amount: '100' }])), // Also send 100 uusdc
  overrides
);

await executeResponse.wait();
const receipt = await provider.getTransactionReceipt(executeResponse.hash);
```

For payable contracts, Sei amounts have to be sent directly to the contract
while other denoms should use the `coins` field.
