-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.jsx
167 lines (134 loc) · 3.94 KB
/
contract.jsx
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var PureRenderMixin = React.addons.PureRenderMixin;
Contract = React.createClass({
mixins: [PureRenderMixin],
getnatspec ( abi ) {
findText = ( o ) => {
return o && o.methods &&
_.find( o.methods, ( v, k ) => {
return (new RegExp(`^${abi.name}`)).test(k);
});
};
var natspecuser = findText( this.props.natspecuser );
var natspecdev = findText( this.props.natspecdev );
return { natspecuser, natspecdev };
},
render() {
var children = this.props.abi
.filter( (abi) => { return abi.type === 'function' })
.map( ( abi ) => {
var caller = (args) => { return this.props.instance[abi.name].apply(this,args); }
return {
nav: ABISelect,
main: ABIView,
object: _.extend( abi, { callContract: caller, ...this.getnatspec(abi), setContracts: this.props.setContracts } ),
class: "abi"
};
});
children.unshift({
nav: ContractInfoSelect,
main: ContractInfoView,
object: this.props,
class: "contractInfo"
});
return (
<div className="contractView">
<TreeView children={children} class="abi" {...this.props} />
</div>
);
}
});
// Contract inspector - Information and meta Contract interaction
ContractInfoView = React.createClass({
mixins: [PureRenderMixin],
getInitialState () {
return {
state: 0
};
},
getInstances() {
if( this.props.instances.length == 0 ) {
return 'Not deployed, yet.';
} else if( this.props.instances.length == 1 ) {
return `@ ${this.props.instances[0].address}`
} else {
var options = this.props.instances.map( (i) => {
return <option value={i.address} key={i.address}>{i.address}</option>;
});
return <div>
@ <select id="instances" name="instances"> {options} </select>
</div>;
}
},
onDeploy (){
this.props.Class.new({ from: web3.eth.coinbase, data: this.props.binary }, (err, con) => {
if( err ) throw new Error(err);
if( !con.address ) return null;
this.props.setContracts( {
[this.props.name]: {
instances: {$unshift: [con]},
instance: {$set: con}
}
});
});
},
onPoint () {
this.setState({state:1});
},
onCancel() {
this.setState({state:0});
},
onOk () {
var addr = this.refs.address.getDOMNode().value;
var con = this.props.Class.at( addr );
this.props.setContracts({
[this.props.name]: {
instances: {$unshift: [con]},
instance: {$set: con}
}
});
this.setState({state:0});
},
renderNewActions() {
if( this.state.state == 0 ) {
return <div>
<button onClick={ this.onDeploy }>Deploy New</button>
<button onClick={ this.onPoint }>Point to Existing Address</button>
</div>;
} else {
return <div>
<label for="">address:
<input type="text" ref="address" />
</label>
<button onClick={this.onOk}>OK</button>
<button onClick={this.onCancel}>Cancel</button>
</div>;
}
},
render() {
return (
<div>
<div className="meta-info">
<span className="name"> { this.props.name } </span>
<span className="author"> { this.props.natspecdev.author } </span>
<span className="title"> { this.props.natspecdev.title } </span>
</div>
<div className="label"> Instance </div>
{this.getInstances()}
{this.renderNewActions()}
</div>
);
}
});
// Small navigation button to display more information for the contract
ContractInfoSelect = React.createClass({
// show address if contract is already deployed
optAddress () {
if( typeof this.props.instance === 'object' ) {
return '@' + this.props.instance.address.slice(0,7);
}
return '';
},
render () {
return <div> { this.props.name } {this.optAddress()} </div>;
}
});