Skip to content

Commit b218a63

Browse files
committed
Step 10 -- Reference Datatype
1 parent 53fa237 commit b218a63

File tree

8 files changed

+187
-0
lines changed

8 files changed

+187
-0
lines changed

10_ref_datatypes/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Reference Datatype
2+
3+
Solidity provides the following reference types:
4+
1) Arrays: These are fixed as well as dynamic arrays. Details are given later in this chapter.
5+
2) Structs: These are custom, user-defined structures.
6+
3) String: This is sequence of characters. In Solidity, strings are eventually stored as bytes. Details are give later in this chapter.
7+
4) Mappings: This is similar to a hash table or dictionary in other languages storing key-value pairs.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
pragma solidity ^0.4.23;
2+
contract Contract1 {
3+
4+
int[3] public list = [31,32,33];
5+
string name = "Hello";
6+
struct User {
7+
int age;
8+
string name;
9+
}
10+
11+
// internal
12+
User myUser = User(32,"Asad");
13+
14+
// This will update array's particular location
15+
function updateList() public {
16+
list[1] = 45;
17+
}
18+
19+
function updateList2() public {
20+
receiveListAndUpdate(list);
21+
}
22+
23+
// function parameters are always memory data locations
24+
// Will not effect original variable that is passed in parameter
25+
function receiveListAndUpdate(int[3] a) public {
26+
a[2] = 55;
27+
}
28+
29+
function updateList3() public returns (int) {
30+
// Reference types are always at storage location we need to
31+
// provide memory keywork to create local variable
32+
// And assigning state ref variable to memory ref variable will create a separate copy
33+
// changing one will not effect other
34+
int[3] memory newList = list;
35+
newList[2] = 67;
36+
return list[2]; //33
37+
}
38+
39+
function updateList4() public returns (int) {
40+
// By default ref type's location is storage so it works as passby ref
41+
42+
//int[3] storage newList = list; //this is same below line
43+
44+
// Assigning Ref state (storage) variable to ref storage local variable
45+
// will not create a separate copy so changing one will effect other
46+
int[3] newList = list;
47+
newList[1] = 100;
48+
return list[1]; //100
49+
}
50+
51+
52+
53+
function updateStruct() public returns (int) {
54+
// Assigning Ref state (storage) variable to ref storage local variable
55+
// will not create a separate copy so changing one will effect other
56+
User myUser2 = myUser;
57+
myUser2.age = 40;
58+
return myUser.age; // 40
59+
}
60+
61+
function updateStruct2() public returns (int) {
62+
// And assigning state ref variable to memory ref variable will create a separate copy
63+
// changing one will not effect other
64+
myUser.age = 98;
65+
User memory myUser2 = myUser;
66+
myUser2.age = 50;
67+
return myUser.age; // 98
68+
}
69+
70+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
pragma solidity ^0.4.23;
2+
3+
contract Migrations {
4+
address public owner;
5+
uint public last_completed_migration;
6+
7+
8+
constructor() public {
9+
owner = msg.sender;
10+
}
11+
12+
modifier restricted() {
13+
if (msg.sender == owner) _;
14+
}
15+
16+
function setCompleted(uint completed) public restricted {
17+
last_completed_migration = completed;
18+
}
19+
20+
function upgrade(address new_address) public restricted {
21+
Migrations upgraded = Migrations(new_address);
22+
upgraded.setCompleted(last_completed_migration);
23+
}
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var Migrations = artifacts.require("./Migrations.sol");
2+
3+
module.exports = function(deployer) {
4+
deployer.deploy(Migrations);
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var contract1 = artifacts.require("./Contract1.sol");
2+
3+
module.exports = function(deployer) {
4+
deployer.deploy(contract1);
5+
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
pragma solidity ^0.4.24;
2+
import "truffle/Assert.sol";
3+
import "truffle/DeployedAddresses.sol";
4+
import "../contracts/Contract1.sol";
5+
6+
contract TestContract1 {
7+
8+
function testUpdateList() public {
9+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
10+
meta.updateList();
11+
Assert.equal(meta.list(0),31,"List 0 should be 31");
12+
Assert.equal(meta.list(1),45,"List 1 should be 45");
13+
Assert.equal(meta.list(2),33,"List 2 should be 33");
14+
}
15+
16+
function testUpdateList2() public {
17+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
18+
// updateList2 function pass state variable which is array to another function
19+
// but because function's parameter are always memory location so it
20+
// will create separate copy and changing the one will not effect the other
21+
meta.updateList2();
22+
Assert.equal(meta.list(0),31,"List 0 should be 31");
23+
Assert.equal(meta.list(1),45,"List 1 should be 45");
24+
Assert.equal(meta.list(2),33,"List 2 should be 33");
25+
}
26+
27+
function testUpdateList3() public {
28+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
29+
// And assigning state ref variable to memory ref variable will create a separate copy
30+
// changing one will not effect other
31+
int val = meta.updateList3();
32+
Assert.equal(val,33,"Val should be 33");
33+
34+
}
35+
36+
function testUpdateList4() public {
37+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
38+
// Assigning Ref state (storage) variable to ref storage local variable
39+
// will not create a separate copy so changing one will effect other
40+
int val = meta.updateList4();
41+
Assert.equal(val,100,"Val should be 100");
42+
43+
}
44+
45+
function testUpdateStruct() public {
46+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
47+
// Assigning Ref state (storage) variable to ref storage local variable
48+
// will not create a separate copy so changing one will effect other
49+
int val = meta.updateStruct();
50+
Assert.equal(val,40,"Val should be 40");
51+
}
52+
53+
function testUpdateStruct2() public {
54+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
55+
// And assigning state ref variable to memory ref variable will create a separate copy
56+
// changing one will not effect other
57+
int val = meta.updateStruct2();
58+
Assert.equal(val,98,"Val should be 98");
59+
60+
}
61+
}

10_ref_datatypes/truffle-config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
// See <http://truffleframework.com/docs/advanced/configuration>
3+
// to customize your Truffle configuration!
4+
};

10_ref_datatypes/truffle.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
// See <http://truffleframework.com/docs/advanced/configuration>
3+
// to customize your Truffle configuration!
4+
networks: {
5+
development: {
6+
host: "127.0.0.1",
7+
port: 8545,
8+
network_id: "*" // Match any network id
9+
}
10+
}
11+
};

0 commit comments

Comments
 (0)