DEV Community

Cover image for Deploying Your First Arweave Contract
Nick
Nick

Posted on

Deploying Your First Arweave Contract

At a high level, smart contracts make sense to a lot of people. They are programs that live on chain, that are deterministic in nature, and are based in that idea code is law. I first learned about smart contracts back in 2017 with ethereum first gaining traction. Instantly my mind wandered into ideas of these interweaving contracts that mitigate the need for lawyers. I think the most important thing to remember about smart contracts is that they are supposed to remove a human element, which all programs typically try to do, but do it in a verifiable and reproducible way... All on the ✨ Blockchain ✨

Arweave

Arweave released their version of smart contracts back in June 2020. Smartweave, or a new smart contract protocol, enabling computation-heavy dapps on top of the Arweave network, allowed devs to write some simple javascript and easily deploy it to the chain. If this is your first time learning about Arweave, I recommend checking out my introduction. If you're familiar with the ecosystem, you will notice that Smartweave is built upon the foundation of the blockweave. Unlike Eth v1 implementation of smart contracts, where each node has to validate the outcome - smartweave deploys a lazy evaluation method. Where Arweave's "network is utilized as a generic data consensus and sharing layer". Basically, the user broadcasts the outcome of their smart contract and the network validates. Learn more here

In a later post, I will dive deeper into the background of smartweave and its technical implementation.

First, let's go ahead and create a contract.js and a contract.json in the folder of your choice. Smartweave uses javascript through the client's unmodified execution engine. What I have found the biggest mental switch is that you don't define variables in the .js file. You define the name and initial state of each relevant variable in .json.

Picture of code

The first bit of code we are going to write is our javascript. Something important to remember is that contracts must be deterministic. I feel like deterministic is one of those signal words CS major use to beckon each other. All it means is that the function will produce the same output from a given starting input. You should never do a network call or produce a random number inside of a smartweave contract. Lastly, we can arbitrarily (up to a certain number of bites) input any data when we call our contract. We pass in these inputs via action.input.function === 'someString'.

export function handle(state, action){
if (action.input.function === 'Twitter') {
    state.twitter = true;
  }
  if (action.input.function === 'ArWallet') {
    state.arWallet = true;
  }

  if (action.input.function === 'arVerify') {
    state.arVerify = true
  }
  if (state.twitter && state.arVerify && wallet) {
    state.mintNft = true;
  }
  return mintNFt; 
}
Enter fullscreen mode Exit fullscreen mode

You'll notice we have a bunch of variables passed in through the state variables. These are the variables defined in your json.

{
  "twitter": false,
  "arWallet": false,
  "arVerify": false,
  "mintNft": false
}
Enter fullscreen mode Exit fullscreen mode

This contract was created while I was working on another Arweave project where if you complete a certain number of steps you will receive a free NFT. More on that at a later date. This contract will get called when a user connects their wallet. At that time I will pass in arWallet: true. This contract will continue to return false until all three variables are set to true. In the next article when we interact with our contract, you'll see how we can retrieve state without an input. What's cool is that contracts can read the state of other contracts.

Let's go ahead and check if we have a key-file representing our Arweave wallet. While you don't need gas to interact with a contract you do to deploy, although it's nominal. Luckily you can get a dollar or two of AR through the faucet.

Wallet grab

I'll be honest, when I was getting ready to deploy my first the README.md was confusing me. It wasn't until I ran smartweave create -h in my terminal didn't I really understand what I needed to pass in. To deploy our contract you need to have Node install and run npm install -g smartweave. This globally installs the smartweave package so we can call it anywhere. Next, navigate to the folder your contracts are in and remember where your key file is.

smartweave create -h
  ____                       _ __        __
 / ___| _ __ ___   __ _ _ __| |\ \      / ___  __ ___   _____
 \___ \| '_ ` _ \ / _` | '__| __\ \ /\ / / _ \/ _` \ \ / / _ \
  ___) | | | | | | (_| | |  | |_ \ V  V |  __| (_| |\ V |  __/
 |____/|_| |_| |_|\__,_|_|   \__| \_/\_/ \___|\__,_| \_/ \___|

smartweave create <contractSource> <initStateFile>

Creates a new contract from a source file or existing contract source already
on-chain.

Positionals:
  contractSource  The contract source. A path to a .js file, or transaction id
                                                                      [required]
  initStateFile   The initial state of the contract. Path to a .json file
                                                                      [required]

Options:
  --version   Show version number                                      [boolean]
  --help      Show help                                                [boolean]
  --key-file  Your key file                                           [required]
Enter fullscreen mode Exit fullscreen mode

Looking at the -h you see the create call is really quite simple. Just pass in the path to your js and json file in that order. Last declare your key pair with --key-file. When you click enter the network will first calculate your tx fee. After this is calculated it will ask you to enter a random adjective it generated.

CLI Upload

The contract will get pushed to the network pretty quickly. However, it will take a while for your transaction (tx) to be mined. Tx are prioritized by their fee amount. I use https://arweave.app/ to check my pending transactions.

In the next smartweave tutorial we will look at our wallet transaction history, interact with our wallet using the command line, and through a web application.

If you found this helpful (AR Wallet):
WtrURL9vjAUQQXMQxY5c9FZnh_IKOog5P38KO7oZnbg

Latest comments (0)