5. Interaction
Contents
Intro
Ideally, interacting with your protected smart contract would be in conjunction with the frontend and backend - the user presses a button (i.e. does a transaction to your protected smart contract) which initiates a backend call, the frontend receives payload from it and proceeds to submit it to the blockchain.
But for the sake of simplicity, let's just create a simple nodejs application that executes a transaction on our protected function.
1. Creating cube3_transaction_executer
nodejs application
cube3_transaction_executer
nodejs application Let's start by creating a nodejs project using typescript.
create src/index.ts
file where we will write our implementation. Initially, add this inside:
We will need to access your protected contracts ABI
. So paste the file containing ABI
it into cube3_transaction_executer/src
.
2. Installing necessary packages
Install CUBE3 packages:
For blockchain interactions, we will use ethers.js
:
3. Setup contract object
First, import all necessary functions, Cube3ProtectedErc20
and setup constants:
To generate a payload, you need to make an HTTP request to the CUBE3 validation service. For that, we will use CUBE3 sdk. Let's begin.
For CUBE3 to generate a payload, it needs transaction data. For this, we will need to create a transaction object and pass it to CUBE3 sdk.
The code below goes into generatePayload()
function.
For any interaction with a smart contract, we need to create a Wallet and Contract objects.
At this point, we have the ability to call our protected contract. Let's move to payload generation.
4. Payload generation
These are the steps:
Before we send an HTTP request to the CUBE3 validation endpoint, we need to generate transaction data without actually executing the transaction. For this, we are going to utilize ethers populateTransaction to create the transaction object which stores information about the transaction.
Pass it into
constructCube3Validation
function which will extract the necessary data for payload generation.Send an HTTP request to the CUBE3 validation endpoint using
cube3ValidateTransaction
function, which will return us whether this transaction is safe and the payload along with other information (to learn more about what the validation service returns, read more here). If CUBE3 finds that transaction to be unsafe/malicious, the payload will not be provided and the transaction will not be possible.
5. Interacting with your protected functions
In the previous step, we generated the payload for our protected mintCube3Protected
function. Now all that is left is to execute the transaction on the blockchain by passing the payload into the function call.
From the project root directory, in cmd run:
And the transaction receipt will appear. Copy the transaction hash and paste it into etherscan (for example sepolia) https://sepolia.etherscan.io/tx/<your-hash> on which you should see a successful transaction, for example:
Lastly, you can analyze any protected transaction on the Panorama dashboard:
To learn more about the transaction dashboard, read here.
Last updated