Deploying Your First Arweave Contract
Tags: blockchain, programming, tutorial
Published: 2021-10-30 13:41:40

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19t5ksy3q8tpjo5mti2w.png)
 
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](https://dev.to/apollotoday/getting-started-with-arweave-2e9k). 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](https://arweave.medium.com/introducing-smartweave-building-smart-contracts-with-arweave-1fc85cb3b632)

>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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/053b1rl2an9e5owlujlu.png)
 
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'`.
```js
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; 
}
```
You'll notice we have a bunch of variables passed in through the state variables. These are the variables defined in your json. 
```json
{
  "twitter": false,
  "arWallet": false,
  "arVerify": false,
  "mintNft": false
}
```
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](https://faucet.arweave.net/). 

![Wallet grab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0kfcac2we8i71l8zjhq.gif)
 
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.

```sh
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]
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qkmhqvgfg74a9lppgta.gif)
 

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/](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`