This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
poly.ts
152 lines (133 loc) · 4.42 KB
/
poly.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { Hash, HexNumber } from "@ckb-lumos/base";
import { envConfig } from "../../base/env-config";
import { MethodNotSupportError, Web3Error } from "../error";
import { GodwokenClient } from "@godwoken-web3/godwoken";
import { gwConfig } from "../../base/index";
import { Store } from "../../cache/store";
import { CACHE_EXPIRED_TIME_MILSECS } from "../../cache/constant";
import { Query } from "../../db";
import { ethTxHashToGwTxHash, gwTxHashToEthTxHash } from "../../cache/tx-hash";
import { middleware, validators } from "../validator";
import { MAX_ALLOW_SYNC_BLOCKS_DIFF } from "../constant";
const { version: web3Version } = require("../../../package.json");
export class Poly {
private rpc: GodwokenClient;
private cacheStore: Store;
private query: Query;
constructor() {
this.rpc = new GodwokenClient(
envConfig.godwokenJsonRpc,
envConfig.godwokenReadonlyJsonRpc
);
this.cacheStore = new Store(
envConfig.redisUrl,
true,
CACHE_EXPIRED_TIME_MILSECS
);
this.cacheStore.init();
this.query = new Query();
this.getGwTxHashByEthTxHash = middleware(
this.getGwTxHashByEthTxHash.bind(this),
1,
[validators.txHash]
);
this.getEthTxHashByGwTxHash = middleware(
this.getEthTxHashByGwTxHash.bind(this),
1,
[validators.txHash]
);
}
async getCreatorId(_args: []): Promise<HexNumber> {
try {
const creatorIdHex = gwConfig.accounts.polyjuiceCreator.id;
return creatorIdHex;
} catch (err: any) {
throw new Web3Error(err.message);
}
}
// from in eth_call is optional, DEFAULT_FROM_ADDRESS fills it when empty
async getDefaultFromId(_args: []): Promise<HexNumber> {
return gwConfig.accounts.defaultFrom.id;
}
async getContractValidatorTypeHash(_args: []): Promise<Hash> {
return gwConfig.backends.polyjuice.validatorScriptTypeHash;
}
async getRollupTypeHash(_args: []): Promise<Hash> {
return gwConfig.rollupCell.typeHash;
}
async getRollupConfigHash(_args: []): Promise<Hash> {
throw new MethodNotSupportError("ROLLUP_CONFIG_HASH not supported!");
}
async getEthAccountLockHash(_args: []): Promise<Hash> {
return gwConfig.eoaScripts.eth.typeHash;
}
async getChainInfo(_args: []): Promise<any> {
throw new MethodNotSupportError(
"getChainInfo is deprecated! please use poly_version"
);
}
async version() {
return {
versions: {
web3Version,
web3IndexerVersion: web3Version, // indexer and api-server should use the same version
godwokenVersion: gwConfig.nodeVersion,
},
nodeInfo: {
nodeMode: gwConfig.nodeMode,
rollupCell: gwConfig.rollupCell,
rollupConfig: gwConfig.rollupConfig,
gwScripts: gwConfig.gwScripts,
eoaScripts: gwConfig.eoaScripts,
backends: gwConfig.backends,
accounts: gwConfig.accounts,
chainId: gwConfig.web3ChainId,
},
};
}
async getGwTxHashByEthTxHash(args: [Hash]): Promise<Hash | undefined> {
const ethTxHash = args[0];
return await ethTxHashToGwTxHash(ethTxHash, this.query, this.cacheStore);
}
async getEthTxHashByGwTxHash(args: [Hash]): Promise<Hash | undefined> {
const gwTxHash = args[0];
return await gwTxHashToEthTxHash(gwTxHash, this.query, this.cacheStore);
}
async getHealthStatus(_args: []) {
const [pingNode, pingFullNode, pingRedis, isDBConnected, syncBlocksDiff] =
await Promise.all([
this.rpc.ping(),
this.rpc.pingFullNode(),
this.cacheStore.client.PING(),
this.query.isConnected(),
this.syncBlocksDiff(),
]);
const status =
pingNode === "pong" &&
pingFullNode === "pong" &&
pingRedis === "PONG" &&
isDBConnected &&
syncBlocksDiff <= MAX_ALLOW_SYNC_BLOCKS_DIFF;
return {
status,
pingNode,
pingFullNode,
pingRedis,
isDBConnected,
syncBlocksDiff,
};
}
// get block data sync status
private async syncBlocksDiff(): Promise<Number> {
const blockNum = await this.query.getTipBlockNumber();
if (blockNum == null) {
throw new Error("db tipBlockNumber is null");
}
const dbTipBlockNumber = +blockNum.toString();
const tipBlockHash = await this.rpc.getTipBlockHash();
const tipBlock = await this.rpc.getBlock(tipBlockHash);
const gwTipBlockNumber = +tipBlock.block.raw.number;
const diff = gwTipBlockNumber - dbTipBlockNumber;
return diff;
}
}