> For the complete documentation index, see [llms.txt](https://docs.meycoin.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.meycoin.com/platform/smart-contracts/hello-world.md).

# Hello World

This is the most basic lua smart contract to store and retrieve states in Meey. You can save a name on the blockchain with the contract call function. And you can print ‘hello …’ with the query function.

```
-- Define global state variables
state.var {
  Name = state.value(),
  My_map = state.map()
}

-- Initialize the state variables
function constructor()
  -- a constructor is called only once at the contract deployment
  Name:set("world")
  My_map["key"] = "value"
end

-- Update the name
-- @call
-- @param name          string: new name
function set_name(name)
  Name:set(name)
end

-- Say hello
-- @query
-- @return              string: 'hello ' + name
function hello()
  return "hello " .. Name:get()
end

-- register functions to expose
abi.register(set_name, hello)
```

This is explained based on using cli. Variables used in this example are

* Account to deploy and execute a contract: AmPPCH5aMeHnaBPJ782UynpdFa612d8runz2zY83P4AcPHqTrhqL
* Cli commands in this page need a meeysvr with enable personal feature

### Check Account and Balance

First, you need an account with enough balance to deploy and execute smart contracts. (If you don’t) Import or Unlock Account to meey server.

### Compile Contract

Copy above code and save it to a file (e.g. helloword.lua). And Compile using the `meeyluac` compiler

```
./bin/meeyluac --payload lua_file_location.lua
```

```
./bin/meeycli contract deploy AmPPCH5aMeHnaBPJ782UynpdFa612d8runz2zY83P4AcPHqTrhqL --payload 37mGLDoCPNDQw7HbCG5WPfcM3E3cLhqhgE2V2UJK
```

### Get receipt of contract

Look up the actual contract address with the transaction ID above.

```
./bin/meeycli receipt get DyJg7jkw7AUT9ZNWyBUhwkR56V2E2HhZMGRSexamsUcJ
{
 "BlockNo": 1745,
 "BlockHash": "9NifJDJSTU9ibXsabRbr1VNxs2YeU6gC6ZMeNbAmCw8Z",
 "contractAddress": "AmhTVMdngSCs3xJ5XLAZHm8MGfAdnsNEvWvtpBhRnVky4fbnLZFB",
 "status": "CREATED",
 "ret": {},
 "txHash": "DyJg7jkw7AUT9ZNWyBUhwkR56V2E2HhZMGRSexamsUcJ",
 "txIndex": 0,
 "from": "AmPPCH5aMeHnaBPJ782UynpdFa612d8runz2zY83P4AcPHqTrhqL",
 "to": "",
 "feeUsed": 4080000000000000,
 "gasUsed": 0,
 "feeDelegation": false,
 "events": []
}
```

If the status is not ‘CREATED’, it may not be included in the block yet, or there may be an error. Wait a while until the transaction is included in the block. Or check the server’s error log.

### Get ABI of contract

Look up ABI of contract with the contract address above.

```
./bin/meeycli contract abi AmhTVMdngSCs3xJ5XLAZHm8MGfAdnsNEvWvtpBhRnVky4fbnLZFB
{
 "version": "0.2",
 "language": "lua",
 "functions": [
  {
   "name": "hello"
  },
  {
   "name": "set_name",
   "arguments": [
    {
     "name": "name"
    }
   ]
  },
  {
   "name": "constructor"
  }
 ],
 "state_variables": [
  {
   "name": "Name",
   "type": "value"
  },
  {
   "name": "My_map",
   "type": "map"
  }
 ]
}
```

### Query Initial State

You can query the generated contract in the following way.

```
./bin/meeycli contract query AmhTVMdngSCs3xJ5XLAZHm8MGfAdnsNEvWvtpBhRnVky4fbnLZFB hello
```

You can see that the name ‘world’ assigned by the constructor is output.

### Call Contract

You can change the name recorded in the block chain as follows:

```
./bin/meeycli contract call AmPPCH5aMeHnaBPJ782UynpdFa612d8runz2zY83P4AcPHqTrhqL AmhTVMdngSCs3xJ5XLAZHm8MGfAdnsNEvWvtpBhRnVky4fbnLZFB set_name '["meeychain"]'
```

### Query Changed State

If you look at the results again, it has changed.

```
./bin/meeycli contract query AmhTVMdngSCs3xJ5XLAZHm8MGfAdnsNEvWvtpBhRnVky4fbnLZFB hello
```

### Query contract variable with merkle proof

#### Value

```
./bin/meeycli contract statequery AmhTVMdngSCs3xJ5XLAZHm8MGfAdnsNEvWvtpBhRnVky4fbnLZFB Name --compressed
```

#### Map

```
./bin/meeycli contract statequery AmhTVMdngSCs3xJ5XLAZHm8MGfAdnsNEvWvtpBhRnVky4fbnLZFB My_map key --compressed
```

#### Array

```
./bin/meeycli contract statequery AmhTVMdngSCs3xJ5XLAZHm8MGfAdnsNEvWvtpBhRnVky4fbnLZFB array_name array_index --compressed
```

By default, the returned state is the one at the latest block, but you may specify any past block’s state root.

```
./bin/meeycli contract statequery AmhTVMdngSCs3xJ5XLAZHm8MGfAdnsNEvWvtpBhRnVky4fbnLZFB var_name --root "9NBSjkcNTdE5ciBxfb52RmsVW7vgX5voRsv6KcosiNjE"
```
