-
Notifications
You must be signed in to change notification settings - Fork 2
/
tail-offchain.js
127 lines (114 loc) · 3.96 KB
/
tail-offchain.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
125
126
127
require('dotenv').config()
const { inspect } = require('util')
const { getLogger } = require('@phala/sdk')
const R = require('ramda');
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
async function main() {
const argv = require('arg')({
'--pruntime': String,
'--loggerContractId': String,
'--systemContractId': String,
'--skip': [String],
'--interval': Number,
'-f': Boolean,
'--abi': String,
'--type': String,
'--topic': String,
'--blockNumber': Number,
'--inspect': Boolean,
})
if (!argv['--pruntime']) {
console.log('You neeed specific the target pruntime with --pruntime')
return process.exit(1)
}
if (!argv['--loggerContractId']) {
console.log('You neeed specific the logger contract id with --loggerContractId')
return process.exit(1)
}
if (argv['--skip'] && argv['--type']) {
console.log('You can only specific one of --skip and --type')
return process.exit(1)
}
let type = ['Log', 'MessageOutput', 'QueryIn', 'Event']
if (argv['--skip']) {
if (typeof argv.skip === 'string') {
type = R.filter(i => i !== argv.skip, type)
} else {
type = R.without(argv.skip, type)
}
} else if (argv['--type'] && R.includes(argv['--type'], type)) {
type = argv['--type']
} else if (argv['--topic']) {
type = 'Event'
}
const polling = argv['-f']
const intervalMs = argv['--interval'] || 1500
const contractId = argv._[0]
const blockNumber = Number(argv['--blockNumber'])
//
// END: parse arguments
//
const pinkLogger = await getLogger({
pruntimeURL: argv['--pruntime'],
contractId: argv['--loggerContractId'],
systemContract: argv['--systemContractId'],
})
const query = {
contract: contractId,
type,
topic: argv['--topic'],
abi: argv['--abi'] ? fs.readFileSync(argv['--abi'], 'utf-8') : null,
}
let lastSequence = -1
while (true) {
const { records } = await pinkLogger.tail(10000, query)
if (records) {
const newRecords = R.filter(i => i.sequence > lastSequence, records)
if (newRecords.length > 0) {
const last = R.last(R.map(R.prop('sequence'), newRecords))
if (last) {
lastSequence = last
for (let rec of newRecords) {
if (blockNumber && rec.blockNumber !== blockNumber) {
continue
}
if (argv['--inspect']) {
console.log(inspect(rec, false, null, true))
continue
}
if (rec['type'] === 'Log') {
const d = new Date(rec['timestamp'])
console.log(`${rec['type']} #${rec['blockNumber']} [${d.toISOString()}] ${rec['message']}`)
} else if (rec['type'] === 'MessageOutput') {
console.log(`${rec['type']} #${rec['blockNumber']} ${JSON.stringify(rec['output'])}`)
} else if (rec['type'] === 'Event') {
if (rec.decoded) {
const args = rec.decoded.args.map((i, idx) => `${rec.decoded.event.args[idx].name}=${i.toHuman()}`)
console.log(`${rec['type']} #${rec['blockNumber']} contract=[${rec['contract']}] ${rec.decoded.event.identifier} \{${args.join(", ")}\}`)
} else {
console.log(`${rec['type']} #${rec['blockNumber']} contract=[${rec['contract']}] ${JSON.stringify(rec['topics'])}`)
}
} else {
console.log(`${rec['type']} ${JSON.stringify(rec)}`)
}
}
}
}
}
if (!polling) {
break
}
await sleep(intervalMs)
}
// await waitReady()
// const keyring = new Keyring({ type: 'sr25519' })
// const pair = keyring.addFromUri('//Alice')
// const phactory = createPruntimeClient(argv['--pruntime'])
// const pinkLogger = new PinkLoggerContractPromise(phactory, argv['--remotePubkey'], pair, argv['--loggerContractId'], argv['--systemContractId'])
}
main().then(() => process.exit(0)).catch(err => {
console.error(err)
process.exit(1)
})