Dry goods | Super detailed explanation, first experience of Thunderchain deployment application
星球君的朋友们
2018-10-17 09:41
本文约4295字,阅读全文需要约17分钟
This is the first time I know Thunderchain, and I think Thunderchain is a good choice. So I think it can be developed and applied on it. Thunderchain provides a sandbox environment for testing, and the official documents are written in more detail. But I

This is the first time I know Thunderchain, and I think Thunderchain is a good choice. So I think it can be developed and applied on it. Thunderchain provides a sandbox environment for testing, and the official documents are written in more detail. But I have done related deployment in this sandbox environment, so I will provide a tutorial for calling.

To use the test environment, you must prepare the test environment, such as the developer account for the test and the LinkToken wallet for the test. The official documentation provides many APIs to meet the needs of developers. The first step to develop your own contract application using ThunderChain is to register the developer email.


If developers want their app to use the contract service of the ThunderChain open platform, they only need to call the construction url, and the relevant interface can be seen through the official document. For example, the command line curl command can be used for email registration, and the json email address can be filled in by yourself. Your email address will do:

curl -H "Content-Type:application/json" -X POST -d 

'{"email":"1191094115@qq.com"}' https://sandbox-

walletapi.onethingpcs.com/api/linktest/email_code

If the mailbox has not been registered, it will return:

{"code":0,"data":{},"msg":""}

At the same time, the mailbox will receive an email at this time:

This is just the first step in the development of Thunderchain. Of course, in addition to typing commands directly on the command line, you can also call the api through an official api calling web page.

website link:

https://thunderchain.docs.apiary.io/#reference/0

Open the page to see:

Click Get Email Verification Code to provide it.

It depends on your preferences whether you use the web page to call the api or call it through the command line.

Click the Get Email Verification Code button: the page will change.


At this time, you need to fill in the Body, and other options URL Parameters, Headers can be ignored

After filling in, click the call Resource button, and the post request will be sent successfully

At this point, you will see relevant information in the respond window:

The above is familiar with the basic call operation of the API. In order to describe the whole process more simply, let’s deploy it completely through the web page and call a contract. Of course, the actual application is to implement contract deployment and invocation by developers using the network library to make api calls in their software.

After obtaining the email verification code, you can register your email address.

On the previous web page, click the regist button registered by the developer’s email address: you can see

 

At this time, fill in the email you registered before and the verification code received by the email.

After registration, you can get emails:

These are the two signatures for various operations of the contract.

To call the contract, you have to provide fuel to the ThunderChain contract virtual machine, which is the transaction fee, and you can get some test environment tokens through the test account recharge interface (this interface can only be called 10 times a day). There may be children here who don’t understand the logic that contract developers and the ThunderChain development platform have to call. Here is a flow chart of the official document.

 

The above picture is relatively clear and needs to be clarified:

First of all, the LinkToken pocket cannot be separated from the entire contract call. The LinkToken private key saved in the pocket will issue the transaction, and its LinkToken address will be used as the sender of the contract.

Therefore, the first step is to recharge the LinkToken pocket for testing. The test interface for recharging is:

https://sandbox-walletapi.onethingpcs.com/api/linktest/recharge。

Parameter Description:


To recharge, you must first have a LinkToken address, and generate an account through the LinkToken pocket.

 


Build json data, the account is the address

while the signature is:

md5(email=xxx&address=xxx&secret=xxx)

Here xxx is replaced by personal information: as shown in the figure below, the personal information has been mosaiced. Through this, the signature can be obtained, and at this time, the api interface can be called to make the interface call

 


Build json as shown in the figure:

 

 

One of the developers can only call this interface 10 times a day, after calling, refresh the LinkToken Pocket:

 

At this point 1 LinkToken has arrived!

After you have LinkToken, you can deploy your contract on the Thunder Chain. To successfully deploy the contract, you need to provide basic gas.

Just like the flow chart above, to deploy a contract, you must first build the corresponding json data and send it to

https://sandbox-walletapi.onethingpcs.com/api/linktest/contract/deploy interface

Parameter Description:

After preparing the above things, the contract can be developed and deployed. Contract development can be developed with truffle.


Use truffles to develop a Simplestore contract, note that the bytecode here is a hexadecimal number, if the contract references the Library contract, you need to deploy the Library contract on the Thunder chain, and change the referenced Library field of the real contract to the Library address . In this way, the bytecode is all binary, and the build folder can be obtained through the truffle compile command. Find the bytecode field information in the contract that needs to be called


The contract here is:

pragma solidity ^0.4.21;

 contract SimpleStorage {

     uint myVariable;

 

     function set(uint x) public {

         myVariable = x;

     }

 

     function get() constant public returns (uint) {

         return myVariable;

     }

 }

After building the sign

 

get json:

{

    "email": "1191094115@qq.com",

    "bytecode": "0x608060405234801561001057600080fd5b5060df8061001f6000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146078575b600080fd5b348015605957600080fd5b5060766004803603810190808035906020019092919050505060a0565b005b348015608357600080fd5b50608a60aa565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a72305820101eb74d2d65698a7fd1e63666842673ffb013a500c768f84643fc6b896519430029",

    "params": "",

    "sign": "0d0734ca430c7b79d332d700b0b44cd9"

}


got the answer:

 

After obtaining the id, you can use the id to query the address of the contract.

Its requested information is:

Method: POST
URL: /api/linktest/contract/address
BODY: JSON

Parameter Description:

parameter name

Parameter Type

yes

illustrate

Email

string

yes

yes

Id

int

yes

yes

Sign

string

yes

Signature md5(email=xxx&id=xxx&secret=xxx), xxx fills in the actual value of the request


response

parameter name

Parameter Type

yes

illustrate

Code

int

yes

yes

Msg

string

yes

no

Data

object

no

request return data


Get sign:

 


The contract address can be obtained later

 


After getting the contract address, the contract can be called.

 

 

According to the flow chart above, if you want to make a contract call, you must first obtain the prepay_id to use the LinkToken Pocket in the third party. Here is the process for the third party to obtain the prepay_id, but it is just a demonstration.


test environment:

https://sandbox-walletapi.onethingpcs.com/getPrepayId


Request method: post

When obtaining the prepay_id, the private key entered is the secret sent by the previous email:

 


get sign like this

Because the parameters are the same, the json of each request is the same, and the response is obtained at this time:


There are two contract methods, one is the constant method, and the other is the method that requires gas. This time we call Simplestore's get() method. Then you need to meet:

Method: POST
URL: /call
BODY: JSON

Parameter Description:

 

params detail

object details

object - transaction call object

Where data is a hash string of the calling function

Its calculation method is as follows;

Suppose the method function bar(fixed[2] x,bool y), where x is 1 and y is 2

At this time, the calculated data is:

1. Perform sha3 on the ascii code of bar(fixed[2], bool), and take the first 8 digits of the hexadecimal representation of the result

2. Convert the value of x to a uint32 number, and convert the value of y to a uint32 number

3. Put together the strings obtained in steps 1 and 2 to get data

You can get the sha3-256 value of get()

6d4ce63caa65600744ac797760560da39ebd16e8240936b51f53368ef9e0e01f

take the first 8 digits

Because there are no parameters, you can build json

 

At this point the result is

 

Because the get method gets 0 when the contract is deployed, it returns 0.

After calling the constant method, you can call the method that requires gas

Call the set(uint x) function this time, because although uint is written, it will be converted to uint256, so

According to the json format, at this time, the hash value of the sha3-256 of set(uint256) should be obtained and the first 8 bits should be taken:

cccdda2c

 

At this point the json can be constructed.

 

At the same time we make x 256, we calculate the hexadecimal 100 of 256 in decimal, and then fill 100 to 32 bits to get

0x00000000000000000000000000000100

put together again

0x60fe47b100000000000000000000000000000100

This is called data.

Here is the address, you can directly select the callback address of LinkToken Pocket. As for the gas_limit in json, it can be obtained through such a request. Of course, if the contract is to succeed, the gas can be as large as possible.

 

The return is a hexadecimal number, just convert it to decimal.

 

Converted to decimal: 26477, but in fact, the contract uses 41605, so it is right to have a larger gas

build json


got the answer:

 

At this time, the obtained callback is the callback url of LinkToken Pocket. Convert url to QR code.

 

Then use LinkToken Pocket to scan:

If successful:

 


If the contract executes successfully

 

If it fails: here I call a wrong method (construct wrong data)

So far, the process of a contract call has ended.

We changed the variable of the contract from 0 to 256, so let's check it through the get method now.

Through the previous constant method, the response at this time is: ps The constant method can also be run with transaction fees through the virtual machine, but this is not necessary.

Convert the hexadecimal to decimal, it is 256!

The above is the first experience of the Thunder Chain deployment contract process, I hope it can give some help to the children's shoes in the future ^.^ Dear students.

 


星球君的朋友们
作者文库