-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkComptrollerBal.js
115 lines (100 loc) · 2.96 KB
/
checkComptrollerBal.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
const { ethers } = require("ethers");
const formatUnits = ethers.utils.formatUnits;
const parseUnits = ethers.utils.parseUnits;
const CONTRACTS = {
// mainnet
1: {
UNION_TOKEN: "0x5Dfe42eEA70a3e6f93EE54eD9C321aF07A85535C",
COMPTROLLER: "0x216dE4089dCdD7B95BC34BdCe809669C788a9A5d",
},
// optimism
10: {
UNION_TOKEN: "0xB025ee78b54B5348BD638Fe4a6D77Ec2F813f4f9",
COMPTROLLER: "0x06a31efa04453C5F9C0A711Cdb96075308C9d6E3",
},
// arbitrum
42161: {
UNION_TOKEN: "0x6DBDe0E7e563E34A53B1130D6B779ec8eD34B4B9",
COMPTROLLER: "0x641DD6258cb3E948121B10ee51594Dc2A8549fe1",
},
// optimism goerli
420: {
UNION_TOKEN: "0xa5DaCCAf7E72Be629fc0F52cD55d500Fd6fa7677",
COMPTROLLER: "0x4A89d70e17F9e765077dfF246c84B47c1181c473",
},
};
const RPC_URLS = {
// mainnet
1: "https://mainnet.infura.io/v3/{INFURA_KEY}",
// optimism
10: "https://optimism-mainnet.infura.io/v3/{INFURA_KEY}",
// arbitrum
42161: "https://arb1.arbitrum.io/rpc",
// optimism goerli
420: "https://goerli.optimism.io",
};
exports.handler = async function (payload) {
const matches = [];
const { UNION_INFURA_KEY } = payload.secrets;
const conditionRequest = payload.request.body;
const events = conditionRequest.events;
for (const evt of events) {
// add custom logic for matching here
const sentinel = evt.sentinel;
// console.log({ sentinel });
// console.log({ tx: evt.transaction });
const [match] = evt.matchReasons;
// console.log({ match });
const chainId = sentinel.chainId;
if (!CONTRACTS[chainId] || !RPC_URLS[chainId]) continue;
console.log({
UNION: CONTRACTS[chainId]["UNION_TOKEN"],
COMPTROLLER: CONTRACTS[chainId]["COMPTROLLER"],
});
const { _, amount } = match.params;
const rpc_url = RPC_URLS[chainId].replace("{INFURA_KEY}", UNION_INFURA_KEY);
const provider = new ethers.providers.JsonRpcProvider(rpc_url);
const union = new ethers.Contract(
CONTRACTS[chainId]["UNION_TOKEN"],
ERC20_ABI,
provider
);
const compBal = await union.balanceOf(CONTRACTS[chainId]["COMPTROLLER"]);
const percent = parseUnits("1").mul(amount).div(compBal.add(amount));
console.log({ percent: formatUnits(percent) });
if (percent.gte(parseUnits("0.01"))) {
// Only match when the withdrawal amount is greater than 1%
matches.push({
hash: evt.hash,
metadata: {
timestamp: parseInt(new Date().getTime() / 1000),
comptrollerBal: formatUnits(compBal),
claimAmount: formatUnits(amount),
dropPercent: formatUnits(percent),
},
});
}
}
return { matches };
};
const ERC20_ABI = [
{
inputs: [
{
internalType: "address",
name: "account",
type: "address",
},
],
name: "balanceOf",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
];