-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-test.ts
99 lines (83 loc) · 2.4 KB
/
api-test.ts
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
import axios from "axios";
import { OPCODE_MAP } from "./interpreter";
const { STOP, ADD, PUSH, STORE, LOAD } = OPCODE_MAP;
const BASE_UREL = "http://localhost:3000";
const postTransact = async ({ code, to, value, gasLimit }: any) => {
return (
await axios.post(`${BASE_UREL}/account/transact`, {
code,
to,
value,
gasLimit,
})
).data;
};
const getMine = async () => {
const getMineResponse = (await axios.get(`${BASE_UREL}/blockchain/mine`))
.data;
console.log("getMineResponse: ", getMineResponse);
return getMineResponse;
};
const getAccountBalance = async ({ address }: any = {}) => {
const getAccountBalanceResponse = (
address
? await axios.get(`${BASE_UREL}/account/balance?address=${address}`)
: await axios.get(`${BASE_UREL}/account/balance`)
).data;
console.log("getAccountBalanceResponse: ", getAccountBalanceResponse);
return getAccountBalanceResponse;
};
const main = async () => {
let smartContractAccountData: any;
const postTransactResponse = await postTransact({});
console.log(
"postTransactResponse: (Create account transaction) ",
postTransactResponse
);
let toAccountData = postTransactResponse.transaction.data.accountData;
setTimeout(async () => {
await getMine();
const postTransactResponse2 = await postTransact({
to: toAccountData.address,
value: 20,
});
console.log(
"postTransactResponse2: (Standard Transaction) ",
postTransactResponse2
);
const key = "foo";
const value = "bar";
const code = [PUSH, value, PUSH, key, STORE, PUSH, key, LOAD, STOP];
const postTransactResponse3 = await postTransact({
code,
});
console.log(
"postTransactResponse3: (Smart Contract) ",
postTransactResponse3
);
smartContractAccountData =
postTransactResponse3.transaction.data.accountData;
setTimeout(async () => {
console.log("started mining");
await getMine();
console.log("mined");
const postTransactResponse4 = await postTransact({
to: smartContractAccountData.codeHash,
value: 0,
gasLimit: 100,
});
console.log(
"postTransactResponse4: (to the smart contract) ",
postTransactResponse4
);
setTimeout(async () => {
await getMine();
const getAccountBalanceRespone = await getAccountBalance({
address: toAccountData.address,
});
const getAccountBalanceRespone2 = await getAccountBalance();
}, 3000);
}, 3000);
}, 3000);
};
main().then().catch(console.error);