Execute Contracts

Explains how to execute smart contract functions.

In the chapter before, we've described how to easily call read-only functions and fetch the results (read state from the contract).

Here, we'll show how you can use the SDK to request your users to execute any state changing function on the smart contract.

The flow you will use is as follows:

  1. Create transaction request on a contract instance

  2. Redirect the user to the link generated for this concrete transaction request

  3. Wait for the user to complete the action and read result

As you can see, there's no complex wallet management or transaction status management included here. You just have to describe to the SDK what you'd like the user to execute, and we take care of the rest.

Before creating the transaction request, you must first obtain the alias of your contract by checking the Dev3 Dashboard.

Create the connection to the contract by initializing the SDK object first as explained in the Initialization section, and then providing the alias found on the dashboard (alias shown in the example above).

Once the connection to the smart contract has been obtained, simply create the transaction request by providing the function name and parameters as defined by the smart contract implementation. You can read the function name and what parameters the function accepts directly from the dashboard, on the contract details page, where you found the contract alias.

In the example above, we expanded the details of 'Mint' function, which is a state changing (write) function on the opened smart contract interface. The function name is highlighted on the right part of the image, and we can actually copy/paste it directly from the dashboard. The function parameters this function accepts are described on the left part, and we should provide the value for these in the SDK execution call. The code snippet to execute such an action will look like something below:

// sdk object initialized as explained in the Initialization section

const contractInstance = sdk.getContractByAlias('<alias-from-the-dashboard>');

const functionName = '<contract-function-name>'; // copy/paste from the dashboard
const functionParams = [
    // function parameter values
];
const options = { };

// 1) build action
const action = await contractInstance.buildAction(
      functionName,
      functionParams,
      options
);

// 2) redirect user
window.open(action.actionUrl, "_blank"); // redirects the user to the new tab

// 3) wait for result
const result = await action.awaitResult();

// 4) Voila! You're done.
if (result.status === RequestStatus.SUCCESS) { 
      // your platform logic
      console.log("transaction hash", result.transactionHash);
}

To see all the possibilities when calling more complex functions accepting different function parameters, please refer to the Function Parameters docs. (previous chapter)

Options object provided when building the action can contain the following:

{
    ethAmount?: string;
    arbitraryData?: Map<string, object>;
    screenConfig?: ScreenConfig;
    callerAddress?: string;
    redirectUrl?: string;
}

All the fields are optional and can all be skipped by passing an empty object as the configuration. However, if you wish to use some (or all) of these, more detail is given in the options section.

Live Example

Run this flow on a live coding environment here!

If you're getting the "You don't have enough gas to execute this transaction..." error, then please refer to our FAQ to resolve this issue in no time!

Last updated