Expanding the capabilities of smart contract automation by Avivi
24 August 2022
next articleAmong the many advantages of smart contracts, self-execution is perhaps the most important. The ability to perform regardless of the wishes of the parties to the contract is a guarantee of justice, for the achievement of which smart contracts were created. However, many users misunderstand the concept of self-execution and confuse it with automation, the technology that allows a device or solution to work independently. This ability is not built into smart contracts in advance, but requires individual development for each individual case. Let's figure out how everything is arranged and what can be done.
Problem of the lunch
We know that the smart contract is executed in the blockchain network and really consistently performs all the actions embedded in its code by the author. This is what is called self-fulfillment. However, the question of who or what initiates the execution of a ready-made smart contract or its individual stages that require external confirmation remains open. Regarding obtaining information from the external environment outside the blockchain, we recommend our previous blog article "Hybrid Smart Contract and the Role of Oracles". But what about the launch itself?
Currently, the most advanced in this matter are DeFi protocols, that is, a decentralized financial asset management system. This is impossible without automatic processes, which is why third-party Keepers are widely used — automated functions that allow interaction with smart contracts under certain conditions. Most often, in the financial environment, automatic triggers are used, when the smart contract is launched only when specific conditions are met, and monitoring of status and events, when you need to get current information about the progress of the smart contract. However, these automated actions have certain drawbacks.
Methods and fees
Let's imagine that we need to implement a smart contract with a function that will automatically repeat every hour. It is simply impossible to achieve this in Solidity using the standard functionality! The maximum that can be done is to specify an hourly interval between operations in the smart contract using the require () operator:
function runMe() public { require(block.timestamp >= lastTriggeredAt + 1 hour); ... }
However, this is not automation, but postponement: this method ensures that at least one hour will pass between operations, but does not create conditions for constant repetition. Therefore, to achieve more specific results, modifiers of standard functions are used. In this case, the function is wrapped in additional conditions (decorated) and thus achieves the set goal.
However, there is a big problem here: the additional logic is not free and requires gas consumption not execution. The system reacts quite simply: whoever calls the modified functions pays for the gas. But it should be remembered that each function has its own cost. Therefore, their layering leads to uncontrolled use of valuable gas, which makes automation of this type economically justified. In addition, decorated functions also require their own unique conditions to run, which reduces their performance. Therefore, it becomes clear that good automation needs to be done in a different way.
The solution is in serverless functions
As you can see, in order to optimally achieve the set tasks, we do not need to deploy something additionally and manage something in the process. Avivi specialists have mastered methods of working with serverless functions that perfectly fit our needs. The main thing that is necessary for this is the transition to a serverless platform and, if necessary, use ready-made solutions "out of the box". There are already quite a few if you're a GitHub user.
The functionality of serverless functions is practically unlimited and comes down to the capabilities of JavaScript functions. With its help, you can create configuration files in which both new functions and events of your service can be stored at the same time. Here is an example:
service: serverless-ethers provider: name: aws runtime: nodejs12.x environment: CHAIN_ID: 3 DEFAULT_GAS_PRICE: 60000000000 functions: AviviFunc: handler: functions/myFunc.handler events: - schedule: rate(1 hour)
It is clear from the code that we have our own function called AviviFunc, a handler property that contains the path to the file with the code we want to execute and an events trigger that describes the conditions for the function to run. That is, we see that now we have both the cause of the activation of the action and the consequence. And this is the basis of any automation.
Of course, this is just the foundation of the basics of all serverless automated processes. But you are convinced that it is absolutely possible to implement a complex multi-level code in an off-chain environment that will interact with your smart contract for a much lower gas fee. Therefore, if you are looking for real automation for the prosperity of your business - contact Aviva and we will definitely help you with any ideas.
We will reach out to within 10 minutes