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");constsinqlarity=newTriweb("YOUR_PROJECT_ID","YOUR_KEY","YOUR_SECRET");// Generate an authorization token for the frontend.constsinqlarityAuthToken=awaitsinqlarity.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.constsinqlarityClient=newSinqlarityClient(sinqlarityAuthToken);// Define the data you want to pass to the smart contract function.constdata= {// Your function's parameters go here.// For example, if your function takes a 'value' parameter: value:100,};// Execute the smart contract function without MetaMask.asyncfunctionexecuteFunction() {try {// Use sinqlarityClient.execute to call your smart contract function.constresponse=awaitsinqlarityClient.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.