You cannot automate smart contracts… until now.
Surprisingly, smart contracts are not as intelligent as their name may imply.
They are not machine learning algorithms on the blockchain — adapting to market conditions and mutating into an automaton surpassing the genius of their creators. No. In fact, smart contracts rely on us to tell them what to do. No different from any other standard machine.
What do I mean when I say “us”?
I mean an external agent. An external agent can be me and you, but it can also be another machine that is capable of iterating and interacting with the contract in question.
Just like us, this external agent is even allowed to interact with said contract free of charge by conveniently making use of functions.
In this article, I provide a step-by-step guide describing how you may set up an automated task that executes smart contract methods on your behalf.
The end result? Something remarkably similar to automation on the blockchain.
In order to execute automated interactions with your smart contract, install the Serverless Framework CLI.
After you have installed the CLI, clone my repo containing sample materials found in my public GitHub repository for serverless deployments.
In order to make the magic happen, you will need to setup a (free or pro) account on the Serverless Framework. After setting up your account, navigate back to your terminal and execute the
serverless login command. Doing so will open your browser and direct you to your Providers page. Do not run the
serverless command quite yet, we will get there soon.
Setting Up Environment Variables
serverless.yml in the main directory and you will arrive at a view similar to the image below. Note that the specification are up to you.
So, for example, if you want to work in the Ropsten testnet, you may update the chain_id to 3, just be sure your contract also exists on the chain of your choice. We will be working in Rinkeby for this example.
This example uses the following environment variables:
DEFAULT_GAS_PRICE: Default gas price used when making write transactions.
MNEMONIC: 12-word mnemonic used to derive an Ethereum address, make sure it's funded with Ether if you intend to write data to Ethereum!
SLACK_HOOK_URL: The example sends messages to Slack using Incoming Webhooks. You can get this URL from your Slack dashboard. (Optional)
AWS Serverless Application Setup
Now that you have your Serverless Framework ready, you are ready to create your first application. Before you enter the
serverless command in your terminal, be sure you have install the serverless-offline plugin by running the
npm install ||
yarn command in your cloned repo.
You will be prompted with a few inquiries, which are self-explanatory. Yes, you want to enable. Name demo, since it’s your first app and we are using this one to test the out-of-the-box solution.
- If you are new to AWS, then you will be prompted to set up AWS credential on your machine, select yes, to proceed.
- If you want to setup a new AWS account, select yes. Doing so will navigate you back to your browser to setup an AWS account.
- Create new Root user.
- Enable the default selection for AWS Access: Programatic Access.
- Continue with all the defaults (for now).
- Your review page should look like the screenshot below.
- Be sure to download the CSV in the subsequent confirmation screen and store in a safe place.
Now that you have your login credentials, you may use this information to login to your account in your terminal.
You will need your
AWS Access Key ID and
Secret Access Key.
While your Access Key ID may be found on your online account in your browser, your secret key is stored in the CSV you downloaded when prompted to do so.
After logging in, you may choose yes for the remaining prompts, until you are prompted that you command line completion was successfully setup.
If you’ve made it this far, congrats! You are now able to serverless deploy your project, monitor, troubleshoot, and test it.
Testing and Deploying Your Automated Application
Before you proceed with testing locally, you will need to update your mnemonic, by creating your copy of the .env.example file.
Duplicate and create a new file with the name .env to ensure your information is not shared, ensure you include .env in your .gitignore file. This is where you may securely store your environment variables.
Once you have entered your
FROM_ADDRESS values in your.env file, you are ready to test locally, just be sure your
FROM_ADDRESS has sufficient ETH on the selected chain.
Congrats, you are now ready to test and deploy!
In order to test locally, execute the following command in your terminal:
serverless invoke local —-function exec.
In order to deploy execute
For further assistance, please navigate to the serverless docs for more information and the most up-to-date details.