# CallGenericWriteFunction

{% hint style="info" %}
This function triggers a modal for the user to confirm the transaction
{% endhint %}

{% hint style="danger" %}
This is an advanced feature and <mark style="color:red;">can drain user wallet funds</mark> if you interact with a malicious contract. Only link to trusted smart contract authors.
{% endhint %}

## Description

This allows you to call any smart contract function and process the response. This function should be used to query functions that require the users signature, hence it should be invoked only once the user is logged in.

## Details

### Signature

{% code overflow="wrap" %}

```csharp
Task CallGenericWriteFunction(dynamic args, System.Action<dynamic> callback = null)
```

{% endcode %}

### Arguments

<table><thead><tr><th width="258">Parameter</th><th width="114">Type</th><th>Definition</th></tr></thead><tbody><tr><td><code>callback?</code></td><td>Action&#x3C;dynamic></td><td><p>Function to callback upon getting result from the smart contract function call<br><br>For numerical return values, please refer to the <a href="#handling-numerical-values">section below</a>.<br><br>Structure of result object passed into callback function:</p><pre class="language-json" data-line-numbers><code class="lang-json"><strong>{
</strong><strong>    "status": 1/0, // success or fail
</strong><strong>    "error": "", // error string if any
</strong><strong>    "data": {any}, // data returned from the smart contract function call
</strong><strong>}
</strong></code></pre></td></tr><tr><td><code>args.contractAddress</code></td><td>String</td><td>Contract address of the smart contract to be called</td></tr><tr><td><code>args.functionABI</code></td><td>String</td><td>The path to the JSON object for the <a href="https://ethereum.org/en/developers/docs/smart-contracts/compiling/#web-applications">smart contract ABI</a>. You can store it in the Resources folder and pass in a path such as <code>@"Assets/Resources/Contracts/Contract.json"</code></td></tr><tr><td><code>args.functionName</code></td><td>String</td><td>Name of the function to be called as per the ABI.</td></tr><tr><td><code>args.value?</code></td><td>String</td><td>The value to be sent along with the smart contract invocation. Please refer to the <a href="#handling-numerical-values">section below</a> for more information.</td></tr><tr><td><code>args.chain</code></td><td>Metafi.Unity.Chain</td><td>Which chain to broadcast the transaction on. Pick a chain from the <a href="broken-reference">supported chains list</a></td></tr><tr><td><code>args.params</code></td><td>List&#x3C;object></td><td>Array of objects that will be sent as the arguments to the smart contract function. Please ensure that the order of params matches the contract ABI.<br><br>For large numerical inputs, please refer to the <a href="#handling-numerical-values">section below</a>.</td></tr></tbody></table>

### Return Value

None

### Handling Large Numerical Values

{% hint style="warning" %}
Applicable to EVM Chains only
{% endhint %}

While passing in large numerical values (values above **9007199254740991**) in `args.params`, a conversion to string format needs to be made. You can use the following methods that convert the number to the appropriate format:

```csharp
// For int values
string ConvertNumberToBigNumber(int value, int decimals);

// For double values
string ConvertNumberToBigNumber(double value, int decimals);

// For float values
string ConvertNumberToBigNumber(float value, int decimals);
```

## Example

{% code lineNumbers="true" %}

```csharp
using System;
using Metafi.Unity;

public class StakeTokenButton : MonoBehaviour {
    public async void StakeTokens(){
        await MetafiProvider.Instance.CallGenericWriteFunction(new {
            contractAddress = "contract-address",
            functionABIPath = @"Assets/Resources/Contracts/path-to-contract.json",
            functionName = "stakeTokens",
            value = Utils.ConvertNumberToBigNumber(0.1, 18),
            @params = new [] {"param1", "param2"},
            chain = Chains.MUMBAI,
        },
        ((System.Action<dynamic>) (result => {
            Debug.Log("CallGenericWriteFunction complete, result: " + result.ToString());
        })));
    }
}
```

{% endcode %}
