-
Notifications
You must be signed in to change notification settings - Fork 46
/
example.html
243 lines (227 loc) · 8.65 KB
/
example.html
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!doctype>
<html>
<head>
<script type="text/javascript" src="../dist/nebulas.js"></script>
<script type="text/javascript">
var HttpRequest = require("nebulas").HttpRequest;
var Neb = require("nebulas").Neb;
var Account = require("nebulas").Account;
var Transaction = require("nebulas").Transaction;
var Unit = require("nebulas").Unit;
var NVM = require("nebulas").NVM;
var neb = new Neb();
neb.setRequest(new HttpRequest("https://testnet.nebulas.io"));
var account, tx, txhash;
function createExampleAccount() {
account = Account.NewAccount();
document.getElementById('private').innerText = account.getPrivateKeyString();
document.getElementById('public').innerText = account.getPublicKeyString();
document.getElementById('address').innerText = account.getAddressString();
neb.api.getAccountState(account.getAddressString()).then(function (state) {
state = state.result || state;
document.getElementById('balance').innerText = state.balance;
document.getElementById('nonce').innerText = state.nonce;
}).catch(function (err) {
console.log("err:",err);
});
}
function generatekey() {
var passphrase = document.getElementById("passphrase").value;
if (passphrase.length == 0) {
passphrase = "passphrase";
}
var keyStr = account.toKeyString(passphrase);
document.getElementById('key').value = keyStr;
}
function fromkey() {
var passphrase = document.getElementById("passphrase").value;
if (passphrase.length == 0) {
passphrase = "passphrase";
}
var key = document.getElementById('key').value;
account.fromKey(key, passphrase);
document.getElementById('address').innerText = account.getAddressString();
}
function generateTransaction() {
neb.api.getAccountState(account.getAddressString()).then(function (state) {
state = state.result || state;
var testnetchainID = 1001;
tx = new Transaction(testnetchainID, account, account, Unit.nasToBasic(1), parseInt(state.nonce) + 1);
tx.signTransaction();
document.getElementById('transaction').value = tx.toString();
document.getElementById('rawdata').value = tx.toProtoString();
}).catch(function (err) {
console.log(err);
})
}
function submitTransaction() {
neb.api.sendRawTransaction(tx.toProtoString()).then(function (resp) {
resp = resp.result || resp;
txhash = resp.txhash;
document.getElementById('receipt').value = txhash;
}).catch(function (err) {
console.log(err);
})
}
function receiptTransaction() {
neb.api.getTransactionReceipt(txhash).then(function (resp) {
console.log(resp);
document.getElementById('receipt').value = JSON.stringify(resp);
}).catch(function(err){
document.getElementById('receipt').value = JSON.stringify(err);
});
}
function getTransactionByContract() {
neb.api.getTransactionByContract("n1sqDHGjYtX6rMqFoq5Tow3s3LqF4ZxBvE3").then(function(resp) {
document.getElementById('contract').value = JSON.stringify(resp);
}).catch(function(err){
document.getElementById('contract').value = JSON.stringify(err);
});
}
var block = {
timestamp: 0,
height: 1
};
var transaction = {
hash: "2933836c3a56ddd789464c7bd3fd92bdb1c974ac62f7b38a34bc48eb33679f52",
from: "n1dAmstUGQ3YB4EVokmRdrvvVCNfJVU5WuS",
to: "n1dAmstUGQ3YB4EVokmRdrvvVCNfJVU5WuS",
value: "10",
nonce: 1,
timestamp: 1527077193,
gasPrice: "1000000",
gasLimit: "20000"
};
function deploy() {
var source = document.getElementById('contract-source').value;
var nvm = new NVM(block, transaction);
var args = document.getElementById('deploy-args').value;
try {
var result = nvm.deploy(source, args);
if (result === undefined) {
result = "deploy success!"
}
document.getElementById('contract-result').value = result;
} catch (e) {
document.getElementById('contract-result').value = e;
}
}
function call() {
var source = document.getElementById('contract-source').value;
var nvm = new NVM(block, transaction);
var func = document.getElementById('call-func').value;
var args = document.getElementById('call-args').value;
try {
var result = nvm.call(source, func, args);
if (result === undefined) {
result = "call success!"
}
if (typeof result === "object") {
result = JSON.stringify(result);
}
document.getElementById('contract-result').value = result;
} catch (e) {
document.getElementById('contract-result').value = e;
}
}
</script>
<style>
textarea {
width: 400px;
height: 100px;
}
</style>
</head>
<body>
<h1>Demo</h1>
<h3>Simple demo for how to use Nebulas API</h3>
<div>
<button type="button" onClick="createExampleAccount();">new account</button>
</div>
<div>1. Click [new Account] button to get your Account Info</div>
<div>private key:<label id="private"></label></div>
<div>public key:<label id="public"></label></div>
<div>address:<label id="address"></label></div>
<div>state:
<span style="display: inline">
balance:<label id="balance">0</label>
nonce:<label id="nonce">0</label>
</span>
</div>
<br>
<br>
<div>
<div>2. Claim token here (limited times per day, check your account later)</div>
<a href="https://testnet.nebulas.io/claim/">https://testnet.nebulas.io/claim/</a>
</div>
<br>
<br>
<div style="display: inline-block">
<div>3. Click [generatekey] button to get local keyfiles</div>
<input type="password" id="passphrase" placeholder="passphrase"/>
<button type="button" onClick="generatekey();">generatekey</button>
<button type="button" onClick="fromkey();">fromkey</button>
</div>
<div>key file:</div>
<textarea id="key" readonly></textarea>
<br>
<br>
<br>
<div>
<div>4. Click [new transaction] to generate local signed transactions</div>
<button type="button" onClick="generateTransaction();">new transaction</button>
</div>
<div style="display: inline-block">
<div>transaction:</div>
<textarea readonly id="transaction"></textarea>
<div>transaction raw data:</div>
<textarea id="rawdata" readonly></textarea>
</div>
<br>
<br>
<br>
<div>
<div>5. Click [submit transaction] to send transaction to server and check transaction hash</div>
<button type="button" onClick="submitTransaction();">submit transaction</button>
<button type="button" onClick="receiptTransaction();">receipt transaction</button>
</div>
<div>
<div>transaction receipt:</div>
<textarea readonly id="receipt"></textarea>
</div>
<div>
<div>6. Click [check transaction] to get transaction of contract "n1sqDHGjYtX6rMqFoq5Tow3s3LqF4ZxBvE3"</div>
<button type="button" onClick="getTransactionByContract();">check transaction</button>
</div>
<div>
<div>transaction receipt:</div>
<textarea readonly id="contract"></textarea>
</div>
<br>
<br>
<br>
<div>
<div>7. Click [deploy/call contract] to mock deploy/call javascript contract</div>
<div style="display: inline-block">
<div>deploy args:</div>
<input type="text" id="deploy-args" placeholder="args:[]"/>
<button type="button" onClick="deploy();">deploy contract</button>
</div>
<div style="display: inline-block">
<div>call func:</div>
<input type="text" id="call-func" placeholder="function"/>
<div>call args:</div>
<input type="text" id="call-args" placeholder="args:[]"/>
<button type="button" onClick="call();">call contract</button>
</div>
</div>
<div>
<div>contract code:</div>
<textarea id="contract-source"></textarea>
</div>
<div>
<div>result:</div>
<textarea id="contract-result"></textarea>
</div>
</body>
</html>