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.
However, 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 will walk you through, step-by-step 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 founder on GitHub here. 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
Navigate to 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)
Setting Up Your AWS Serverless App
Now that you have your Serverless Framework ready, you are ready to create your first app. Before you enter the
serverless command in your terminal, be sure you have install the serverless-offline plugin by running the
npm install 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 and 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 App
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 MNEMONIC and 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
serverless deploy. For further assistance, navigate to the serverless docs.
About Buns Enchantress
0xBuns is the operated by Web3 developer Buns Enchantress, who is an experienced full-stack developer and Cofounder of SoulSwap Finance and Luxor Money.