HOW TO IMPLEMENT FUNCTIONS

Let us assume that we want to execute a function and that we're interacting with a smart contract called MyContract on the Polygon network, and you want to call a function called myFunction.

Please note that the code snippets below are simplified for demonstration purposes, and you should adapt them to your specific smart contract and use case.

Backend Integration:

⚠️ Warning: Make sure to keep your API key and secret private.

// Import the SinQlarity library and initialize it with your credentials.
const { Sinqlrity } =  require("sinqlarity");
const sinqlarity = new Triweb("YOUR_PROJECT_ID", "YOUR_KEY", "YOUR_SECRET");

// Generate an authorization token for the frontend.
const sinqlarityAuthToken = await sinqlarity.getAuthenticationToken();

// Pass the `sinqlarityAuthenticationToken` to the frontend.

⚠️ Disclaimer: It is recommended to use back-end for optimal security.

Frontend Integration:

// In the frontend, import from triweb:
import { TriwebClient, NETWORKS, CONTRACTS } from "sinqlarity";

// Initialize the SinqlarityClient with the authentication token.
const sinqlarityClient = new SinqlarityClient(sinqlarityAuthToken);

// Define the data you want to pass to the smart contract function.
const data = {
  // Your function's parameters go here.
  // For example, if your function takes a 'value' parameter:
  value: 100,
};

// Execute the smart contract function without MetaMask.
async function executeFunction() {
  try {
    // Use sinqlarityClient.execute to call your smart contract function.
    const response = await sinqlarityClient.execute(
      NETWORKS.POLYGON, // Specify the blockchain network (e.g., Polygon).
      CONTRACTS.MY_CONTRACT.MY_FUNCTION, // The smart contract and function.
      data // Data to pass to the function.
    );

    // Handle the response from the smart contract function.
    console.log("Smart contract response:", response);
  } catch (error) {
    // Handle any errors that may occur during the execution.
    console.error("Error executing smart contract function:", error);
  }
}

// Call the executeFunction when needed in your application.

In this example, the sinqlarityClient.execute function allows you to call the MY_FUNCTION of the MY_CONTRACT on the Polygon network. The function takes the necessary data as an argument, and it uses the authorization token obtained from the backend to sign the transaction, effectively executing the smart contract function.

Please replace "YOUR_PROJECT_ID", "YOUR_KEY", "YOUR_SECRET", and adjust the contract and function names to match your specific use case.

Last updated