Neo Compiler Eco: Example 3 — Negation Contract (C#)
This series of articles will include simple examples for Neo blockchain coding using Neo Compiler Eco online tool - https://neocompiler.io
In this article we will build a Smart Contract that receives a boolean value and returns the negation of this value. Example: user submits True and contract responds False; user submits False and contract returns True.
** this example was inspired on a code suggested by github user devhawk
Step 1: Open neocompiler.io
Step 2: select C# language and write the following code
using Neo.SmartContract.Framework;
namespace NegatorContract
{
public class Contract1 : SmartContract
{
public static bool Main(bool value)
{
return !value;
}
}
}
Step 3: Click “Compile”
The following AVM Script will be generated: 51c56b6c766b00527ac46a00c3009c616c7566
* Note: newer compiler versions with latest optimization techniques may generate slightly different AVMs. For this example, let’s consider this.
This AVM is equivalent to the following ScriptHash: fd217ab3777ce71e349e878e8b50c1b92cc58404
Step 4 — Go to Deploy/Invoke tab and click “Deploy”
The expected feedback is ok from the network.
On Activity tab, the deploy transaction must have been recorded. The expected good result is: HALT, BREAK
Step 5— Go to Deploy/Invoke tab and click “Invoke”, after passing a integer value 1 (equivalent to value True)
On Activity, this Invoke must have been recorded too.
If you click on HALT,BREAK , you are redirected to Raw RPC screen, and the following result is expected:
[
{
“jsonrpc”: “2.0”,
“id”: 5,
“result”: {
“txid”: “0x59c07c53ed2335f5cc913fc803b68494ff406cace5fd3180e92e51b93b220d0e”,
“executions”: [
{
“trigger”: “Application”,
“contract”: “0x7b3d2001e772612a8f3421afd8d46bd0a5453e8a”,
“vmstate”: “HALT, BREAK”,
“gas_consumed”: “0.023”,
“stack”: [
{
“type”: “Boolean”,
“value”: false
}
],
“notifications”: []
}
]
}
}
]
The value: {“type”: “Boolean”, “value”: false} indicates that False is returned (ok, since parameter was True).
Step 6 — Do the same Invoke passing parameter False (integer 0)
After clicking HALT,BREAK again, the expected Raw RPC is:
[
{
“jsonrpc”: “2.0”,
“id”: 5,
“result”: {
“txid”: “0xc453a757082e584d5d705e28e5beb748f1539b7c14cba5f9e719228a97daf4a4”,
“executions”: [
{
“trigger”: “Application”,
“contract”: “0x8b6ee4afcdd46f15195ff6ff8c654f9d8858c395”,
“vmstate”: “HALT, BREAK”,
“gas_consumed”: “0.023”,
“stack”: [
{
“type”: “Boolean”,
“value”: true
}
],
“notifications”: []
}
]
}
}
]
The part { “type”: “Boolean”, “value”: true } indicates that contract has returned True.
— — — — — — — — — — — — — — —
This is it, any questions? Feel free to comment. :)