83 |
84 |
85 |
86 |
87 |
--------------------------------------------------------------------------------
/tutorial-22/contracts/MyToken.sol:
--------------------------------------------------------------------------------
1 | pragma solidity ^0.4.0;
2 |
3 | import "./Token.sol";
4 | import "./ERC20.sol";
5 | import "./ERC223.sol";
6 | import "./ERC223ReceivingContract.sol";
7 | import "./SafeMath.sol";
8 | import "./Addresses.sol";
9 |
10 | contract MyToken is Token("MFT", "My Token", 0, 10000), ERC20, ERC223 {
11 |
12 | using SafeMath for uint;
13 | using Addresses for address;
14 |
15 | function MyToken()
16 | public {
17 | _balanceOf[msg.sender] = _totalSupply;
18 | }
19 |
20 | function totalSupply()
21 | public
22 | view
23 | returns (uint) {
24 | return _totalSupply;
25 | }
26 |
27 | function balanceOf(address _addr)
28 | public
29 | view
30 | returns (uint) {
31 | return _balanceOf[_addr];
32 | }
33 |
34 | function transfer(address _to, uint _value)
35 | public
36 | returns (bool) {
37 | return transfer(_to, _value, "");
38 | }
39 |
40 | function transfer(address _to, uint _value, bytes _data)
41 | public
42 | returns (bool) {
43 | if (_value > 0 &&
44 | _value <= _balanceOf[msg.sender]) {
45 |
46 | if (_to.isContract()) {
47 | ERC223ReceivingContract _contract = ERC223ReceivingContract(_to);
48 | _contract.tokenFallback(msg.sender, _value, _data);
49 | }
50 |
51 | _balanceOf[msg.sender] = _balanceOf[msg.sender].sub(_value);
52 | _balanceOf[_to] = _balanceOf[_to].add(_value);
53 |
54 | return true;
55 | }
56 | return false;
57 | }
58 |
59 | function transferFrom(address _from, address _to, uint _value)
60 | public
61 | returns (bool) {
62 | return transferFrom(_from, _to, _value, "");
63 | }
64 |
65 | function transferFrom(address _from, address _to, uint _value, bytes _data)
66 | public
67 | returns (bool) {
68 | if (_allowances[_from][msg.sender] > 0 &&
69 | _value > 0 &&
70 | _allowances[_from][msg.sender] >= _value &&
71 | _balanceOf[_from] >= _value) {
72 |
73 | _allowances[_from][msg.sender] -= _value;
74 |
75 | if (_to.isContract()) {
76 | ERC223ReceivingContract _contract = ERC223ReceivingContract(_to);
77 | _contract.tokenFallback(msg.sender, _value, _data);
78 | }
79 |
80 | _balanceOf[_from] = _balanceOf[_from].sub(_value);
81 | _balanceOf[_to] = _balanceOf[_to].add(_value);
82 |
83 | Transfer(_from, _to, _value);
84 |
85 | return true;
86 | }
87 | return false;
88 | }
89 |
90 | function approve(address _spender, uint _value)
91 | public
92 | returns (bool) {
93 | if (_balanceOf[msg.sender] >= _value) {
94 | _allowances[msg.sender][_spender] = _value;
95 | Approval(msg.sender, _spender, _value);
96 | return true;
97 | }
98 | return false;
99 | }
100 |
101 | function allowance(address _owner, address _spender)
102 | public
103 | view
104 | returns (uint) {
105 | if (_allowances[_owner][_spender] < _balanceOf[_owner]) {
106 | return _allowances[_owner][_spender];
107 | }
108 | return _balanceOf[_owner];
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/tutorial-25/contracts/MultiSigWallet.sol:
--------------------------------------------------------------------------------
1 | pragma solidity ^0.4.0;
2 |
3 | contract MultiSigWallet {
4 |
5 | address private _owner;
6 | mapping(address => uint8) private _owners;
7 |
8 | uint constant MIN_SIGNATURES = 2;
9 | uint private _transactionIdx;
10 |
11 | struct Transaction {
12 | address from;
13 | address to;
14 | uint amount;
15 | uint8 signatureCount;
16 | mapping (address => uint8) signatures;
17 | }
18 |
19 | mapping (uint => Transaction) private _transactions;
20 | uint[] private _pendingTransactions;
21 |
22 | modifier isOwner() {
23 | require(msg.sender == _owner);
24 | _;
25 | }
26 |
27 | modifier validOwner() {
28 | require(msg.sender == _owner || _owners[msg.sender] == 1);
29 | _;
30 | }
31 |
32 | event DepositFunds(address from, uint amount);
33 | event TransactionCreated(address from, address to, uint amount, uint transactionId);
34 | event TransactionCompleted(address from, address to, uint amount, uint transactionId);
35 | event TransactionSigned(address by, uint transactionId);
36 |
37 | function MultiSigWallet()
38 | public {
39 | _owner = msg.sender;
40 | }
41 |
42 | function addOwner(address owner)
43 | isOwner
44 | public {
45 | _owners[owner] = 1;
46 | }
47 |
48 | function removeOwner(address owner)
49 | isOwner
50 | public {
51 | _owners[owner] = 0;
52 | }
53 |
54 | function ()
55 | public
56 | payable {
57 | DepositFunds(msg.sender, msg.value);
58 | }
59 |
60 | function withdraw(uint amount)
61 | public {
62 | transferTo(msg.sender, amount);
63 | }
64 |
65 | function transferTo(address to, uint amount)
66 | validOwner
67 | public {
68 | require(address(this).balance >= amount);
69 | uint transactionId = _transactionIdx++;
70 |
71 | Transaction memory transaction;
72 | transaction.from = msg.sender;
73 | transaction.to = to;
74 | transaction.amount = amount;
75 | transaction.signatureCount = 0;
76 |
77 | _transactions[transactionId] = transaction;
78 | _pendingTransactions.push(transactionId);
79 |
80 | TransactionCreated(msg.sender, to, amount, transactionId);
81 | }
82 |
83 | function getPendingTransactions()
84 | view
85 | validOwner
86 | public
87 | returns (uint[]) {
88 | return _pendingTransactions;
89 | }
90 |
91 | function signTransaction(uint transactionId)
92 | validOwner
93 | public {
94 |
95 | Transaction storage transaction = _transactions[transactionId];
96 |
97 | // Transaction must exist
98 | require(0x0 != transaction.from);
99 | // Creator cannot sign the transaction
100 | require(msg.sender != transaction.from);
101 | // Cannot sign a transaction more than once
102 | require(transaction.signatures[msg.sender] != 1);
103 |
104 | transaction.signatures[msg.sender] = 1;
105 | transaction.signatureCount++;
106 |
107 | TransactionSigned(msg.sender, transactionId);
108 |
109 | if (transaction.signatureCount >= MIN_SIGNATURES) {
110 | require(address(this).balance >= transaction.amount);
111 | transaction.to.transfer(transaction.amount);
112 | TransactionCompleted(transaction.from, transaction.to, transaction.amount, transactionId);
113 | deleteTransaction(transactionId);
114 | }
115 | }
116 |
117 | function deleteTransaction(uint transactionId)
118 | validOwner
119 | public {
120 | uint8 replace = 0;
121 | for(uint i = 0; i < _pendingTransactions.length; i++) {
122 | if (1 == replace) {
123 | _pendingTransactions[i-1] = _pendingTransactions[i];
124 | } else if (transactionId == _pendingTransactions[i]) {
125 | replace = 1;
126 | }
127 | }
128 | delete _pendingTransactions[_pendingTransactions.length - 1];
129 | _pendingTransactions.length--;
130 | delete _transactions[transactionId];
131 | }
132 |
133 | function walletBalance()
134 | constant
135 | public
136 | returns (uint) {
137 | return address(this).balance;
138 | }
139 | }
140 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Learning Solidity
2 | The companion to the Youtube tutorials
3 |
4 | ### Videos
5 |
6 | - [Learning Solidity : Tutorial 1 The Basics](https://www.youtube.com/watch?v=v_hU0jPtLto)
7 | - [Learning Solidity : Tutorial 2 Inheritance](https://www.youtube.com/watch?v=6hkmLOtIq8A)
8 | - [Learning Solidity : Tutorial 3 Custom Modifiers and Error Handling](https://www.youtube.com/watch?v=3ObTNzDM3wI)
9 | - [Learning Solidity : Tutorial 4 Imports and Libraries](https://www.youtube.com/watch?v=0Lyf_3kA3Ms)
10 | - [Learning Solidity : Tutorial 5 Event logging and Transaction Information](https://www.youtube.com/watch?v=Jlq997yOoRs)
11 | - [Learning Solidity : Tutorial 6 Data Types (Array, Mapping, Struct)](https://www.youtube.com/watch?v=8UhO3IKApSg)
12 | - [Learning Solidity : Tutorial 7 Extending String Functionality and Bytes](https://www.youtube.com/watch?v=6iiWwT0O2fY)
13 | - [Learning Solidity : Tutorial 8 Debugging Solidity Using Remix](https://www.youtube.com/watch?v=7z52hP26MFs)
14 | - [Learning Solidity : Tutorial 9 ERC20 Tokens and Creating your own Crypto Currency](https://www.youtube.com/watch?v=r7XojpIDuhA)
15 | - [Learning Solidity : Tutorial 10 ERC223 Tokens and Creating your own Crypto Currency](https://www.youtube.com/watch?v=IWC9-yGoDGs)
16 | - [Learning Solidity : Tutorial 11 Deploying Tokens and Creating your own Crypto Currency](https://www.youtube.com/watch?v=WfkPTyvOL_g)
17 | - [Learning Solidity : Tutorial 12 Functional Assembly](https://www.youtube.com/watch?v=nkGN6GwkMzU)
18 | - [Learning Solidity : Tutorial 13 Instructional Assembly](https://www.youtube.com/watch?v=axZJ2NFMH5Q)
19 | - [Learning Solidity : Tutorial 14 Transferring Ethereum between contracts](https://www.youtube.com/watch?v=ELWSKMcJfI8)
20 | - [Learning Solidity : Tutorial 15 Public vs External](https://www.youtube.com/watch?v=Ii4g38mPPlg)
21 | - [Learning Solidity : Tutorial 16 Time Based Events](https://www.youtube.com/watch?v=HGw-yalqdgs)
22 | - [Learning Solidity : Tutorial 17 Polymorphism](https://www.youtube.com/watch?v=l_E5F5qnbtk)
23 | - [Learning Solidity : Tutorial 18 Randomness and Gambling](https://www.youtube.com/watch?v=3wY5PRliphE)
24 | - [Learning Solidity : Tutorial 19 Nested Arrays and Storage](https://www.youtube.com/watch?v=zkNHRJEuYQg)
25 | - [Learning Solidity : Tutorial 20 Parameter Mapping and Multiple Return Values](https://www.youtube.com/watch?v=v3aoiTh-UVQ)
26 | - [Learning Solidity : Tutorial 21 Truffle, Atom and TestRPC](https://www.youtube.com/watch?v=YcTSilIfih0)
27 | - [Learning Solidity : Tutorial 22 Developing an ICO/Crowdsale with TDD](https://www.youtube.com/watch?v=Cow_aL7NUGY)
28 | - [Learning Solidity : Tutorial 23 State Modifiers (view, pure, constant)](https://www.youtube.com/watch?v=RKos31UueqY)
29 | - [Learning Solidity : Tutorial 24 Multisig Wallet](https://www.youtube.com/watch?v=OwavQTuHoM8)
30 | - [Learning Solidity : Tutorial 25 Multisig Wallet cont. Multi Authentication](https://www.youtube.com/watch?v=23YLeX7mpbU)
31 | - [Learning Solidity : Tutorial 26 Auditing, Security and Testing (Long, but important)](https://www.youtube.com/watch?v=LGCMZ7S_ITE)
32 | - [Learning Solidity : Tutorial 27 Getting started with browser development using Metamask](https://www.youtube.com/watch?v=eog2eYrPEu0)
33 | - [Learning Solidity : Tutorial 28 Address book on the blockchain powered by Angular](https://www.youtube.com/watch?v=bvxKICus3bw)
34 | - [Learning Solidity : Tutorial 29 What is WEI and how is Ether defined?](https://www.youtube.com/watch?v=yOfYNUQVjxk)
35 | - [Learning Solidity : Tutorial 30 Gas Explained](https://www.youtube.com/watch?v=sPrYkYk_Beo)
36 | - [Learning Solidity : Tutorial 31 Interacting with RPC using Java and web3j](https://www.youtube.com/watch?v=fzUGvU2dXxU)
37 | - [Learning Solidity : Tutorial 32 Transferring ether with Java using web3j](https://www.youtube.com/watch?v=kJ905hVbQ_E)
38 | - [Learning Solidity : Tutorial 33 Deploying and using a contract using Java and web3j](https://www.youtube.com/watch?v=ibAh04Csp0M)
39 |
40 | ### Support
41 |
42 | - [Invalid implicit conversion of arrays from storage to memory and vice-versa](https://github.com/willitscale/learning-solidity/blob/master/support/INVALID_IMPLICIT_CONVERSION_OF_ARRAYS.MD)
43 | - [UnimplementedFeatureError: Nested arrays not implemented?](https://github.com/willitscale/learning-solidity/blob/master/support/NESTED_ARRAYS_NOT_IMPLEMENTED.MD)
44 |
45 | ### Contributions
46 |
47 | - [Nikita Chebykin](https://github.com/chebykin)
48 | - [Shubham Singh](https://github.com/imshubhamsingh)
49 | - [Shubham Tatvamasi](https://github.com/ShubhamTatvamasi)
50 | - [vardhanapoorv](https://github.com/vardhanapoorv)
51 | - [Kashish Khullar](https://github.com/kashishkhullar)
52 |
53 |
--------------------------------------------------------------------------------
/tutorial-26/test/TestMultiSigWallet.js:
--------------------------------------------------------------------------------
1 | var MultiSigWallet = artifacts.require("./MultiSigWallet.sol");
2 |
3 | contract('MultiSigWallet', (accounts) => {
4 | var creatorAddress = accounts[0];
5 | var firstOwnerAddress = accounts[1];
6 | var secondOwnerAddress = accounts[2];
7 | var externalAddress = accounts[3];
8 |
9 | it('should revert the transaction of addOwner when an invalid address calls it', () => {
10 | return MultiSigWallet.deployed()
11 | .then(instance => {
12 | return instance.addOwner(firstOwnerAddress, {from:externalAddress});
13 | })
14 | .then(result => {
15 | assert.fail();
16 | })
17 | .catch(error => {
18 | assert.notEqual(error.message, "assert.fail()", "Transaction was not reverted with an invalid address");
19 | });
20 | });
21 |
22 | it('should revert the transaction of removeOwner when an invalid address calls it', () => {
23 | return MultiSigWallet.deployed()
24 | .then(instance => {
25 | return instance.removeOwner(firstOwnerAddress, {from:externalAddress});
26 | })
27 | .then(result => {
28 | assert.fail();
29 | })
30 | .catch(error => {
31 | assert.notEqual(error.message, "assert.fail()", "Transaction was not reverted with an invalid address");
32 | });
33 | });
34 |
35 | it('should not revert the transaction of owner modification by the creator address', () => {
36 | var multiSigWalletInstance;
37 | return MultiSigWallet.deployed()
38 | .then(instance => {
39 | multiSigWalletInstance = instance;
40 | return multiSigWalletInstance.removeOwner(creatorAddress);
41 | })
42 | .then(removedResult => {
43 | return multiSigWalletInstance.addOwner(creatorAddress);
44 | })
45 | .catch(error => {
46 | assert.fail("Transaction was reverted by a creator call");
47 | });
48 | });
49 |
50 | it('should revert the transaction of deleteTransaction on an invalid transaction ID', () => {
51 | return MultiSigWallet.deployed()
52 | .then(instance => {
53 | return instance.deleteTransaction(1);
54 | })
55 | .then(result => {
56 | assert.fail();
57 | })
58 | .catch(error => {
59 | assert.notEqual(error.message, "assert.fail()", "Transaction was not reverted with an invalid transaction ID passed");
60 | })
61 | });
62 |
63 | it('should revert the transaction if the creator of a pending transaction tries to sign the transaction', () => {
64 | var multiSigWalletInstance;
65 | return MultiSigWallet.deployed()
66 | .then(instance => {
67 | multiSigWalletInstance = instance;
68 | return multiSigWalletInstance.sendTransaction({from: creatorAddress, value: 1000})
69 | })
70 | .then(sendResult => {
71 | return multiSigWalletInstance.withdraw(100);
72 | })
73 | .then(withdrawResult => {
74 | return multiSigWalletInstance.signTransaction(0);
75 | })
76 | .then(signResult => {
77 | assert.fail();
78 | })
79 | .catch(error => {
80 | assert.notEqual(error.message, "assert.fail()", "Transaction was not reverted after creator signed a transaction");
81 | });
82 | });
83 |
84 | it('should revert the transaction if the signer of a pending transaction tries to sign the transaction again', () => {
85 | var multiSigWalletInstance;
86 | return MultiSigWallet.deployed()
87 | .then(instance => {
88 | multiSigWalletInstance = instance;
89 | return multiSigWalletInstance.sendTransaction({from: creatorAddress, value: 1000})
90 | })
91 | .then(transferResult => {
92 | return multiSigWalletInstance.addOwner(firstOwnerAddress);
93 | })
94 | .then(addOwnerResult => {
95 | return multiSigWalletInstance.withdraw(100, {from: firstOwnerAddress});
96 | })
97 | .then(firstWithdrawResult => {
98 | return multiSigWalletInstance.signTransaction(1);
99 | })
100 | .then(secondWithdrawResult => {
101 | return multiSigWalletInstance.signTransaction(1);
102 | })
103 | .then(signResult => {
104 | assert.fail();
105 | })
106 | .catch(error => {
107 | assert.notEqual(error.message, "assert.fail()", "Transaction was not reverted after creator signed a transaction");
108 | });
109 | });
110 |
111 | });
112 |
--------------------------------------------------------------------------------
/tutorial-26/contracts/MultiSigWallet.sol:
--------------------------------------------------------------------------------
1 | pragma solidity ^0.4.0;
2 |
3 | contract MultiSigWallet {
4 |
5 | address private _owner;
6 | mapping(address => uint8) private _owners;
7 |
8 | uint _transactionIdx;
9 | uint[] private _pendingTransactions;
10 |
11 | struct Transaction {
12 | address from;
13 | address to;
14 | uint amount;
15 | uint8 signatureCount;
16 | mapping (address => uint8) signatures;
17 | }
18 |
19 | mapping(uint => Transaction) _transactions;
20 | uint8 constant private _sigRequiredCount = 2;
21 |
22 | modifier validOwner() {
23 | require(msg.sender == _owner || _owners[msg.sender] == 1);
24 | //require(_owners[msg.sender] == 1);
25 | _;
26 | }
27 |
28 | event DepositFunds(address from, uint amount);
29 | event TransactionCreated(address from, address to, uint amount, uint transactionId);
30 | event TransactionCompleted(address from, address to, uint amount, uint transactionId);
31 | event TransactionSigned(address by, uint transactionId);
32 |
33 | function MultiSigWallet()
34 | public {
35 | // Set master contract owner
36 | _owner = msg.sender;
37 | //_owners[msg.sender] = 1;
38 | }
39 |
40 | function addOwner(address owner)
41 | // isOwner
42 | validOwner
43 | public {
44 | _owners[owner] = 1;
45 | }
46 |
47 | function removeOwner(address owner)
48 | // isOwner
49 | // The owner validation was missing
50 | validOwner
51 | public {
52 | _owners[owner] = 0;
53 | }
54 |
55 | function ()
56 | public
57 | payable {
58 | DepositFunds(msg.sender, msg.value);
59 | }
60 |
61 | function send()
62 | public
63 | payable{}
64 |
65 | function withdraw(uint amount)
66 | validOwner
67 | public {
68 | transferTo(msg.sender, amount);
69 | }
70 |
71 | function transferTo(address to, uint amount)
72 | validOwner
73 | public {
74 | require(address(this).balance >= amount);
75 | uint transactionId = _transactionIdx++;
76 | Transaction memory transaction;
77 | transaction.from = msg.sender;
78 | transaction.to = to;
79 | transaction.amount = amount;
80 | transaction.signatureCount = 0;
81 | _transactions[transactionId] = transaction;
82 | _pendingTransactions.push(transactionId);
83 | TransactionCreated(msg.sender, to, amount, transactionId);
84 | }
85 |
86 | function getActiveTransactions()
87 | validOwner
88 | view
89 | public
90 | returns (uint[]) {
91 | return _pendingTransactions;
92 | }
93 |
94 | function signTransaction(uint transactionId)
95 | validOwner
96 | public {
97 |
98 | Transaction storage transaction = _transactions[transactionId];
99 |
100 | // Transaction must exist
101 | require(0x0 != transaction.from);
102 | //Creator cannot sign this
103 | require(msg.sender != transaction.from);
104 | // Has not already signed this transaction
105 | require(transaction.signatures[msg.sender] == 0);
106 |
107 | transaction.signatures[msg.sender] = 1;
108 | transaction.signatureCount++;
109 |
110 | TransactionSigned(msg.sender, transactionId);
111 |
112 | if (transaction.signatureCount >= _sigRequiredCount) {
113 | require(address(this).balance >= transaction.amount);
114 | transaction.to.transfer(transaction.amount);
115 | TransactionCompleted(msg.sender, transaction.to, transaction.amount, transactionId);
116 | deleteTransaction(transactionId);
117 | }
118 | }
119 |
120 | function deleteTransaction(uint transactionId)
121 | validOwner
122 | public {
123 | uint8 replace = 0;
124 | require(_pendingTransactions.length > 0);
125 | for(uint i = 0; i < _pendingTransactions.length; i++) {
126 | if (1 == replace) {
127 | _pendingTransactions[i-1] = _pendingTransactions[i];
128 | } else if (_pendingTransactions[i] == transactionId) {
129 | replace = 1;
130 | }
131 | }
132 | assert(replace == 1);
133 | // Created an Overflow
134 | delete _pendingTransactions[_pendingTransactions.length - 1];
135 | _pendingTransactions.length--;
136 | delete _transactions[transactionId];
137 | }
138 |
139 | function getPendingTransactionLength()
140 | public
141 | view
142 | returns (uint) {
143 | return _pendingTransactions.length;
144 | }
145 |
146 | function walletBalance()
147 | constant
148 | public returns (uint) {
149 | return address(this).balance;
150 | }
151 | }
152 |
--------------------------------------------------------------------------------
/tutorial-33/src/main/java/youtube/solidity/learning/Main.java:
--------------------------------------------------------------------------------
1 | package youtube.solidity.learning;
2 |
3 | import org.web3j.crypto.CipherException;
4 | import org.web3j.crypto.Credentials;
5 | import org.web3j.crypto.WalletUtils;
6 | import org.web3j.protocol.Web3j;
7 | import org.web3j.protocol.core.methods.response.TransactionReceipt;
8 | import org.web3j.protocol.core.methods.response.Web3ClientVersion;
9 | import org.web3j.protocol.http.HttpService;
10 | import org.web3j.tx.RawTransactionManager;
11 | import org.web3j.tx.TransactionManager;
12 | import org.web3j.tx.Transfer;
13 | import org.web3j.utils.Convert;
14 | import youtube.solidity.learning.contracts.AddressBook;
15 |
16 | import java.io.IOException;
17 | import java.math.BigDecimal;
18 | import java.math.BigInteger;
19 | import java.util.List;
20 |
21 | public class Main {
22 |
23 | private final static String PRIVATE_KEY = "087db5d7c2647f17e4d028f65d46babac4525eb7f810fec992a3eac10cc53ae1";
24 |
25 | private final static BigInteger GAS_LIMIT = BigInteger.valueOf(6721975L);
26 | private final static BigInteger GAS_PRICE = BigInteger.valueOf(20000000000L);
27 |
28 | private final static String RECIPIENT = "0x466B6E82CD017923298Db45C5a3Db7c66Cd753C8";
29 |
30 | private final static String CONTRACT_ADDRESS = "0x2cf178c0fcf153dd0f40db1af064824a8c6751a5";
31 |
32 | public static void main(String[] args) {
33 | try {
34 | new Main();
35 | } catch (Exception e) {
36 | e.printStackTrace();
37 | }
38 | }
39 |
40 | private Main() throws Exception {
41 | Web3j web3j = Web3j.build(new HttpService());
42 |
43 | Credentials credentials = getCredentialsFromPrivateKey();
44 |
45 | AddressBook addressBook = loadContract(CONTRACT_ADDRESS, web3j, credentials);
46 |
47 | removeAddress(addressBook);
48 |
49 | printAddresses(addressBook);
50 | }
51 |
52 | private void printWeb3Version(Web3j web3j) {
53 | Web3ClientVersion web3ClientVersion = null;
54 | try {
55 | web3ClientVersion = web3j.web3ClientVersion().send();
56 | } catch (IOException e) {
57 | e.printStackTrace();
58 | }
59 | String web3ClientVersionString = web3ClientVersion.getWeb3ClientVersion();
60 | System.out.println("Web3 client version: " + web3ClientVersionString);
61 | }
62 |
63 | private Credentials getCredentialsFromWallet() throws IOException, CipherException {
64 | return WalletUtils.loadCredentials(
65 | "passphrase",
66 | "wallet/path"
67 | );
68 | }
69 |
70 | private Credentials getCredentialsFromPrivateKey() {
71 | return Credentials.create(PRIVATE_KEY);
72 | }
73 |
74 | private void transferEthereum(Web3j web3j, Credentials credentials) throws Exception {
75 | TransactionManager transactionManager = new RawTransactionManager(
76 | web3j,
77 | credentials
78 | );
79 |
80 | Transfer transfer = new Transfer(web3j, transactionManager);
81 |
82 | TransactionReceipt transactionReceipt = transfer.sendFunds(
83 | RECIPIENT,
84 | BigDecimal.ONE,
85 | Convert.Unit.ETHER,
86 | GAS_PRICE,
87 | GAS_LIMIT
88 | ).send();
89 |
90 | System.out.print("Transaction = " + transactionReceipt.getTransactionHash());
91 | }
92 |
93 | private String deployContract(Web3j web3j, Credentials credentials) throws Exception {
94 | return AddressBook.deploy(web3j, credentials, GAS_PRICE, GAS_LIMIT)
95 | .send()
96 | .getContractAddress();
97 | }
98 |
99 | private AddressBook loadContract(String contractAddress, Web3j web3j, Credentials credentials) {
100 | return AddressBook.load(contractAddress, web3j, credentials, GAS_PRICE, GAS_LIMIT);
101 | }
102 |
103 | private void addAddresses(AddressBook addressBook) throws Exception {
104 | addressBook
105 | .addAddress("0x256a04B9F02036Ed2f785D8f316806411D605285", "Tom")
106 | .send();
107 |
108 | addressBook
109 | .addAddress("0x82CDf5a3192f2930726637e9C738A78689a91Be3", "Susan")
110 | .send();
111 |
112 | addressBook
113 | .addAddress("0x95F57F1DD015ddE7Ec2CbC8212D0ae2faC9acA11", "Bob")
114 | .send();
115 | }
116 |
117 | private void printAddresses(AddressBook addressBook) throws Exception {
118 | for (Object address : addressBook.getAddresses().send()) {
119 | String addressString = address.toString();
120 | String alias = addressBook.getAlias(addressString).send();
121 | System.out.println("Address " + addressString + " aliased as " + alias);
122 | }
123 | }
124 |
125 | private void removeAddress(AddressBook addressBook) throws Exception {
126 | addressBook
127 | .removeAddress("0x256a04B9F02036Ed2f785D8f316806411D605285")
128 | .send();
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/tutorial-11/Compiled.sol:
--------------------------------------------------------------------------------
1 | pragma solidity ^0.4.0;
2 |
3 | interface ERC20 {
4 | function transferFrom(address _from, address _to, uint _value) public returns (bool);
5 | function approve(address _spender, uint _value) public returns (bool);
6 | function allowance(address _owner, address _spender) public constant returns (uint);
7 | event Approval(address indexed _owner, address indexed _spender, uint _value);
8 | }
9 |
10 | interface ERC223 {
11 | function transfer(address _to, uint _value, bytes _data) public returns (bool);
12 | event Transfer(address indexed from, address indexed to, uint value, bytes indexed data);
13 | }
14 |
15 | contract ERC223ReceivingContract {
16 | function tokenFallback(address _from, uint _value, bytes _data) public;
17 | }
18 |
19 | contract Token {
20 | string internal _symbol;
21 | string internal _name;
22 | uint8 internal _decimals;
23 | uint internal _totalSupply = 1000;
24 | mapping (address => uint) internal _balanceOf;
25 | mapping (address => mapping (address => uint)) internal _allowances;
26 |
27 | function Token(string symbol, string name, uint8 decimals, uint totalSupply) public {
28 | _symbol = symbol;
29 | _name = name;
30 | _decimals = decimals;
31 | _totalSupply = totalSupply;
32 | }
33 |
34 | function name() public constant returns (string) {
35 | return _name;
36 | }
37 |
38 | function symbol() public constant returns (string) {
39 | return _symbol;
40 | }
41 |
42 | function decimals() public constant returns (uint8) {
43 | return _decimals;
44 | }
45 |
46 | function totalSupply() public constant returns (uint) {
47 | return _totalSupply;
48 | }
49 |
50 | function balanceOf(address _addr) public constant returns (uint);
51 | function transfer(address _to, uint _value) public returns (bool);
52 | event Transfer(address indexed _from, address indexed _to, uint _value);
53 | }
54 |
55 | library SafeMath {
56 | function mul(uint256 a, uint256 b) internal pure returns (uint256) {
57 | uint256 c = a * b;
58 | assert(a == 0 || c / a == b);
59 | return c;
60 | }
61 |
62 | function div(uint256 a, uint256 b) internal pure returns (uint256) {
63 | // assert(b > 0); // Solidity automatically throws when dividing by 0
64 | uint256 c = a / b;
65 | // assert(a == b * c + a % b); // There is no case in which this doesn't hold
66 | return c;
67 | }
68 |
69 | function sub(uint256 a, uint256 b) internal pure returns (uint256) {
70 | assert(b <= a);
71 | return a - b;
72 | }
73 |
74 | function add(uint256 a, uint256 b) internal pure returns (uint256) {
75 | uint256 c = a + b;
76 | assert(c >= a);
77 | return c;
78 | }
79 | }
80 |
81 | contract MyFirstToken is Token("MFT", "My First Token", 18, 1000), ERC20, ERC223 {
82 |
83 | using SafeMath for uint;
84 |
85 | function MyFirstToken() public {
86 | _balanceOf[msg.sender] = _totalSupply;
87 | }
88 |
89 | function totalSupply() public constant returns (uint) {
90 | return _totalSupply;
91 | }
92 |
93 | function balanceOf(address _addr) public constant returns (uint) {
94 | return _balanceOf[_addr];
95 | }
96 |
97 | function transfer(address _to, uint _value) public returns (bool) {
98 | if (_value > 0 &&
99 | _value <= _balanceOf[msg.sender] &&
100 | !isContract(_to)) {
101 | _balanceOf[msg.sender] = _balanceOf[msg.sender].sub(_value);
102 | _balanceOf[_to] = _balanceOf[_to].add(_value);
103 | Transfer(msg.sender, _to, _value);
104 | return true;
105 | }
106 | return false;
107 | }
108 |
109 | function transfer(address _to, uint _value, bytes _data) public returns (bool) {
110 | if (_value > 0 &&
111 | _value <= _balanceOf[msg.sender] &&
112 | isContract(_to)) {
113 | _balanceOf[msg.sender] = _balanceOf[msg.sender].sub(_value);
114 | _balanceOf[_to] = _balanceOf[_to].add(_value);
115 | ERC223ReceivingContract _contract = ERC223ReceivingContract(_to);
116 | _contract.tokenFallback(msg.sender, _value, _data);
117 | Transfer(msg.sender, _to, _value, _data);
118 | return true;
119 | }
120 | return false;
121 | }
122 |
123 | function isContract(address _addr) private constant returns (bool) {
124 | uint codeSize;
125 | assembly {
126 | codeSize := extcodesize(_addr)
127 | }
128 | return codeSize > 0;
129 | }
130 |
131 | function transferFrom(address _from, address _to, uint _value) public returns (bool) {
132 | if (_allowances[_from][msg.sender] > 0 &&
133 | _value > 0 &&
134 | _allowances[_from][msg.sender] >= _value &&
135 | _balanceOf[_from] >= _value) {
136 | _balanceOf[_from] = _balanceOf[_from].sub(_value);
137 | _balanceOf[_to] = _balanceOf[_to].add(_value);
138 | _allowances[_from][msg.sender] = _allowances[_from][msg.sender].sub(_value);
139 | Transfer(_from, _to, _value);
140 | return true;
141 | }
142 | return false;
143 | }
144 |
145 | function approve(address _spender, uint _value) public returns (bool) {
146 | _allowances[msg.sender][_spender] = _allowances[msg.sender][_spender].add(_value);
147 | Approval(msg.sender, _spender, _value);
148 | return true;
149 | }
150 |
151 | function allowance(address _owner, address _spender) public constant returns (uint) {
152 | return _allowances[_owner][_spender];
153 | }
154 | }
155 |
--------------------------------------------------------------------------------
/tutorial-31/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/tutorial-32/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/tutorial-33/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/tutorial-31/src/main/java/youtube/solidity/learning/contracts/AddressBook.java:
--------------------------------------------------------------------------------
1 | package youtube.solidity.learning.contracts;
2 |
3 | import java.math.BigInteger;
4 | import java.util.Arrays;
5 | import java.util.Collections;
6 | import java.util.List;
7 | import java.util.concurrent.Callable;
8 | import org.web3j.abi.TypeReference;
9 | import org.web3j.abi.datatypes.Address;
10 | import org.web3j.abi.datatypes.DynamicArray;
11 | import org.web3j.abi.datatypes.Function;
12 | import org.web3j.abi.datatypes.Type;
13 | import org.web3j.abi.datatypes.Utf8String;
14 | import org.web3j.crypto.Credentials;
15 | import org.web3j.protocol.Web3j;
16 | import org.web3j.protocol.core.RemoteCall;
17 | import org.web3j.protocol.core.methods.response.TransactionReceipt;
18 | import org.web3j.tx.Contract;
19 | import org.web3j.tx.TransactionManager;
20 |
21 | /**
22 | *
Auto generated code.
23 | *
Do not modify!
24 | *
Please use the web3j command line tools,
25 | * or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the
26 | * codegen module to update.
27 | *
28 | *
Please use the web3j command line tools,
25 | * or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the
26 | * codegen module to update.
27 | *
28 | *
Please use the web3j command line tools,
25 | * or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the
26 | * codegen module to update.
27 | *
28 | *
Generated with web3j version 3.5.0.
29 | */
30 | public class AddressBook extends Contract {
31 | private static final String BINARY = "608060405234801561001057600080fd5b50610639806100206000396000f3006080604052600436106100615763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416634ba79dfe811461006657806399900d1114610089578063a39fac121461011f578063d033c45614610184575b600080fd5b34801561007257600080fd5b50610087600160a060020a03600435166101eb565b005b34801561009557600080fd5b506100aa600160a060020a0360043516610386565b6040805160208082528351818301528351919283929083019185019080838360005b838110156100e45781810151838201526020016100cc565b50505050905090810190601f1680156101115780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561012b57600080fd5b50610134610438565b60408051602080825283518183015283519192839290830191858101910280838360005b83811015610170578181015183820152602001610158565b505050509050019250505060405180910390f35b34801561019057600080fd5b5060408051602060046024803582810135601f8101859004850286018501909652858552610087958335600160a060020a03169536956044949193909101919081908401838280828437509497506104a29650505050505050565b33600090815260208190526040812054905b818110156103815733600090815260208190526040902080548290811061022057fe5b600091825260209091200154600160a060020a03848116911614156103795733600090815260208190526040902054600110801561026057506001820381105b156102e5573360009081526020819052604090208054600019840190811061028457fe5b60009182526020808320909101543383529082905260409091208054600160a060020a0390921691839081106102b657fe5b9060005260206000200160006101000a815481600160a060020a030219169083600160a060020a031602179055505b3360009081526020819052604090208054600019840190811061030457fe5b60009182526020808320909101805473ffffffffffffffffffffffffffffffffffffffff191690553382528190526040902080549061034790600019830161050a565b50336000908152600160209081526040808320600160a060020a038716845290915281206103749161052e565b610381565b6001016101fd565b505050565b336000908152600160208181526040808420600160a060020a038616855282529283902080548451600294821615610100026000190190911693909304601f8101839004830284018301909452838352606093909183018282801561042c5780601f106104015761010080835404028352916020019161042c565b820191906000526020600020905b81548152906001019060200180831161040f57829003601f168201915b50505050509050919050565b336000908152602081815260409182902080548351818402810184019094528084526060939283018282801561049757602002820191906000526020600020905b8154600160a060020a03168152600190910190602001808311610479575b505050505090505b90565b3360008181526020818152604080832080546001808201835591855283852001805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0389169081179091559484528252808320938352928152919020825161038192840190610575565b815481835581811115610381576000838152602090206103819181019083016105f3565b50805460018160011615610100020316600290046000825580601f106105545750610572565b601f01602090049060005260206000209081019061057291906105f3565b50565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106105b657805160ff19168380011785556105e3565b828001600101855582156105e3579182015b828111156105e35782518255916020019190600101906105c8565b506105ef9291506105f3565b5090565b61049f91905b808211156105ef57600081556001016105f95600a165627a7a7230582002b4cd1de8dbad4668d406cbb6ac585b72bbd2cfc54d016da10df49ff42ea9e90029";
32 |
33 | public static final String FUNC_REMOVEADDRESS = "removeAddress";
34 |
35 | public static final String FUNC_GETALIAS = "getAlias";
36 |
37 | public static final String FUNC_GETADDRESSES = "getAddresses";
38 |
39 | public static final String FUNC_ADDADDRESS = "addAddress";
40 |
41 | protected AddressBook(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
42 | super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
43 | }
44 |
45 | protected AddressBook(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
46 | super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
47 | }
48 |
49 | public RemoteCall removeAddress(String addr) {
50 | final Function function = new Function(
51 | FUNC_REMOVEADDRESS,
52 | Arrays.asList(new org.web3j.abi.datatypes.Address(addr)),
53 | Collections.>emptyList());
54 | return executeRemoteCallTransaction(function);
55 | }
56 |
57 | public RemoteCall getAlias(String addr) {
58 | final Function function = new Function(FUNC_GETALIAS,
59 | Arrays.asList(new org.web3j.abi.datatypes.Address(addr)),
60 | Arrays.>asList(new TypeReference() {}));
61 | return executeRemoteCallSingleValueReturn(function, String.class);
62 | }
63 |
64 | public RemoteCall getAddresses() {
65 | final Function function = new Function(FUNC_GETADDRESSES,
66 | Arrays.asList(),
67 | Arrays.>asList(new TypeReference>() {}));
68 | return new RemoteCall(
69 | new Callable() {
70 | @Override
71 | @SuppressWarnings("unchecked")
72 | public List call() throws Exception {
73 | List result = (List) executeCallSingleValueReturn(function, List.class);
74 | return convertToNative(result);
75 | }
76 | });
77 | }
78 |
79 | public RemoteCall addAddress(String addr, String alias) {
80 | final Function function = new Function(
81 | FUNC_ADDADDRESS,
82 | Arrays.asList(new org.web3j.abi.datatypes.Address(addr),
83 | new org.web3j.abi.datatypes.Utf8String(alias)),
84 | Collections.>emptyList());
85 | return executeRemoteCallTransaction(function);
86 | }
87 |
88 | public static RemoteCall deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
89 | return deployRemoteCall(AddressBook.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
90 | }
91 |
92 | public static RemoteCall deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
93 | return deployRemoteCall(AddressBook.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
94 | }
95 |
96 | public static AddressBook load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
97 | return new AddressBook(contractAddress, web3j, credentials, gasPrice, gasLimit);
98 | }
99 |
100 | public static AddressBook load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
101 | return new AddressBook(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
102 | }
103 | }
104 |
--------------------------------------------------------------------------------