-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_api.js
83 lines (70 loc) · 2.46 KB
/
test_api.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
import tx_util from './src/js-v-sdk/src/utils/txUtil.js'
import seed_lib from './src/libs/seed.js'
import {VSYS_PRECISION, TX_FEE, FEE_SCALE} from './src/js-v-sdk/src/constants'
var request = require('request')
//your seed here
const seed_str = 'type ethics salute ensure shove suffer jealous raise teach erode cinnamon loud orange hard snack'
//your api node for testing
var api_node_url = 'http://52.53.239.131:8080/api'
var key_pair = seed_lib.fromExistingPhrasesWithIndex(seed_str, 0).keyPair
const payment_url = api_node_url + '/vsys/broadcast/payment'
const lease_url = api_node_url + '/leasing/broadcast/lease'
const cancel_lease_url = api_node_url + '/leasing/broadcast/cancel'
const TX_TYPES = {
PAYMENT: 2,
LEASE: 3,
CANCEL_LEASE: 4
}
var solve_response = (err, res, body) => {
if (!err && res.statusCode === 200) {
console.log('response:')
console.log(body)
} else {
console.log("error: " + err)
console.log("statusCode: " + res.statusCode)
}
}
var post_tx_request = (tx_info, tx_type, tx_url) => {
var api_data = tx_util.prepareForAPI(tx_info, key_pair, tx_type)
console.log('tx data:')
console.log(api_data)
request({
url: tx_url,
method: 'POST',
json: api_data
}, solve_response)
}
var payment = (recipient, amount, attachment) => {
const payment_tx_info = {
recipient: recipient,
amount: Number(( amount * VSYS_PRECISION).toFixed(0)),
fee: TX_FEE * VSYS_PRECISION,
feeScale: FEE_SCALE,
timestamp: Date.now() * 1e6,
attachment: attachment? attachment: ''
}
post_tx_request(payment_tx_info, TX_TYPES.PAYMENT, payment_url)
}
var lease = (recipient, amount) => {
const lease_tx_info = {
recipient: recipient,
amount: Number((amount * VSYS_PRECISION).toFixed(0)),
fee: TX_FEE * VSYS_PRECISION,
feeScale: FEE_SCALE,
timestamp: Date.now() * 1e6
}
post_tx_request(lease_tx_info, TX_TYPES.LEASE, lease_url)
}
var cancel_lease = (tx_id) => {
const cancel_lease_tx_info = {
txId: tx_id,
fee: TX_FEE * VSYS_PRECISION,
feeScale: FEE_SCALE,
timestamp: Date.now() * 1e6
}
post_tx_request(cancel_lease_tx_info, TX_TYPES.CANCEL_LEASE, cancel_lease_url)
}
// you test code below:
payment('ATrvbrWqrS2Rm1sCwwgAPwhoM276wnY6kuf', 1000)
lease('ATrvbrWqrS2Rm1sCwwgAPwhoM276wnY6kuf', 1000)
// cancel_lease('4JKMXAAAfQtJrK3RZkzzXMudjkDrnFTsUBjqNSQmcK2N')