-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathatlas-mine.ts
136 lines (113 loc) · 3.78 KB
/
atlas-mine.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
import { BigDecimal, BigInt, log, store } from "@graphprotocol/graph-ts";
import {
Deposit as DepositEvent,
Staked,
Unstaked,
UtilizationRate,
Withdraw as WithdrawEvent,
} from "../../generated/Atlas Mine/AtlasMine";
import {
AtlasMine,
Deposit,
StakedToken,
Withdraw,
} from "../../generated/schema";
import { LOCK_PERIOD_IN_SECONDS } from "../helpers";
import { getUser, getUserOrMultisigAddress } from "../helpers/user";
import { getAddressId } from "../helpers/utils";
const ONE = BigDecimal.fromString((1e18).toString());
export function handleDeposit(event: DepositEvent): void {
const params = event.params;
const userId = params.user;
const user = getUser(userId.toHexString());
user.deposited = user.deposited.plus(params.amount);
user.save();
const lock = params.lock;
const deposit = new Deposit(getAddressId(userId, params.index));
deposit.transactionHash = event.transaction.hash;
deposit.amount = params.amount;
deposit.depositId = params.index;
deposit.startTimestamp = event.block.timestamp.times(BigInt.fromI32(1000));
deposit.endTimestamp = event.block.timestamp
.plus(BigInt.fromI32(LOCK_PERIOD_IN_SECONDS[lock]))
.times(BigInt.fromI32(1000));
deposit.lock = lock;
deposit.mine = event.address.toHexString();
deposit.user = user.id;
deposit.save();
}
export function handleStaked(event: Staked): void {
const params = event.params;
const quantity = params.amount;
let userId = getUserOrMultisigAddress(event).toHexString();
const user = getUser(userId);
user.boosts = user.boosts + quantity.toI32();
user.boost = params.currentBoost.divDecimal(ONE).toString();
user.save();
const addressId = getAddressId(params.nft, params.tokenId);
const stakedTokenId = `${userId}-${addressId}`;
let stakedToken = StakedToken.load(stakedTokenId);
if (!stakedToken) {
stakedToken = new StakedToken(stakedTokenId);
stakedToken.mine = event.address.toHexString();
stakedToken.quantity = BigInt.zero();
stakedToken.token = addressId;
stakedToken.user = user.id;
}
stakedToken.quantity = stakedToken.quantity.plus(quantity);
stakedToken.save();
}
export function handleUnstaked(event: Unstaked): void {
const params = event.params;
const quantity = params.amount;
const userId = getUserOrMultisigAddress(event).toHexString();
const user = getUser(userId);
user.boosts = user.boosts - quantity.toI32();
user.boost = params.currentBoost.divDecimal(ONE).toString();
user.save();
const stakedToken = StakedToken.load(
`${userId}-${getAddressId(params.nft, params.tokenId)}`
);
if (stakedToken) {
stakedToken.quantity = stakedToken.quantity.minus(quantity);
stakedToken.save();
if (stakedToken.quantity.toI32() == 0) {
store.remove("StakedToken", stakedToken.id);
}
}
}
export function handleUtilizationRate(event: UtilizationRate): void {
let id = event.address.toHexString();
let mine = AtlasMine.load(id);
if (!mine) {
mine = new AtlasMine(id);
}
mine.utilization = event.params.util;
mine.save();
}
export function handleWithdraw(event: WithdrawEvent): void {
let mine = event.address.toHexString();
let params = event.params;
let userId = params.user;
let id = getAddressId(userId, params.index);
let withdraw = Withdraw.load(id);
let deposit = Deposit.load(id);
if (!withdraw) {
withdraw = new Withdraw(id);
withdraw.amount = BigInt.zero();
withdraw.deposit = id;
withdraw.mine = mine;
withdraw.user = userId.toHexString();
}
if (!deposit) {
log.error("Unknown deposit: {}", [id]);
return;
}
const user = getUser(userId.toHexString());
user.deposited = user.deposited.minus(params.amount);
user.save();
deposit.withdrawal = id;
deposit.save();
withdraw.amount = withdraw.amount.plus(params.amount);
withdraw.save();
}