Skip to content

Commit 091b7a4

Browse files
committedMar 12, 2023
Updating chapter codes
1 parent b39aad8 commit 091b7a4

24 files changed

+511
-0
lines changed
 

‎chapter 20/DIDExample.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"@context": [
3+
"https://www.w3.org/ns/did/v1",
4+
"https://w3id.org/ethr/credential/v1"
5+
],
6+
"id": "did:ethr:0x123456789abcdefghijklmnopqrstuvwxyz",
7+
"authentication": [
8+
{
9+
"id": "did:ethr:0x123456789abcdefghijklmnopqrstuvwxyz#controller",
10+
"type": "Secp256k1SignatureAuthentication2018",
11+
"publicKey": "0x123456789abcdefghijklmnopqrstuvwxyz#controller",
12+
"controller": "did:ethr:0x123456789abcdefghijklmnopqrstuvwxyz"
13+
}
14+
]
15+
}

‎chapter 20/VCExample.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"@context": [
3+
"https://www.w3.org/2018/credentials/v1",
4+
"https://www.w3.org/2018/credentials/examples/v1"
5+
],
6+
"id": "https://example.com/credentials/123",
7+
"type": [
8+
"VerifiableCredential",
9+
"DegreeCredential"
10+
],
11+
"issuer": {
12+
"id": "https://example.com/issuers/14",
13+
"name": "Example University"
14+
},
15+
"issuanceDate": "2023-02-16T22:12:03Z",
16+
"credentialSubject": {
17+
"id": "did:example:123",
18+
"degree": {
19+
"type": "BachelorDegree",
20+
"name": "Bachelor of Science in Computer Science",
21+
"college": "College of Engineering and Applied Science",
22+
"university": "Example University",
23+
"degreeDate": "2022-05-15",
24+
"degreeStatus": "awarded"
25+
}
26+
},
27+
"proof": {
28+
"type": "Ed25519Signature2018",
29+
"created": "2023-02-16T22:12:03Z",
30+
"proofPurpose": "assertionMethod",
31+
"verificationMethod": "https://example.com/issuers/14#key-1",
32+
"jws": "eyJhbGciOiJFZERTQSIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19..0pw88foZzKHJmP4x4m0F5m5ZOyP5WAGGbsS0L9CKiKjxI5_ZfE5i5y5q5vqBQ9XivnC5pwcbZi75jmZ1xou4Dw"
33+
}
34+
}

‎chapter11/Addition.sol

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: NONE
2+
pragma solidity ^0.8.0;
3+
4+
contract Addition {
5+
uint8 x;
6+
7+
function addx(uint8 y, uint8 z) public {
8+
x = y + z;
9+
}
10+
11+
function retrievex() public view returns (uint8) {
12+
return x;
13+
}
14+
}

‎chapter11/FunctionTest1.sol

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: NONE
2+
pragma solidity >=0.8.0;
3+
4+
contract Test1 {
5+
uint256 x = 2;
6+
7+
function addition1() public view returns (uint256 y) {
8+
y = x + 2;
9+
}
10+
}

‎chapter11/Test1.sol

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: NONE
2+
pragma solidity >=0.8.0;
3+
4+
contract Test1 {
5+
function addition1(uint256 x, uint256 y)
6+
public
7+
pure
8+
returns (uint256 z, uint256 a)
9+
{
10+
z = x + y;
11+
a = x + y;
12+
return (z, a);
13+
}
14+
}

‎chapter11/TestStruct.sol

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: NONE
2+
pragma solidity ^0.8.0;
3+
4+
contract TestStruct {
5+
struct Trade {
6+
uint256 tradeid;
7+
uint256 quantity;
8+
uint256 price;
9+
string trader;
10+
}
11+
//This struct can be initialized and used as below
12+
Trade tStruct =
13+
Trade({tradeid: 123, quantity: 1, price: 1, trader: "equinox"});
14+
}

‎chapter11/ValueChecker.sol

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: NONE
2+
pragma solidity >=0.8.0;
3+
4+
contract valueChecker {
5+
uint8 price = 10;
6+
//price variable declared and initialized with a value of 10
7+
event valueEvent(bool returnValue);
8+
9+
function Matcher(uint8 x) public returns (bool y) {
10+
if (x >= price) {
11+
emit valueEvent(true);
12+
y = true;
13+
}
14+
}
15+
}

‎chapter11/myContract.sol

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
contract myContract {
2+
function getValue() returns (uint256 z) {
3+
z = x + y;
4+
}
5+
}

‎chapter11/valuecheckercontract2.sol

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: NONE
2+
pragma solidity >=0.8.0;
3+
4+
contract valueChecker {
5+
uint8 price = 20;
6+
event valueEvent(bool returnValue);
7+
8+
function Matcher(uint8 x) public returns (bool y) {
9+
if (x >= price) {
10+
emit valueEvent(true);
11+
y = true;
12+
}
13+
}
14+
}
15+
16+
contract valueChecker2 is valueChecker {
17+
function Matcher2() public view returns (uint256) {
18+
return price + 10;
19+
}
20+
}

‎chapter12/1_initial_migration.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var Migrations = artifacts.require("./Migrations.sol");
2+
module.exports = function (deployer) {
3+
deployer.deploy(Migrations);
4+
};
5+

‎chapter12/2_deploy_contracts.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var SimpleStorage = artifacts.require("Addition");
2+
module.exports = function (deployer) {
3+
deployer.deploy(SimpleStorage);
4+
};

‎chapter12/Addition.sol

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-License-Identifier: NONE
2+
pragma solidity ^0.8.0;
3+
4+
contract Addition {
5+
uint8 x; //declare variable x
6+
7+
// define function addx with two parameters y and z, and modifier public
8+
function addx(uint8 y, uint8 z) public {
9+
x = y + z; //performs addition
10+
}
11+
12+
// define function retrievex to retrieve the value stored, variable x
13+
function retrievex() public view returns (uint8) {
14+
return x;
15+
}
16+
}

‎chapter12/Migrations.sol

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-License-Identifier: NONE
2+
pragma solidity >=0.4.21 <0.7.0;
3+
4+
contract Migrations {
5+
address public owner;
6+
uint256 public last_completed_migration;
7+
8+
constructor() public {
9+
owner = msg.sender;
10+
}
11+
12+
modifier restricted() {
13+
if (msg.sender == owner) _;
14+
}
15+
16+
function setCompleted(uint256 completed) public restricted {
17+
last_completed_migration = completed;
18+
}
19+
}

‎chapter12/TestAddition.sol

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: NONE
2+
pragma solidity ^0.8.0;
3+
import "truffle/Assert.sol";
4+
import "truffle/DeployedAddresses.sol";
5+
import "../contracts/Addition.sol";
6+
7+
contract TestAddition {
8+
function testAddition() public {
9+
Addition adder = Addition(DeployedAddresses.Addition());
10+
adder.addx(100, 100);
11+
uint256 returnedResult = adder.retrievex();
12+
uint256 expected = 200;
13+
Assert.equal(returnedResult, expected, "should result 200");
14+
}
15+
}

‎chapter12/app.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var Web3 = require('web3');
2+
if (typeof web3 !== 'undefined') {
3+
web3 = new Web3(web3.currentProvider);
4+
} else {
5+
// set the provider you want from Web3.providers
6+
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8001"));
7+
}
8+
web3.eth.defaultAccount = web3.eth.accounts[0];
9+
var SimpleContract = web3.eth.contract([
10+
{
11+
"constant": false,
12+
"inputs": [
13+
{
14+
"name": "x",
15+
"type": "uint8"
16+
}
17+
],
18+
"name": "Matcher",
19+
"outputs": [
20+
{
21+
"name": "",
22+
"type": "bool"
23+
}
24+
],
25+
"payable": false,
26+
"stateMutability": "nonpayable",
27+
"type": "function"
28+
},
29+
{
30+
"anonymous": false,
31+
"inputs": [
32+
{
33+
"indexed": false,
34+
"name": "returnValue",
35+
"type": "bool"
36+
}
37+
],
38+
"name": "valueEvent",
39+
"type": "event"
40+
}
41+
]);
42+
var simplecontract = SimpleContract.at("0x3b52828c63ffdcb27fa5105bc66b8f1cb1bc648c");
43+
console.log(simplecontract);
44+
function callMatchertrue() {
45+
var txn = simplecontract.Matcher.call(12);
46+
{
47+
};
48+
console.log("return value: " + txn);
49+
}
50+
function callMatcherfalse() {
51+
var txn = simplecontract.Matcher.call(1); {
52+
};
53+
console.log("return value: " + txn);
54+
}
55+
function myFunction() {
56+
var x = document.getElementById("txtValue").value;
57+
var txn = simplecontract.Matcher.call(x); {
58+
};
59+
console.log("return value: " + txn);
60+
document.getElementById("decision").innerHTML = txn;
61+
}

‎chapter12/index.html

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<html>
2+
3+
<head>
4+
<title>SimpleContract Interactor</title>
5+
<script src="./web3.min.js"></script>
6+
<script src="./app.js"></script>
7+
</head>
8+
9+
<body>
10+
<p>Enter your value:</p>
11+
<input type="text" id="txtValue" value="">
12+
<p>Click the "get decision" button to get the decision from the smart contract.</p>
13+
<button onclick="myFunction()">get decision</button>
14+
<p id="decision"></p>
15+
<p>Calling the contract manually with hardcoded values, result logged in browser debug console:</p>
16+
<button onclick="callMatchertrue()">callTrue</button>
17+
<button onclick="callMatcherfalse()">callFalse</button>
18+
</body>
19+
20+
</html>

‎chapter12/privategenesis.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"nonce": "0x0000000000000042",
3+
"timestamp": "0x00",
4+
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
5+
"extraData": "0x00",
6+
"gasLimit": "0x8000000",
7+
"difficulty": "0x0400",
8+
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
9+
"coinbase": "0x3333333333333333333333333333333333333333",
10+
"alloc": {},
11+
"config": {
12+
"chainId": 786,
13+
"homesteadBlock": 0,
14+
"eip150Block": 0,
15+
"eip155Block": 0,
16+
"eip158Block": 0
17+
}
18+
}

‎chapter12/simplecontract2.sol

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: NONE
2+
pragma solidity ^0.8.0;
3+
4+
contract SimpleContract2 {
5+
uint256 z;
6+
7+
function addition(uint256 x) public returns (uint256 y) {
8+
z = x + 5;
9+
y = z;
10+
}
11+
12+
function difference(uint256 x) public returns (uint256 y) {
13+
z = x - 5;
14+
y = z;
15+
}
16+
17+
function division(uint256 x) public returns (uint256 y) {
18+
z = x / 5;
19+
y = z;
20+
}
21+
22+
function currValue() public view returns (uint256) {
23+
return z;
24+
}
25+
}

‎chapter12/truffle.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
// Uncommenting the defaults below
3+
// provides for an easier quick-start with Ganache.
4+
// You can also follow this format for other networks;
5+
// see <http://truffleframework.com/docs/advanced/configuration>
6+
// for more details on how to specify configuration options!
7+
//
8+
networks: {
9+
development: {
10+
host: "127.0.0.1",
11+
port: 7545,
12+
network_id: "*"
13+
},
14+
test: {
15+
host: "127.0.0.1",
16+
port: 7545,
17+
network_id: "*"
18+
}
19+
}
20+
};

‎chapter18/MyERC20Token.sol

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// SPDX-License-Identifier: NONE
2+
pragma solidity ^0.8.0;
3+
4+
contract MyERC20Token {
5+
mapping(address => uint256) _balances;
6+
mapping(address => mapping(address => uint256)) _allowed;
7+
8+
string public name = "My ERC20 Token";
9+
string public symbol = "MET";
10+
uint8 public decimals = 0;
11+
uint256 private _totalSupply = 100;
12+
event Transfer(address indexed _from, address indexed _to, uint256 _value);
13+
event Approval(
14+
address indexed _owner,
15+
address indexed _spender,
16+
uint256 _value
17+
);
18+
19+
constructor() {
20+
_balances[msg.sender] = _totalSupply;
21+
emit Transfer(address(0), msg.sender, _totalSupply);
22+
}
23+
24+
function totalSupply() public view returns (uint256) {
25+
return _totalSupply - _balances[address(0)];
26+
}
27+
28+
function balanceOf(address _owner) public view returns (uint256 balance) {
29+
return _balances[_owner];
30+
}
31+
32+
function allowance(address _owner, address _spender)
33+
public
34+
view
35+
returns (uint256 remaining)
36+
{
37+
return _allowed[_owner][_spender];
38+
}
39+
40+
function transfer(address _to, uint256 _value)
41+
public
42+
returns (bool success)
43+
{
44+
require(
45+
_balances[msg.sender] >= _value,
46+
"value exceeds senders balance"
47+
);
48+
_balances[msg.sender] -= _value;
49+
_balances[_to] += _value;
50+
emit Transfer(msg.sender, _to, _value);
51+
return true;
52+
}
53+
54+
function approve(address _spender, uint256 _value)
55+
public
56+
returns (bool success)
57+
{
58+
_allowed[msg.sender][_spender] = _value;
59+
emit Approval(msg.sender, _spender, _value);
60+
return true;
61+
}
62+
63+
function transferfrom(
64+
address _from,
65+
address _to,
66+
uint256 _value
67+
) public returns (bool success) {
68+
require(_value <= _balances[_from], "Not enough balance");
69+
require(_value <= _allowed[_from][msg.sender], "Not enough allowance");
70+
_balances[_from] -= _value;
71+
_balances[_to] += _value;
72+
_allowed[_from][msg.sender] -= _value;
73+
emit Transfer(_from, _to, _value);
74+
return true;
75+
}
76+
}

‎chapter18/token.sol

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
pragma solidity ^0.8.0;
2+
3+
contract token {
4+
mapping(address => uint256) public coinBalanceOf;
5+
event CoinTransfer(address sender, address receiver, uint256 amount);
6+
7+
/* Initializes contract with initial supply tokens to the creator of the contract */
8+
function tokenx(uint256 supply) public {
9+
supply = 1000;
10+
coinBalanceOf[msg.sender] = supply;
11+
}
12+
13+
/* Very simple trade function */
14+
function sendCoin(address receiver, uint256 amount)
15+
public
16+
returns (bool sufficient)
17+
{
18+
if (coinBalanceOf[msg.sender] < amount) return false;
19+
coinBalanceOf[msg.sender] -= amount;
20+
coinBalanceOf[receiver] += amount;
21+
emit CoinTransfer(msg.sender, receiver, amount);
22+
return true;
23+
}
24+
}

‎chapter23/SimpleIOT.abi

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[
2+
{
3+
"constant": false,
4+
"inputs": [
5+
{
6+
"name": "x",
7+
"type": "uint8"
8+
}
9+
],
10+
"name": "getRent",
11+
"outputs": [
12+
{
13+
"name": "",
14+
"type": "bool"
15+
}
16+
],
17+
"payable": false,
18+
"stateMutability": "nonpayable",
19+
"type": "function"
20+
},
21+
{
22+
"anonymous": false,
23+
"inputs": [
24+
{
25+
"indexed": false,
26+
"name": "returnValue",
27+
"type": "bool"
28+
}
29+
],
30+
"name": "roomRented",
31+
"type": "event"
32+
}
33+
]

‎chapter23/SimpleIOT.sol

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
pragma solidity ^0.4.0;
2+
3+
contract simpleIOT {
4+
uint256 roomrent = 10;
5+
event roomRented(bool returnValue);
6+
7+
function getRent(uint8 x) public returns (bool) {
8+
if (x == roomrent) {
9+
emit roomRented(true);
10+
return true;
11+
}
12+
}
13+
}

‎chapter23/index.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var Web3 = require('web3');
2+
if (typeof web3 !== 'undefined') {
3+
web3 = new Web3(web3.currentProvider);
4+
} else {
5+
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:9002"));
6+
//http-rpc-port
7+
}
8+
var Gpio = require('onoff').Gpio;
9+
var led = new Gpio(21, 'out');
10+
var coinbase = web3.eth.coinbase;
11+
var ABIString = '[{"constant":false,"inputs":[{"name":"x","type":"uint8"}],"name":"getRent","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"returnValue","type":"bool"}],"name":"roomRented","type":"event"}]';
12+
var ABI = JSON.parse(ABIString);
13+
var ContractAddress = '0x975881c44fbef4573fef33cccec1777a8f76669c';
14+
web3.eth.defaultAccount = web3.eth.accounts[0];
15+
var simpleiot = web3.eth.contract(ABI).at(ContractAddress);
16+
var event = simpleiot.roomRented({}, function (error, result) {
17+
if (!error) {
18+
console.log("LED On");
19+
led.writeSync(1);
20+
}
21+
});

0 commit comments

Comments
 (0)
Please sign in to comment.