-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-test.js
124 lines (93 loc) · 2.81 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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));
})
}, 5000);
})
}
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;
//create transaction
postTransact({})
.then(postTransactResponse => {
console.log('postTransactResponse (Create Transation)',
postTransactResponse);
toAccountData = postTransactResponse.transaction.data.accountData;
return getMine();
}).then(getMineResponse => {
console.log('getMineResponse', getMineResponse);
//transaction to address that has just created in create transaction
return postTransact({to: toAccountData.address, value:20});
})
.then(postTransactResponse2 => {
console.log('postTransactResponse2 (Standard Transation)',
postTransactResponse2);
const key = 'foo';
const value = 'bar';
const code = [PUSH, value, PUSH, key, STORE, PUSH, key, LOAD, STOP];
// const code = [PUSH, value, PUSH, key, STORE, STOP];
// const code = [PUSH, value, STOP];
//create smart contract address
return postTransact({ code });
})
.then(postTransactResponse3 => {
console.log(
`postTransactResponse3 (smart contract)`,
postTransactResponse3
);
smartContractAccountData = postTransactResponse3.transaction.data.accountData;
return getMine();
})
.then(getMineResponse2 => {
console.log('getMineResponse2', getMineResponse2);
return postTransact({
to: smartContractAccountData.codeHash,
value: 0,
gasLimit:100
});
})
.then(postTransactResponse4 => {
console.log(
`postTransactResponse4 (to the smart contract)`,
postTransactResponse4
);
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);
});