-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.txt
149 lines (109 loc) · 3.58 KB
/
script.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
What is Truffle:
Ethereum and smart contract development framework
Contains tools for:
Compiling, linking, deploying and managing contracts
Automated Testing tools
Deployment and Migration scripts
Network management for keeping track of contracts deployed on different networks
External Packages from NPM and EthPM
Console for interacting with contracts
Scripting for interacting with contracts
mkdir denvertoken
cd denvertoken
truffle unbox tutorialtoken
# run through folder structure:
## contracts
## migrations
## src
## test
## truffle.js
code contracts/DenverToken.sol
pragma solidity ^0.4.17;
contract DenverToken {
}
npm install zeppelin-solidity
import 'zeppelin-solidity/contracts/token/ERC20/StandardToken.sol';
contract DenverToken is StandardToken, Ownable {
string public name = "DenverToken";
string public symbol = "DT";
uint8 public decimals = 2;
uint public INITIAL_SUPPLY = 20000;
function DenverToken() public {
totalSupply_ = INITIAL_SUPPLY;
balances[msg.sender] = INITIAL_SUPPLY;
}
code migrations/2_deploy_contracts.js
const DenverToken = artifacts.require("DenverToken");
module.exports = function(deployer) {
deployer.deploy(DenverToken);
};
truffle compile
# Ganache
truffle migrate
truffle console
let instance = null
DenverToken.deployed().then(_instance => instance = _instance)
instance.totalSupply.call().then(result => result.toString())
instance.balanceOf.call('0x627306090abab3a6e1400e9345bc60c78a8bef57').then(result => result.toString())
instance.transfer('0xf17f52151EbEF6C7334FAD080c5704D77216b732', 1234).then(result => console.log(result.logs[0].args))
instance.balanceOf.call('0xf17f52151EbEF6C7334FAD080c5704D77216b732').then(result => result.toString())
instance.balanceOf.call('0x627306090abab3a6e1400e9345bc60c78a8bef57').then(result => result.toString())
# add ownership
Add zeppelin Ownable.sol to DenverToken
console > migrate --reset
DenverToken.deployed().then(_instance => instance = _instance)
instance.owner.call().then(result => result.toString())
DenverToken.sol
import 'zeppelin-solidity/contracts/ownership/Ownable.sol';
contract DenverToken is StandardToken, Ownable {
# now we want to add some custom functionality to our contract
console > .exit
# TESTING
code test/changeName.test.js
const DenverToken = artifacts.require('./DenverToken.sol')
contract('DenverToken', function(accounts) {
const owner = accounts[0]
const notOwner = accounts[1]
let contract = null
beforeEach( async () => {
contract = await DenverToken.new({from:owner})
})
describe('.changeName', () => {
it('it should allow the owner to change the token name', async () => {
const newName = 'New Name'
await contract.changeName(newName, {from:owner})
const currentName = await contract.name.call()
assert.equal(currentName, newName, "name did not successfully change")
})
})
})
truffle test
# Switch to DenverToken.sol
function changeName(string _name)
public
onlyOwner
returns (bool success)
{
name = _name;
return true;
}
truffle test
Add a sad path test
const isEVMException = err => (
err.toString().includes('revert')
)
...
it('it should not allow other accounts to change the token name', async () => {
const errMsg = "A non-owner was allowed to change the name"
const newName = 'New Name'
const originalName = await contract.name.call()
try {
await contract.changeName(newName, {from:notOwner})
} catch(err) {
assert(isEVMException(err), err.toString())
const currentName = await contract.name.call()
assert.equal(currentName, originalName, errMsg)
return
}
assert(false, errMsg)
})