# Custom Contract Interaction

{% hint style="success" %}
Before we continue, please ensure that you have had a look at our [Supported Ethereum Chains](broken://pages/-MeAyzR2e2VKsOneDqAi), followed the steps in [Install Etherspot SDK](/getting-started/install-sdk.md) and how to [Bootstrap Etherspot SDK](/getting-started/bootstrap-etherspot-sdk.md). We're assuming that you have completed these steps before going forward.
{% endhint %}

In this article, we are going to generically explain how to call a contract method using Etherspot SDK. For better Understanding, we will consider stake method to be called from a staking contract.&#x20;

## Before we continue...

Take the contract address and its abi which is available on the respective chain's block explorer and initialise the sdk on the chain where the contract address was found. For this demostration we will consider Klima DAO as an example which is found in Matic Chain.

```javascript
import {
  Sdk as EtherspotSdk,
  NetworkNames,
  randomPrivateKey,
} from 'etherspot';

// ...

// First, define the contract address and abi
const contractAddress = '0x4D70a031Fc76DA6a9bC0C922101A05FA95c3A227';

// For the sake of example, we are only defining the method we are going to execute
const contractAbi = [
  "function stake(uint256 value)",
]; 

// Next, instantiate our SDK on Matic as the contract address is on the same chain
const etherspotSdk = new EtherspotSdk({
  privateKey: randomPrivateKey(),
}, {
  networkName: NetworkNames.Matic,
});

// Finally, compute the contract account addresses ahead of time
// The responses will return the account details.
const maticAccount = await etherspotSdk.computeContractAccount();
```

## Create Contract Interface using Etherspot SDK

Next, we are going to call registerContract function on the Etherspot SDK to register the contract details and encode the parameters required for the contract method. You can find the required parameters inside the abi on the name of the function.

{% code overflow="wrap" %}

```javascript
const StakingContract = sdk.registerContract<{ encodeStake: (amount: BigNumberish) => TransactionRequest }>('stakingContract', contractAbi, contractAddress); // amount type is defined based on the constract function parameter type
const stakeTransactionRequest = stakingContract.encodeStake(AmountToBeStakedInWei);
```

{% endcode %}

After we get the transaction details (stakeTransactionRequest), the data is passed into Etherspot SDK in order to execute the transaction.

## Adding your transaction to a batch

When using Etherspot to send transactions, we first add the transaction to a "batch". A batch can contain many transactions for a more gas-efficient operation, but in this example - we're just going to add one transaction to the batch. It will behave as if we are just sending a single transaction.

```typescript
await etherspotSdk.batchExecuteAccountTransaction({
  to: stakeTransactionRequest.to,
  data: stakeTransactionRequest.data,
  value: stakeTransactionRequest.value,
}).catch(console.error);
```

To see the full SDK reference for the `batchExecuteAccountTransaction`, click [here](https://sdk.etherspot.dev/classes/executeaccounttransactiondto.html).

## Estimating your batch

At this point, your batch is ready to have the gas cost estimated. This gives you, or your users, the opportunity to see how much this transaction may cost on the chain that you have instantiated the SDK on. You can read more about instantiating the Etherspot SDK on different chains here:[Supported Ethereum Chains](broken://pages/-MeAyzR2e2VKsOneDqAi).&#x20;

```typescript
const estimationResponse = await etherspotSdk
  .estimateGatewayBatch()
  .catch(console.error);
  
console.log('Gas estimated at:', estimationResponse);
```

If you're happy with the cost, proceed to the next and final step.

## Submitting your batch

The final step is to submit your batch, containing your one or more transactions, to the Etherspot gateway. The Etherspot gateway will queue and manage your batch, and endeavour to do everything it can to get your transaction onto your chosen blockchain.

```typescript
const submissionResponse = await sdk
  .submitGatewayBatch()
  .catch(console.error);
```

If you had previously set up a notification subscription [here](/use-cases/transactions.md#optional-setting-up-a-notifications-subscription), then this will fire with different events as your batch is queued, processed and eventually sent to the blockchain.

## :tada: Finished!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://v1.etherspot.io/use-cases/custom-contract-interaction.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
