> For the complete documentation index, see [llms.txt](https://be-technology.gitbook.io/web3library-for-unity/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://be-technology.gitbook.io/web3library-for-unity/how-to-use/smartcontract.md).

# Smartcontract

### Generate Class

To generate class you have a menu on unity editor.\
Web3 -> Generate Contracts Classes

![](/files/6XNtvnrbIp7sv0t4a4rc)

Give a name to the generated class and paste your abi json, and save.

<img src="/files/b9uMb0l7Fy2JBI7IynOn" alt="" data-size="original">

Once you have do that, you will find 2 files in your folder Scripts/Contracts/NameOfYourContract.cs

One is for class definition, other for request the contract.

### Interact with a smartcontract

For the exemple I will use a classic ERC20 token.

You can use the class service generate with the contract&#x20;

#### Call

Exemple for request balance of an address with contract service generated

<pre><code>// Pass the address of the contract as Argument of the constructor
var smartcontract = new TokenContractService("0x61A154Ef11d64309348CAA98FB75Bd82e58c9F89");
<strong>// Call BalanceOfQueryAsync and pass the address of the account you want to know the balance
</strong><strong>System.Numerics.BigInteger result = await smartcontract.BalanceOfQueryAsync("0xDBf0DC3b7921E9Ef897031db1DAe239B4E45Af5f");
</strong>Debug.Log("balance 0xDBf0DC3b7921E9Ef897031db1DAe239B4E45Af5f " + result);
</code></pre>

The name of the function start with the name of the function present in the abi json.

You can have more information on [Nethereum docs](https://docs.nethereum.com/en/latest/nethereum-smartcontrats-gettingstarted/)

#### Send

Exemple for transfer token

```
var smartcontract = new TokenContractService("0x61A154Ef11d64309348CAA98FB75Bd82e58c9F89");
// In case or you want just get the transaction hash, call this method
// pass at first argurment the recipient and as second argument the amount 
string transactionHash = await smartcontract.TransferRequestAsync("0x0b33fA091642107E3a63446947828AdaA188E276", 1000);
// if you want have the result of the transaction call this method
TransactionReceipt result = await smartcontract.TransferRequestAndWaitForReceiptAsync("0x0b33fA091642107E3a63446947828AdaA188E276", 1000);
if (result.Succeeded())
{
// you can extract the event from the TransactionReceipt
// these methods are present in Web3Unity namespace
    TransferEventDTO transferEvent = result.GetEvent<TransferEventDTO>();
// If you need to extract multiple transfer event use GetEventList
    List<TransferEventDTO> transferEventList = result.GetEventList<TransferEventDTO>();
}
```

### Troubleshooting

If you have an error when you try to call a generic method, like the methods in service contract.\
The solution is to add or replace --generic-virtual-method-iterations in /ProjectSettings/ProjectSettings.assets\
Put 50 in values.

```
additionalIl2CppArgs: --generic-virtual-method-iterations=50
```

More info on [Unity docs](https://docs.unity3d.com/Manual/handling-IL2CPP-additional-args.html).
