-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-test.js
103 lines (82 loc) · 3.09 KB
/
api-test.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
95
96
97
98
99
100
101
102
const request = require('request');
const { OPCODE_MAP } = require('./interpreter');
const { STOP, ADD, PUSH, STORE, LOAD } = OPCODE_MAP;
const BASE_URL = 'http://localhost:3000';
const postTransact = ({ code, to, value, gasLimit }) => {
return new Promise((resolve, reject) => {
request(`${BASE_URL}/account/transact`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code, to, value, gasLimit })
}, (error, response, body) => {
return resolve(JSON.parse(body));
});
});
}
const getMine = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
request(`${BASE_URL}/blockchain/mine`, (error, response, body) => {
return resolve(JSON.parse(body));
});
}, 1000);
});
}
const getAccountBalance = ({ address } = {}) => {
return new Promise((resolve, reject) => {
request(
`${BASE_URL}/account/balance` + (address ? `?address=${address}` : ''),
(error, response, body) => {
return resolve(JSON.parse(body));
});
})
}
let toAccountData;
let smartContractAccountData
postTransact({})
.then(postTransactionResponse => {
console.log(
'postTransactionResponse (Create Account Transaction)',
postTransactionResponse
);
toAccountData = postTransactionResponse.transaction.data.accountData;
return getMine();
}).then(getMineResponse => {
console.log('getMineResponse', getMineResponse);
return postTransact({ to: toAccountData.address, value: 20 });
})
.then(postTransactionResponse2 => {
console.log('postTransactionResponse2 (Standard Transaction)', postTransactionResponse2);
const key = 'foo';
const value = 'bar'
const code = [PUSH, value, PUSH, key, STORE, PUSH, key, LOAD, STOP];
return postTransact({ code });
})
.then(postTransactionResponse3 => {
console.log('postTransactResponse3 (Smart Contract)', postTransactionResponse3);
smartContractAccountData = postTransactionResponse3.transaction.data.accountData;
return getMine();
})
.then(getMineResponse2 => {
console.log('getMineResponse2', getMineResponse2);
return postTransact({
to: smartContractAccountData.codeHash,
value: 0,
gasLimit: 100
})
})
.then(postTransactionResponse4 => {
console.log('postTransactResponse (to the Smart Contract)', postTransactionResponse4);
return getMine();
})
.then(getMineResponse3 => {
console.log('getMineResponse3', getMineResponse3);
return getAccountBalance();
})
.then(getAccountBalanceResponse => {
console.log('getAccountBalanceResponse', getAccountBalanceResponse);
return getAccountBalance({ address: toAccountData.address });
})
.then(getAccountBalanceResponse2 => {
console.log('getAccountBalanceResponse2', getAccountBalanceResponse2);
});