Blockchain Enthusiast (qq:53016353)
A simple implementation of an array of additions and deletions of the smart contract pragma solidity 0.4.9;
/* Array type demo*/
Contract DemoTypes303 {
/*string Array Example */
String[] Strarr;
function Add (String str) {
Strarr.push (str);
}
function Getstrat (uint N) constant returns (string s) {
string tmp = Strarr[n];
return TMP;
}
function Updatestrat (uint n, string str) {
Strarr[n] = str;
}
function Deletestrat (UINT index) {
UINT len = strarr.length;
if (index >= len) return;
for (UINT i = index; i<len-1; i++) {
Strarr[i] = strarr[i+1];
}
Delete Strarr[len-1];
strarr.length--;
}
}
Code Analysis
String[] Strarr; An array variable Strarr that defines a string, and the variable is not public and therefore not visible
Strarr.push (str); The push is one of the two member of the array type, and the other is length. The push here is to add an element to the array. New features for arrays are implemented here
function Add (String str) {
Strarr.push (str);
}
Getstrat (uint N) is a simple function to read a string,//The Read function of an array is implemented here
Updatestrat (uint N, string str)//implement update function for arrays
Deletestrat (UINT index) This is worth saying, because the solidity default array type does not have a delete function, so here you write a delete code, the core method is to ensure that after the deletion of an item, the subsequent elements go forward, and delete the data, Also ensure that the member length of the array is correct.
Browser solidity deployment and invocation p111
Copy the above code to the browser-solidity, and you can see that the compilation was successful and the deployable Web3 deploy code was generated.
We'll try this in memory. 4 basic methods of the Smart contract Delete and change
1. Add function
P1
Enter Hello 01 and then click the Add button and we can see that the data is executed successfully after a portion of gas has been consumed.
PS: It is important to note that here the Getstrat and Add,update,delete methods such as color, according to the above classification, one is transaction, one is call, so the method of invocation on the Geth is also different.
2. Getstrat function
Now that we have added an element, we can look at the value of the array Strarr, and based on the knowledge of the array, we only add one element, so getstrat[0] should be Hello 01. and getstrat[1] should be an error.
The actual results are as follows: Getstrat[0]
P2
GETSTRAT[1]:
P3
3. Updatestrat Functoin
P4
4. Deletestrat function
To test this method, a little more trouble, preferably
Step 1 requires Add ("Hello"), add ("Hello"), add ("Hello 03")
Step 2 Then call Deletestrat (0);
Step 3 Checks if the first item is deleted, while Getstrat (0) should be equal to "Hello 02″, Getstrat (1) should be equal to" Hello 03″
Then repeat the step1-3 for Deletestrat (1), delete the intermediate items, or Deletestrat (2) To delete the last item, and Deletestrat (3)/deletestrat (+)/deletestrat (-100 Check whether the boundary check is done.
Here time is limited, do not experiment, interested students can themselves in browser solidity above try.
As a gift, there are a few other simple smart contracts, interested students can use more browser-solidity to try
pragma solidity ^0.4.4;
Contract Demotypes {
/*uint Public resultoff;*/
Event Loguint (uint value);
Event Logint (int value);
function f (UINT a) returns (UINT B)
{
UINT result = A * 8;
/*resultoff = result; Debug used only;*/
Loguint (result);
return result;
}
/* Enter length width, return area */
function f2 (int width, int height) returns (int square) {
if (Width < 0 | | Height < 0) throw;
int result = width * height;
Logint (result);
return result;
}
/* Enter n, calculate the factorial of n, loop implementation */
function F3 (UINT N) returns (UINT Jiecheng) {
if (n = = 0) throw; UINT result = 1;
for (uint I=1; i<=n; i++) {
Result *= i;
}
return result;
}
/* Calculates the sum from 1 to n */
function F4 (UINT N) returns (UINT sum) {
if (n = = 0) throw; UINT result = 0;
for (uint i=0; i<=n; i++) {
Result +=i;
}
return result;
}
}
In the next chapter we will introduce a simplified version of the token contract, so please look forward to it.
—————————————————————————————————————
Start with a minimized token contract;
pragma solidity 0.4.9;
Contract Token {
function Token () {
}
}
As previously mentioned, solidity is a class JS language, and as with all object-oriented languages, solidity also has the most basic constructors.
The code above is one of the simplest Token contracts, and function Token () {} is the constructor for the contract.
The contract can be executed successfully on browser-solidity, and this is not demonstrated here.
We add a number array to this token tokens contract and give an initial value.
pragma solidity 0.4.9;
Contract Token2 {
uint[] public balancesof;
function Token () {
Balancesof.push (100);
Balancesof.push (200);
}
}
The contract is simple, defining a public variable balancesof, and then adding two initial values to the variable at the construction stage. & 200
P5
Users first click on the token constructor, then enter 0 and 1, and then click Balancesof to see two values respectively.
P6
Add an Account transfer function to the token contract transfer ()
pragma solidity 0.4.9;
Contract Token3 {
uint[] public balancesof;
function Token () {
Balancesof.push (100);
Balancesof.push (200);
}
function transfer (UINT _from, uint _to, uint _amount) {
Balancesof[_from]-= _amount;
BALANCESOF[_TO] + = _amount;
}
}
Transfer function of the code is to transfer a quantitative _amount from _from to _to;
This feature was successfully debugged in browser solidity, as shown in the following figure:
P7
Add a mining method to the token tokens contract mint ()
As shown in the code above, we can see that only the balancesof is assigned at the time of the constructor, and then the transfer is always the same, regardless of the operation. So we can add a mining method, the added amount is summed up by default in the first account in Balancesof[0].
pragma solidity 0.4.9;
Contract TOKEN4 {
uint[] public balancesof;
function Token () {
Balancesof.push (100);
Balancesof.push (200);
}
function transfer (UINT _from, uint _to, uint _amount) {
Balancesof[_from]-= _amount;
BALANCESOF[_TO] + = _amount;
}
function mint (uint value) {
Balancesof[0] + = value;
}
}
by Mint () We are free to add tokens to balancesof[0. This code is interested in classmates, please experiment on browser-solidity.
Note that the above code is used only for learning, and many scenarios have not been considered, such as scenarios where _amount> current account balances are not considered in transfer. Not available for commercial applications.
This paper introduces the core functions of token, including construction, mining, transfer and so on.
However, we know that the core of Ethereum is the account system and the smart contract, and what really makes sense is a currency contract that can be circulated between accounts. With these tokens, we can be used for crowdfunding, crowdsourcing and other business applications.
The next chapter describes a token contract that includes an account.
——————————————————————————————————————————
Token contract based on the account address
pragma solidity 0.4.9;
Contract Token {
Mapping (address = uint) public balancesof;
function Token () {
Balancesof[msg.sender] = 10000;
}
}
Mapping definition of key words
Mapping types is declared as Mapping (_keytype = _valuetype). Here _keytype can is almost any type except for a mapping, a dynamically sized array, a contract, an enum and a struct. _valuetype can actually is any type, including mappings.
Mappings can be seen as hashtables which being virtually initialized such that every possible key exists and was mapped to a Value whose byte-representation is all zeros:a type ' s default value. Quote from here
Definition of Address:
Address:holds A-byte value (size of an Ethereum address). Address types also has members and serve as base for all contracts.
Members of Addresses:balance and send
Quote from here
The address here is simply a geth of the account (public key)
So here's the code mapping (address = UINT) Balancesof representative defines a key as the address type, value is a uint-type Hashtable, and the name is balancesof.
So any account that is on the blockchain's private chain can be queried for the number of tokens through balancesof[address].
Definition of MSG
MSG refers to the information that is sent when the smart contract is called, and the smart contract is also an account. So invoking a smart contract in Ethereum from the bottom of the list is an account that sends a transaction to the smart contract account that contains who sent it, how much of the etheric currency was sent, how much gas it sent
Here are some of the variables for MSG
Msg.data (bytes): Complete Calldata
Msg.gas (UINT): remaining gas
Msg.sender (address): Sender of the message (current call)
Msg.sig (BYTES4): First four bytes of the calldata (i.e. function identifier)
Msg.value (UINT): Number of Wei sent with the message referenced from here
The main thing is Msg.sender, which calls the smart contract on behalf of that account. Msg.value
therefore balancesof[msg.sender] = 10000; The delegate assigns an initial value of 10000 to Msg.sender, which is the address where the contract was created;
A token contract with transfer & Mining
pragma solidity 0.4.9;
Contract Token {
Mapping (address = uint) public balancesof;
Address public owner;
function Token () {
Owner = Msg.sender;
Balancesof[msg.sender] = 10000;
}
function transfer (address _to, uint _value) {
if (Balancesof[msg.sender] < _value) throw; Avoid transfers of tokens over current inventory
if (balancesof[_to] + _value < balancesof[_to]) throw; Avoid calling yourself, or recursively calling
Balancesof[msg.sender]-= _value;
BALANCESOF[_TO] + = _value;
}
function mint (uint _amount) {
Balancesof[owner] + = _amount;
}
}
In the above code, we set a public owner, save the Msg.sender of the created contract, and record the creator's address.
In function transfer (), we set up two conditions to prevent the transfer of tokens from exceeding the inventory, as well as the problems that you transferred to yourself.
in function mint (), we add tokens we define to the owner of the contract. So that it can be conveniently divided to others.
The above contract code on the Browser-solidity debug Pass, view the following figure
P8
We will have Owner = Msg.sender in the code, so owner is the address to create the contract. We can see that the data for Balancesof[owner] is 10000, and the code is the same.
P9
Call Method Mint (500), automatically give Balancesof[owner] + = 500; View deposit increased from 10000 to 10500.
P10
Calling method Transfer ("0XCCCCC", 300), you can see that the owner's save amount is reduced 300,balancesof["0XCCCCC"] to increase by 300.
P11
This contract achieves the most streamlined token functionality, but in browser-solidity we can see the obvious question of not being combined with several ethereum accounts in the Geth that we set up in the second part.
In the next chapter we will introduce another very useful tool, mist, to combine smart contracts, Geth, and mist. Really run smart contracts on the ethereum private chain.