Creating your first smart contact with semux!

Grumbles
5 min readNov 19, 2018

--

Semux is a cryptocurrency that recently added support for Ethereum smart contracts. Let’s walk through how to interact with it using solidity, remix, and the semux console. May be a little hard to follow, but let me know where it falls apart and I’ll update! I’m generally going to assume you know how ethereum handles smart contracts, and try to concentrate on the differences. There’s way smarter folks writing better docs on interacting with ethereum, so start there if this is new ground, then come back for the differences in semux.

Intro

Semux is a java-based cryptocurrency. It uses proof-of-stake consensus, 100 ‘validator’ nodes, and now supports Ethereum Virtual Machine (EVM) smart contacts (in its testnet anyway). They claim >166 TPS (compared to ethereum’s ~15 TPS), and since they are a new network, prices per contract should be much lower as well.

I’ll start with the basics, but if you’re already familiar with solidity and compiling to byte code, skip ahead.

Running testnet

Just run ./semux-gui.sh — network=testnet (two hyphens). Let it sync (takes ~2 hours), and get some semux from the testnet faucet.

Solidity

Solidity is a programming language for smart contacts. There’s a plethora of information regarding this subject, well beyond scope of this, so we’ll just start with someone else’s code :D. Let’s take everyone’s favorite, an ERC-20 token.

Specifically, the sample ERC-20 contract, as there are tutorials around about modifying it to be your own contract. As this is a testnet, I’m happy to just take random open source code off the internet for purposes of a demo.

We modify this code to give it a unique name, edit the addresses and whatever other variables we want to make it our own.

However, the general process should be similar for any other solidity contract you wish to publish/interact with. You want to take solidity code, compile it to bytecode, then deploy it to semux blockchain, then call methods on it.

Compiling Solidity

We take our solidity source code, and go to remix paste our code in, replacing the default demo, select ‘0.4.24+commit.e67f0147’ as the compiler version, and select ‘Start to compile’.

We then select our new token from the dropdown (not Erc20Interface, or any of the other classes), and click on the ‘Bytecode’ button, which copies our contract to our clipboard

Paste this into a text editor and behold a mess of gibberish

{
“linkReferences”: {},
“object”: “608060405…”,
“opcodes”: “PUSH1 0x80 PUSH1 …”
}

We want to select just the content in ‘object’ that starts with 608. That is our ‘contract’. Let’s add ‘0x’ to the front of it, so now its 0x608… We do this so it’s a properly formatted hex string for eth/semux.

Now we have to get this into semux. They have an open issue for adding web3js support, which would make this a lot more standard, but alas, it’s not implemented yet, so we need to either use their API, or their Console. I’m going to choose to use their console.

Open the semux app, and go to Help | Console. From typing ‘help’ I can see the following call.

create <from> <value> <data> <gasPrice> <gas> <fee> <nonce> <validateNonce>

Well, that’s a little sparse, but let’s figure it out.

from — address that you own that creates the contract. Must have a balance.
value — ‘0’ Not sure why it’s here?
data — your contract, prefixed by 0x
gasPrice — ‘1’ cost of gas is cheap on testnet apparently.
gas — ‘1000000’ It’s a big contract, so give it some love.
fee — ‘5000000’ 0.005 semux, in the long format. Whatever semux calls their wei or satoshi.
nonce — supposed to be optional, but in console it’s required, see below for how to find it
validateNonce — uh, ‘true’ Not sure it matters

Get a nonce

A nonce here is just the number of transactions you’ve sent. In the console, you can find it by typing ‘getAccount <your address>

> getAccount 0xebefc16b7c4d29224d67ced1e1400a1e8e024b9d

{
“success” : true,
“message” : “successful operation”,
“result” : {
“address” : “0xebefc16b7c4d29224d67ced1e1400a1e8e024b9d”,
“available” : “10000000000”,
“locked” : “0”,
“nonce” : “0”,
“transactionCount” : 0,
“pendingTransactionCount” : 0
}
}

Cool, so my nonce is ‘0’ in this case. Looks like I’m rich! but that’s only 10 semux.

I like to construct it in a text editor (notepad) so I can edit it easier, so, in the paste file with our contract, let’s add the other stuff. It should look like

create 0xebefc16b7c4d29224d67ced1e1400a1e8e024b9d 0 0x60806040523…giantstring 1 1000000 5000000 0 true

Paste this into the semux console and press enter. With any luck, you should see

{
“success” : true,
“message” : “successful operation”,
“result” : “0x2aec2fd2ef5a1309fd36ad2e2ed2303e9132fd9ad015fa0c6a5c1bd7039ed5dd”
}

This means it should now show up in your Transactions tab as ‘pending’. Wait 30 seconds.

Ok, now we have a contract! To find the ‘contract address’ is same as in ethereum (some hash of source address and nonce), but we’ll take a shortcut. If we double click on our create call in transactions, the ‘to’ field is our contract address. In this case ‘0xdf678702e40cc6daf1974d431b9bb573459e0306’.

Congrats, you’ve created a contract on the blockchain!

Calling our contract

Let’s send some of our new ERC-20 coin to another address

in our semux console window for help we see

call <from> <to> <value> <gasPrice> <gas> <fee> <nonce> <validateNonce> <data> <local>

Describing the params:

from — our contract owner from first call
to — our new contract address
value — again 0, we’re not sending see
gasPrice — 1
gas — much smaller call, so let’s try ‘50000’
fee — ‘5000000’
nonce — will be a new nonce, since my nonce last time was ‘0’, now it will be ‘1’
validateNonce — ‘true’
data — the ABI call, see below
local — ‘false’ This controls if we’re making a transaction, or just using the read-only methods, like getBalance()

ABI call data

There’s a lot of information about this online. It’s same as ethereum, so let’s just do it the easy way. It’s a whole other topic that is fairly complex, but let’s simplify. If we go back to our remix window…

Change to the ‘run’ tab, select our Token from dropdown, and click deploy.

In ‘deployed contracts’ expand the window, and you can see the calls available. Let’s expand ‘transfer’ and fill out address and tokens. I’m going to send 10000 of my TestToken to another address. After I fill out the fields, I click the little toolbox icon, and it will copy the following to my clipboard

0xa9059cbb000000000000000000000000837c1e6ceca4ccad20e59849cb7a9678394f73130000000000000000000000000000000000000000000000000000000000002710

You can construct these by hand, but it’s easier to do with web3js or the remix site. This is our data field for call.

We end up with

call 0xebefc16b7c4d29224d67ced1e1400a1e8e024b9d 0xdf678702e40cc6daf1974d431b9bb573459e0306 0 1 50000 5000000 1 true 0xa9059cbb000000000000000000000000837c1e6ceca4ccad20e59849cb7a9678394f73130000000000000000000000000000000000000000000000000000000000002710 false

We paste our filled out version into the console, and let it run. Again, we see success and a pending transaction, and we’ve just transferred funds to a new account, once transaction succeeds.

Wow, there’s a lot to cover here, but hopefully that’s given you some idea how to interact with semux using mostly ethereum tools and the occasional Semux console function. I hope this gets easier in the future.

--

--