How GCC Turns into Ether — Exchange Code Analysis

Seung Woo Kim
GoCryptobot
Published in
4 min readJun 1, 2018

In GoCryptobot, one can earn GCC(an ERC20 token) by participating in the PvP or selling his/her parts on the Market. Users can either purchase new parts with GCC or save it up for future purposes. However, you can also convert your GCC into Ethereum. How does this process work?

You can read more about GCC and ERC20 tokens here.

GoCryptobot in-game wallet

Withdraw GCC First

The circulation of GCC within the game client happens within the game server’s database. However, when charging or withdrawing GCC, the blockchain is utilized. This is because if every action used the blockchain, then there is a fee for every little transaction made. For more details about this, please read this article.

Users can transfer GCC from their in-game wallet to their own wallet’s address. When the withdraw button is pressed and the target address is inserted, the game server sends GCC to the users address by using the transfer() function within the GoCryptobotCoin contract.

Users can see that their GCC balance has increased through the balanceOf() function.

The exchange address to convert GCC into Ether is:

http://gocryptobot.io/exchange.html

In the exchange, the rate for conversion between GCC and Ether is written down. 1GCC is pegged at 1USD, and the user can see how much Ether can be exchanged for the amount of GCC inserted at that point in time. Since Ether’s price changes every second, we need logic that checks Ethereum’s municipal rate and store its value every day.

function setExchangeRate(uint256 rate) external onlyOwnerOrOperator {
ExchangeRateChange(exchangeRate, rate);
exchangeRate = rate;
}

The contract above is called everyday in order to check Ethereum’s rates and decide the new exchange rate.

What Happens Once You Press the Exchange Button

If you know how ERC20 tokens work, then you probably somewhat expected the logic below.

  • Call the approve() function so that the exchange can take the user’s token.
  • The exchange checks if the token is an allowed one by calling allowance().
  • Send the allowed token to the exchange by calling transferFrom().
  • Send Ether to the user’s address.
Flow before applying ERC827

GoCryptobot’s exchange is a web application, and the user creates the contract that calls the transaction, which is all done by utilizing Metamask. Once the user clicks on the exchange button, the exchange uses Metamask in order to get access to the user account which contains the GoCryptobotCoin contract. Within this contract, a transaction is created which calls approve(). Then, by using GCCExchangeCore contract’s withdraw() function, it takes the allowed GCC and creates a transaction that gives Ether. In order for GCC to become allowed and for the exchange to convert GCC into Ether, there has to be a total of two transaction calls. However, GCC has an additional implementation of ERC827 in order to combine the two transaction calls into a single transaction call.

ERC827 expands ERC20’s functionalities and is an interface that allows transfer() and approve() to be internal transactions. The GoCryptobotCoinERC827 contract of GCC contains the functionality of doing an internal transaction at the end of transfer, approve functions, which is a functionality of ERC827.

If you were to call withdraw(), which is implemented using Javascript, it internally calls the approve() function of the GCC contract, passing the callback function that calls the withdraw() function of the GoCryptobotExchange contract as the final argument, all according to ERC827’s specification.

Flow after applying ERC827

Thus, if you were to call the withdraw() code above, then the GCC contract’s approve() and GoCryptobotExchange contract’s withdraw() are called in that order respectively.

GoCryptobotExchange contract’s withdraw() function(called by the web server) internally calls the token’s allowance() function to determine the allowance, and call transferFrom() to deduct the the amount of GCC with the processing fee added onto it.

coin.allowance()

Coin stores the address of GoCrytobotCoinERC827 via setCointContract(), which was already called previously. If the address is stored in a similar manner as above, then the applicable address’ contract can be called.

We have discussed how GCC gets converted into Ether. Since everything runs on the blockchain, the user can trust the developers, and the developers and gain the user’s trust more easily than before. (If a user claims they did not receive the money, the devs can prove successful transaction with etherscan.) Although these new functionalities are great, many users still face difficulty in using the Metamask, or other private wallets out there, and thus, have trouble using the exchange. Thus, I think developers should think about improving accessibility for the average user.

** Warning

If there is not enough Ether to withdraw, there will be an error which results in a withdraw request failure. This ultimately leads to the user wasting his/her gas. Always check if you have enough Ether in the exchange or not.

You can download GoCryptobot through the links below:

- App Store: https://apple.co/2KbMipn

- Google Play: https://bit.ly/2K5tYhx

(To read the Korean version of this article, click here)

--

--