/
test-example.js
94 lines (87 loc) · 16.4 KB
/
test-example.js
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
/* jslint node:true, mocha:true */
'use strict';
/*
* Intended to be run against the testnet via the web3 RPC API.
* e.g. might start geth like:
* geth --testnet --port 31313 --datadir e:/ethereum -rpc -rpcport 8646 -rpcapi "eth,web3,personal"
*/
var DAppTestRunner = require('./dapp-test-runner');
var tests = new DAppTestRunner('King of the Ether Throne Core');
tests.setWeb3MasterAccount('0xb028d69032b7ca02056d684e6086030aecbf6eb0');
var mocha = require('mocha');
var reporter = new mocha.reporters.Spec(tests);
// maybe some way to register contracts with the runner would be nice
var kingOfTheEtherThroneInterface = [{"constant":false,"inputs":[{"name":"amount","type":"uint256"}],"name":"sweepCommission","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"name","type":"string"},{"name":"compensationAddress","type":"address"}],"name":"claimThroneFor","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"currentClaimPrice","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"pastMonarchs","outputs":[{"name":"compensationAddress","type":"address"},{"name":"originAddress","type":"address"},{"name":"name","type":"string"},{"name":"claimPrice","type":"uint256"},{"name":"coronationTimestamp","type":"uint256"},{"name":"compensationStatus","type":"uint8"},{"name":"compensationPaid","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"name","type":"string"}],"name":"claimThrone","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"numberOfMonarchs","outputs":[{"name":"n","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"monarchNumber","type":"uint256"},{"name":"sendToOriginalOrigin","type":"bool"}],"name":"resendFailedPayment","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"currentMonarch","outputs":[{"name":"compensationAddress","type":"address"},{"name":"originAddress","type":"address"},{"name":"name","type":"string"},{"name":"claimPrice","type":"uint256"},{"name":"coronationTimestamp","type":"uint256"},{"name":"compensationStatus","type":"uint8"},{"name":"compensationPaid","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"type":"function"},{"inputs":[],"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"usurperOriginAddress","type":"address"},{"indexed":false,"name":"usurperName","type":"string"},{"indexed":false,"name":"newClaimPrice","type":"uint256"}],"name":"ThroneClaimed","type":"event"}];
var kingOfTheEtherThroneBytecode = "60606040525b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff0219169083021790555067016345785d8a000060016000508190555060e060405190810160405280600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff168152602001600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff168152602001604060405190810160405280600881526020017f5b566163616e745d000000000000000000000000000000000000000000000000815260200150815260200160008152602001428152602001600081526020016000815260200150600260005060008201518160000160006101000a81548173ffffffffffffffffffffffffffffffffffffffff0219169083021790555060208201518160010160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908302179055506040820151816002016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106101b557805160ff19168380011785556101e6565b828001600101855582156101e6579182015b828111156101e55782518260005055916020019190600101906101c7565b5b50905061021191906101f3565b8082111561020d57600081815060009055506001016101f3565b5090565b5050606082015181600301600050556080820151816004016000505560a08201518160050160006101000a81548160ff0219169083021790555060c082015181600601600050559050505b61132d8061026a6000396000f360606040523615610095576000357c0100000000000000000000000000000000000000000000000000000000900480630eb3f5a0146100da5780631d935a6b146100f257806364325ddb146101515780637842c52d14610174578063b66a323c14610278578063c8fdc891146102ce578063e32ee3c5146102f1578063e40d0ac314610312578063f2fde38b1461040d57610095565b6100d85b6100d56000368080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505061119f565b5b565b005b6100f06004808035906020019091905050611029565b005b61014f6004808035906020019082018035906020019191908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050909091908035906020019091905050610425565b005b61015e60048050506111ad565b6040518082815260200191505060405180910390f35b61018a6004808035906020019091905050611241565b604051808873ffffffffffffffffffffffffffffffffffffffff1681526020018773ffffffffffffffffffffffffffffffffffffffff168152602001806020018681526020018581526020018481526020018381526020018281038252878181546001816001161561010002031660029004815260200191508054600181600116156101000203166002900480156102635780601f1061023857610100808354040283529160200191610263565b820191906000526020600020905b81548152906001019060200180831161024657829003601f168201915b50509850505050505050505060405180910390f35b6102cc6004808035906020019082018035906020019191908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505090909190505061119f565b005b6102db600480505061118a565b6040518082815260200191505060405180910390f35b6103106004808035906020019091908035906020019091905050610d0a565b005b61031f60048050506111b6565b604051808873ffffffffffffffffffffffffffffffffffffffff1681526020018773ffffffffffffffffffffffffffffffffffffffff168152602001806020018681526020018581526020018481526020018381526020018281038252878181546001816001161561010002031660029004815260200191508054600181600116156101000203166002900480156103f85780601f106103cd576101008083540402835291602001916103f8565b820191906000526020600020905b8154815290600101906020018083116103db57829003601f168201915b50509850505050505050505060405180910390f35b6104236004808035906020019091905050611104565b005b60006000600060006000600034955060016000505486101561044657610002565b60016000505486111561045857610002565b6064600187020494508486039350600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16600260005060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161415610519576000600260005060050160006101000a81548160ff0219169083021790555060006002600050600601600050819055506105c0565b6127109250610551600260005060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1685856112eb565b9150811561057d576001600260005060050160006101000a81548160ff021916908302179055506105af565b6002600260005060050160006101000a81548160ff0219169083021790555083600a6000828282505401925050819055505b836002600050600601600050819055505b600960005080548060010182818154818355818115116106fe576007028160070283600052602060002091820191016106fd91906105f9565b808211156106f95760006000820160006101000a81549073ffffffffffffffffffffffffffffffffffffffff02191690556001820160006101000a81549073ffffffffffffffffffffffffffffffffffffffff021916905560028201600050805460018160011615610100020316600290046000825580601f1061067d57506106ba565b601f0160209004906000526020600020908101906106b9919061069b565b808211156106b5576000818150600090555060010161069b565b5090565b5b50600382016000506000905560048201600050600090556005820160006101000a81549060ff02191690556006820160005060009055506001016105f9565b5090565b5b5050509190906000526020600020906007020160005b600260005090919091506000820160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff168160000160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908302179055506001820160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff168160010160006101000a81548173ffffffffffffffffffffffffffffffffffffffff0219169083021790555060028201600050816002016000509080546001816001161561010002031660029004828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061081b5780548555610858565b8280016001018555821561085857600052602060002091601f016020900482015b8281111561085757825482559160010191906001019061083c565b5b5090506108839190610865565b8082111561087f5760008181506000905550600101610865565b5090565b505060038201600050548160030160005055600482016000505481600401600050556005820160009054906101000a900460ff168160050160006101000a81548160ff021916908302179055506006820160005054816006016000505550505060e060405190810160405280888152602001328152602001898152602001878152602001428152602001600081526020016000815260200150600260005060008201518160000160006101000a81548173ffffffffffffffffffffffffffffffffffffffff0219169083021790555060208201518160010160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908302179055506040820151816002016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106109d857805160ff1916838001178555610a09565b82800160010185558215610a09579182015b82811115610a085782518260005055916020019190600101906109ea565b5b509050610a349190610a16565b80821115610a305760008181506000905550600101610a16565b5090565b5050606082015181600301600050556080820151816004016000505560a08201518160050160006101000a81548160ff0219169083021790555060c082015181600601600050559050506002600360016000505402049050662386f26fc10000811015610aaa5780600160005081905550610be9565b67016345785d8a0000811015610ad957655af3107a40008104655af3107a400002600160005081905550610be8565b670de0b6b3a7640000811015610b0a5766038d7ea4c68000810466038d7ea4c6800002600160005081905550610be7565b678ac7230489e80000811015610b3b57662386f26fc100008104662386f26fc1000002600160005081905550610be6565b68056bc75e2d63100000811015610b6f5767016345785d8a0000810467016345785d8a000002600160005081905550610be5565b683635c9adc5dea00000811015610ba357670de0b6b3a76400008104670de0b6b3a764000002600160005081905550610be4565b69021e19e0c9bab2400000811015610bd857678ac7230489e800008104678ac7230489e8000002600160005081905550610be3565b806001600050819055505b5b5b5b5b5b5b7f66dd2fa17295ffce5da0fb78b9a7146bc2c19cfbab9752e98fd016cfde14e0de600260005060010160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff166002600050600201600050600160005054604051808473ffffffffffffffffffffffffffffffffffffffff16815260200180602001838152602001828103825284818154600181600116156101000203166002900481526020019150805460018160011615610100020316600290048015610cef5780601f10610cc457610100808354040283529160200191610cef565b820191906000526020600020905b815481529060010190602001808311610cd257829003601f168201915b505094505050505060405180910390a15b5050505050505050565b6000600060006000600060006000600260096000508a815481101561000257906000526020600020906007020160005b5060050160009054906101000a900460ff16141515610d5857610002565b60009650600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610dbd57600196508650610ec1565b600960005089815481101561000257906000526020600020906007020160005b5060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610e4057600196508650610ec0565b600960005089815481101561000257906000526020600020906007020160005b5060010160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163273ffffffffffffffffffffffffffffffffffffffff161415610ebf576001965086505b5b5b861515610ecd57610002565b600960005089815481101561000257906000526020600020906007020160005b506006016000505494508715610f5157600960005089815481101561000257906000526020600020906007020160005b5060010160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1695508550600093508350610fb7565b600960005089815481101561000257906000526020600020906007020160005b5060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16955085506161a892505a915082821015610faf57610002565b828203935083505b610fc28686866112eb565b9050801515610fd057610002565b600160096000508a815481101561000257906000526020600020906007020160005b5060050160006101000a81548160ff0219169083021790555084600a6000828282505403925050819055505b505050505050505050565b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415611100573073ffffffffffffffffffffffffffffffffffffffff1631600a60005054820111156110aa57610002565b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16600082604051809050600060405180830381858888f19350505050505b5b50565b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156111865780600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908302179055505b5b50565b6000600960005080549050905061119c565b90565b6111a98133610425565b5b50565b60016000505481565b60026000508060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16908060010160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169080600201600050908060030160005054908060040160005054908060050160009054906101000a900460ff16908060060160005054905087565b600960005081815481101561000257906000526020600020906007020160005b915090508060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16908060010160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169080600201600050908060030160005054908060040160005054908060050160009054906101000a900460ff16908060060160005054905087565b60008373ffffffffffffffffffffffffffffffffffffffff16838390604051809050600060405180830381858888f193505050509050611326565b939250505056";
tests.registerContract('KingOfTheEtherThrone', kingOfTheEtherThroneInterface, kingOfTheEtherThroneBytecode);
tests.add({
title: 'Claim Throne at Starting Price should Increase Claim Price',
steps: [
function(helper) {
this.throne = helper.createContractInstance('KingOfTheEtherThrone');
// throne costs 100, but need a bit extra to cover gas:
this.playerOneAccount = helper.createAccountWith(helper.toWei('120', 'finney'));
},
function(helper) {
if (!this.throne.address) {
console.log('throne 1 not ready yet');
helper.waitMore();
}
console.log('throne=', this.throne);
var originalClaimPrice = this.throne.currentClaimPrice();
helper.assertWeiEqual(originalClaimPrice, helper.toWei('100','finney'), 'starting claim price');
// when the first player pays the exact claim price
console.log('t1s1 claiming');
var txnHash = this.throne.claimThrone('playerOne', {
from: this.playerOneAccount,
value: originalClaimPrice,
gas: 500000
});
helper.waitForTxn(txnHash);
},
function(helper) {
var newClaimPrice = this.throne.currentClaimPrice();
helper.assertWeiEqual(newClaimPrice, helper.toWei('150','finney'),
'expected new claim price to increase by 50%');
}
]
});
tests.add({
title: 'Claim Throne below Starting Price should not Increase Claim Price but should refund',
steps: [
function(helper) {
this.throne = helper.createContractInstance('KingOfTheEtherThrone');
this.playerOneAccount = helper.createAccountWith(helper.toWei('70', 'finney'));
},
function(helper) {
if (!this.throne.address) {
console.log('throne 2 not ready yet');
helper.waitMore();
}
console.log('t2s1 throne=', this.throne);
this.originalClaimPrice = this.throne.currentClaimPrice();
helper.assertWeiEqual(this.originalClaimPrice, helper.toWei('100','finney'), 'starting claim price');
console.log('t2s1 get bal of ', this.playerOneAccount);
this.originalPlayerOneBalance = helper.web3.eth.getBalance(this.playerOneAccount);
console.log('t2s1 claiming');
// when the first player sends just 50 finney
var txnHash = this.throne.claimThrone('playerOne', {
from: this.playerOneAccount,
value: helper.fromWei('50','finney'),
gas: 500000
});
helper.waitForTxn(txnHash);
},
function(helper) {
var newClaimPrice = this.throne.currentClaimPrice();
helper.assertWeiEqual(newClaimPrice, this.originalClaimPrice,
'expected claim price to stay the same');
var newPlayerOneBalance = helper.web3.eth.getBalance(this.playerOneAccount);
helper.assertWeiRoughlyEqual(newPlayerOneBalance, this.originalPlayerOneBalance,
helper.toWei('10', 'finney'), 'expected player to only lose a little gas');
helper.assertWeiLessThan(newPlayerOneBalance, this.originalPlayerOneBalance,
'expected player to lose some funds for non-refunded gas');
}
]
});
tests.run();