forked from ava-labs/avalanche-faucet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainnetCheckService.ts
142 lines (124 loc) · 4.48 KB
/
mainnetCheckService.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
import { DynamoDBClient } from "@aws-sdk/client-dynamodb"
import { DynamoDBDocumentClient, GetCommand, PutCommand } from "@aws-sdk/lib-dynamodb"
import { getNonce } from "./utils/mainnetBalanceCheck";
const MAINNET_CHECK_TRACKER_TABLE = "mainnet_check_tracker";
const MAX_ADDRESS_CHECK_COUNT = 3;
type AddressStatus = {
checkCount: number,
lastUsedNonce: number,
}
type ResponseType = {
isValid: boolean,
internalError: boolean,
message: string
}
export class MainnetCheckService {
private readonly documentClient: DynamoDBDocumentClient
private readonly RPC: string
constructor(rpc: string) {
const ddbClient = new DynamoDBClient({ region: 'us-east-1' })
this.documentClient = DynamoDBDocumentClient.from(ddbClient)
this.RPC = rpc
}
/**
* 1. Get check count and last used nonce (address status)
* 2. If checkCount < MAX_ADDRESS_CHECK_COUNT:
* a. Asynchronously increment check count and update nonce
* b. Return success
* 3. If checkCount == MAX_ADDRESS_CHECK_COUNT
* a. Fetch current nonce from network, and last used nonce from DB
* b. diff = currentNonce - lastUsedNonce
* c. If diff > 0
* i. checkCount = max(0, checkCount - diff)
* ii. asynchronously update address status
* iii. return success
* d. If diff == 0
* i. fail
*/
async checkAddressValidity(address: string): Promise<ResponseType> {
const response: ResponseType = { isValid: false, internalError: false, message: "" };
let addressStatus = await this.getAddressStatus(address)
if (!addressStatus) {
// update address status
addressStatus = await this.updateAddressStatus(address)
if (!addressStatus) {
response.internalError = true
return response
}
}
if (addressStatus.checkCount < MAX_ADDRESS_CHECK_COUNT) {
this.updateAddressStatus(address, ++addressStatus.checkCount, addressStatus.lastUsedNonce);
response.isValid = true
} else {
const currentNonce = await getNonce(this.RPC, address)
if (!currentNonce && currentNonce !== 0) {
response.internalError = true
return response
}
const diff = currentNonce - addressStatus.lastUsedNonce
if (diff > 0) {
const updatedCheckCount = Math.max(0, addressStatus.checkCount - diff) + 1
this.updateAddressStatus(address, updatedCheckCount, currentNonce)
response.isValid = true
}
}
return response
}
// Utility
async getAddressStatus(address: string): Promise<AddressStatus | undefined> {
const params = {
TableName: MAINNET_CHECK_TRACKER_TABLE,
Key: { address }
};
const command = new GetCommand(params);
try {
const data = await this.documentClient.send(command);
if (!data.Item) {
return undefined;
}
return {
checkCount: data.Item.checkCount,
lastUsedNonce: data.Item.lastUsedNonce,
}
} catch (error) {
console.error(JSON.stringify({
date: new Date(),
type: 'GetAddressStatusError',
item: error
}));
}
}
async updateAddressStatus(address: string, checkCount: number = 0, nonce?: number): Promise<AddressStatus | undefined> {
// if nonce is not provided, fetch from network
if (!nonce && nonce !== 0) {
const currentNonce = await getNonce(this.RPC, address)
if (!currentNonce && currentNonce !== 0) {
return
}
nonce = currentNonce
}
const params = {
TableName: MAINNET_CHECK_TRACKER_TABLE,
Item: {
address,
lastUsedNonce: nonce,
checkCount,
}
};
const command = new PutCommand(params);
try {
await this.documentClient.send(command);
} catch (error) {
console.error(JSON.stringify({
date: new Date(),
type: 'PuttingAddressTrackerError',
item: error
}));
return
}
return {
checkCount,
lastUsedNonce: nonce,
}
}
}