forked from bcnmy/hyphen-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
48 lines (40 loc) · 1.12 KB
/
util.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
import { ethers } from "ethers";
import { config } from "./config";
function toJSONRPCPayload(engine: any, method: string, params: any) {
if (!method) {
throw new Error('JSONRPC method should be specified for params: "'+ JSON.stringify(params) +'"!');
}
if(!engine.jsonRPC || engine.jsonRPC.messageId === undefined) {
throw new Error("engine object should have jsonRPC key with field 'messageId'");
}
// advance message ID
engine.jsonRPC.messageId++;
return {
jsonrpc: '2.0',
id: engine.jsonRPC.messageId,
method,
params: params || []
};
};
function isNativeAddress(address: string) : boolean {
let result: boolean = false;
if(address && address.toLowerCase() === config.NATIVE_ADDRESS) {
result = true;
}
return result;
}
const isEthersProvider = (provider: object) => {
return ethers.providers.Provider.isProvider(provider);
}
const formatMessage = (code: number, message: string) => {
return {
code,
message
};
}
export {
toJSONRPCPayload,
isNativeAddress,
isEthersProvider,
formatMessage
}