-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateOverdue.js
137 lines (123 loc) · 3.6 KB
/
updateOverdue.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
128
129
130
131
132
133
134
135
136
137
const { ethers } = require("ethers");
const { DefenderRelaySigner, DefenderRelayProvider } = require("defender-relay-client/lib/ethers");
const { request, gql } = require("graphql-request");
const GRAPH_URLS = {
1: "https://api.studio.thegraph.com/query/78581/union-v1-mainnet/version/latest",
10: "https://api.studio.thegraph.com/query/78581/union-finance/version/latest",
42161: "https://api.studio.thegraph.com/query/78581/union-v1-arbitrum/version/latest",
};
const addresses = {
10: {
uTokenAddress: "0xE478b5e7A423d7CDb224692d0a816CA146A744b2",
userManagerAddress: "0x8E195D65b9932185Fcc76dB5144534e0f3597628",
},
};
exports.handler = async function (event) {
// Initialize defender relayer provider and signer
const provider = new DefenderRelayProvider(event);
const signer = new DefenderRelaySigner(event, provider, { speed: "fast" });
const { chainId } = await provider.getNetwork();
console.log(`chainId: ${chainId}`);
const uToken = new ethers.Contract(addresses[chainId].uTokenAddress, uTokenABI, signer);
const userManager = new ethers.Contract(addresses[chainId].userManagerAddress, userManagerABI, signer);
const borrowQuery = gql`
query ($first: Int) {
borrow: borrowers(first: $first) {
account
}
}
`;
const borrowerQuery = await request(GRAPH_URLS[chainId], borrowQuery, {
first: 1000,
});
const borrowers = borrowerQuery.borrow;
console.log({ borrowers });
let overdueBorrowers = [],
stakers = [];
const promises = Array.from(borrowers).map(async (borrower) => {
const account = borrower.account;
const isOverdue = await uToken.checkIsOverdue(account);
if (isOverdue) {
overdueBorrowers.push(account);
}
});
await Promise.all(promises);
console.log({ overdueBorrowers });
for (let i = 0; i < overdueBorrowers.length; i++) {
const borrower = overdueBorrowers[i];
const query = gql`
query (
$first: Int
$vouchCancellationsFilter: VouchCancellation_filter
$trustLinesFilter: TrustLine_filter
$trustLinesFilter_Vouch: TrustLine_filter
) {
cancel: vouchCancellations(first: $first, where: $vouchCancellationsFilter) {
borrower
staker
}
trust: trustLines(first: $first, where: $trustLinesFilter) {
borrower
staker
}
}
`;
const variables = {
first: 1000,
trustLinesFilter: {
borrower,
},
};
const res = await request(GRAPH_URLS[chainId], query, variables);
for (let i = 0; i < res.trust.length; i++) {
const staker = res.trust[i].staker.toLowerCase();
if (stakers.indexOf(staker) == -1) {
stakers.push(staker);
}
}
}
console.log({ stakers });
if (stakers.length > 0) {
const tx = await userManager.batchUpdateFrozenInfo(stakers);
console.log(`Called updateOverdueInfo in ${tx.hash}`);
return { tx: tx.hash, overdueBorrowers, stakers };
} else {
return { overdueBorrowers, stakers };
}
};
const userManagerABI = [
{
inputs: [
{
internalType: "address[]",
name: "stakerList",
type: "address[]",
},
],
name: "batchUpdateFrozenInfo",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
];
const uTokenABI = [
{
inputs: [
{
internalType: "address",
name: "account",
type: "address",
},
],
name: "checkIsOverdue",
outputs: [
{
internalType: "bool",
name: "isOverdue",
type: "bool",
},
],
stateMutability: "view",
type: "function",
},
];