forked from blocknative/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.ts
More file actions
94 lines (85 loc) · 2.36 KB
/
Copy pathutilities.ts
File metadata and controls
94 lines (85 loc) · 2.36 KB
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
import { Emitter, NotificationObject } from './interfaces'
import { networks } from './config'
export function createEmitter(): Emitter {
return {
listeners: {},
on: function (eventCode, listener) {
// check if valid eventCode
switch (eventCode) {
case 'txSent':
case 'txPool':
case 'txConfirmed':
case 'txSpeedUp':
case 'txCancel':
case 'txFailed':
case 'all':
break
default:
throw new Error(
`${eventCode} is not a valid event code, for a list of valid event codes see: https://github.com/blocknative/sdk`
)
}
// check that listener is a function
if (typeof listener !== 'function') {
throw new Error('Listener must be a function')
}
// add listener for the eventCode
this.listeners[eventCode] = listener
},
emit: function (state) {
if (this.listeners[state.eventCode]) {
return this.listeners[state.eventCode](state)
}
if (this.listeners.all) {
return this.listeners.all(state)
}
}
}
}
export function networkName(blockchain: string, id: number): string {
return networks[blockchain][id]
}
export function serverEcho(eventCode: string): boolean {
switch (eventCode) {
case 'txRequest':
case 'nsfFail':
case 'txRepeat':
case 'txAwaitingApproval':
case 'txConfirmReminder':
case 'txSendFail':
case 'txError':
case 'txUnderPriced':
case 'txSent':
return true
default:
return false
}
}
export function last(
arr: (undefined | boolean | NotificationObject)[]
): undefined | boolean | NotificationObject {
return arr.reverse()[0]
}
// isAddress and isTxid are not meant to perform real validation,
// just needs to work out if it is an address or a transaction id
// the server will do more thorough validation
export function isAddress(blockchain: string, addressOrHash: string) {
switch (blockchain) {
case 'ethereum':
return addressOrHash.length === 42
case 'bitcoin':
return addressOrHash.length !== 64
default:
return false
}
}
export function isTxid(blockchain: string, addressOrHash: string) {
switch (blockchain) {
case 'ethereum':
return addressOrHash.length === 66
case 'bitcoin':
return addressOrHash.length === 64
default:
return false
}
}