This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
distributions.ts
81 lines (69 loc) · 2.16 KB
/
distributions.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
import {
INSUFFICIENT_FUNDS_MESSAGE,
INVALID_GATEWAY_REGISTERED_MESSAGE,
} from './constants';
import { Balances, Gateways, WalletAddress, mIOToken } from './types';
import {
unsafeDecrementBalance,
walletHasSufficientBalance,
} from './utilities';
export function safeDelegateDistribution({
balances,
gateways,
protocolAddress,
gatewayAddress,
delegateAddress,
qty,
}: {
balances: Balances;
gateways: Gateways;
protocolAddress: WalletAddress;
gatewayAddress: WalletAddress;
delegateAddress: WalletAddress;
qty: mIOToken;
}): void {
if (balances[protocolAddress] === null || isNaN(balances[protocolAddress])) {
throw new ContractError(`Caller balance is not defined!`);
}
if (!walletHasSufficientBalance(balances, protocolAddress, qty)) {
throw new ContractError(INSUFFICIENT_FUNDS_MESSAGE);
}
if (!gateways[gatewayAddress]) {
throw new ContractError(INVALID_GATEWAY_REGISTERED_MESSAGE);
}
if (!gateways[gatewayAddress].delegates[delegateAddress]) {
throw new ContractError('Delegate not staked on this gateway.');
}
// Increase the individual delegate's stake
gateways[gatewayAddress].delegates[delegateAddress].delegatedStake +=
qty.valueOf();
// Increase the gateway's total delegated stake
gateways[gatewayAddress].totalDelegatedStake += qty.valueOf();
unsafeDecrementBalance(balances, protocolAddress, qty);
}
export function safeGatewayStakeDistribution({
balances,
gateways,
protocolAddress,
gatewayAddress,
qty,
}: {
balances: Balances;
gateways: Gateways;
protocolAddress: WalletAddress;
gatewayAddress: WalletAddress;
qty: mIOToken;
}): void {
if (balances[protocolAddress] === null || isNaN(balances[protocolAddress])) {
throw new ContractError(`Caller balance is not defined!`);
}
if (!walletHasSufficientBalance(balances, protocolAddress, qty)) {
throw new ContractError(INSUFFICIENT_FUNDS_MESSAGE);
}
if (!gateways[gatewayAddress]) {
throw new ContractError(INVALID_GATEWAY_REGISTERED_MESSAGE);
}
// Increase the gateway's total delegated stake
gateways[gatewayAddress].operatorStake += qty.valueOf();
unsafeDecrementBalance(balances, protocolAddress, qty);
}