-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateEditExpenditureAction.ts
224 lines (198 loc) · 6.13 KB
/
createEditExpenditureAction.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { AnyColonyClient } from '@colony/colony-js';
import { utils } from 'ethers';
import { isEqual, omit } from 'lodash';
import amplifyClient from '~amplifyClient';
import {
ColonyActionType,
ExpenditureFragment,
ExpenditurePayout,
ExpenditureSlot,
ExpenditureStatus,
ExpenditureType,
UpdateExpenditureDocument,
UpdateExpenditureMutation,
UpdateExpenditureMutationVariables,
} from '@joincolony/graphql';
import rpcProvider from '~provider';
import { ContractEvent, ContractEventsSignatures } from '@joincolony/blocks';
import {
checkActionExists,
getExpenditureDatabaseId,
mapLogToContractEvent,
toNumber,
transactionHasEvent,
writeActionFromEvent,
} from '~utils';
import { splitAmountAndFee } from '~utils/networkFee';
import {
decodeUpdatedSlot,
decodeUpdatedStatus,
} from './decodeSetExpenditureState';
import { getUpdatedExpenditureSlots } from './getUpdatedSlots';
export class NotEditActionError extends Error {
constructor() {
super('Transaction does not contain edit expenditure action');
}
}
/**
* This function gets called for both `ExpenditureStateChanged` and `ExpenditurePayoutSet` events
* It determines whether the event is part of an edit action and creates it in the DB
* Otherwise, it returns a result allowing the handler to continue processing as normal
* @TODO: Refactor once multicall limitations are resolved
*/
export const createEditExpenditureAction = async (
event: ContractEvent,
expenditure: ExpenditureFragment,
colonyClient: AnyColonyClient,
): Promise<void> => {
const { transactionHash } = event;
const hasOneTxPaymentEvent = await transactionHasEvent(
transactionHash,
ContractEventsSignatures.OneTxPaymentMade,
);
if (hasOneTxPaymentEvent) {
throw new NotEditActionError();
}
if (
!expenditure.firstEditTransactionHash ||
expenditure.firstEditTransactionHash === transactionHash
) {
/**
* If this is the first transaction containing the relevant events, it is
* part of expenditure creation
* Only subsequent events will be considered as edit actions
*/
throw new NotEditActionError();
}
const actionExists = await checkActionExists(transactionHash);
if (actionExists) {
return;
}
const { contractAddress: colonyAddress, blockNumber } = event;
const { expenditureId } = event.args;
const convertedExpenditureId = toNumber(expenditureId);
const databaseId = getExpenditureDatabaseId(
colonyAddress,
convertedExpenditureId,
);
const logs = await rpcProvider.getProviderInstance().getLogs({
fromBlock: blockNumber,
toBlock: blockNumber,
topics: [
[
utils.id(ContractEventsSignatures.ExpenditureStateChanged),
utils.id(ContractEventsSignatures.ExpenditurePayoutSetOld),
],
],
});
const actionEvents = [];
for (const log of logs) {
const mappedEvent = await mapLogToContractEvent(
log,
colonyClient.interface,
);
if (mappedEvent) {
actionEvents.push(mappedEvent);
}
}
/**
* Determine changes to the expenditure after all relevant events have been processed
*/
let updatedSlots: ExpenditureSlot[] = expenditure.slots;
let updatedStatus: ExpenditureStatus | undefined;
let shouldCreateAction = false;
for (const actionEvent of actionEvents) {
if (
actionEvent.signature === ContractEventsSignatures.ExpenditureStateChanged
) {
const { storageSlot, value } = actionEvent.args;
const keys = actionEvent.args[4];
const updatedSlot = decodeUpdatedSlot(updatedSlots, {
storageSlot,
keys,
value,
});
if (updatedSlot) {
const preUpdateSlot = updatedSlots.find(
({ id }) => id === updatedSlot?.id,
);
updatedSlots = getUpdatedExpenditureSlots(
updatedSlots,
updatedSlot.id,
updatedSlot,
);
/**
* Special case for staged expenditure
* If the only change was claim delay set to 0, we assume it was a stage release
* Otherwise, we set the flag to create an action
*/
const hasClaimDelayChangedToZero =
preUpdateSlot?.claimDelay !== '0' && updatedSlot.claimDelay === '0';
const hasOtherChanges = !isEqual(
omit(preUpdateSlot, 'claimDelay'),
omit(updatedSlot, 'claimDelay'),
);
if (
expenditure.type !== ExpenditureType.Staged ||
!hasClaimDelayChangedToZero ||
hasOtherChanges
) {
shouldCreateAction = true;
}
}
const decodedStatus = decodeUpdatedStatus(actionEvent);
if (decodedStatus) {
updatedStatus = decodedStatus;
}
} else if (
actionEvent.signature === ContractEventsSignatures.ExpenditurePayoutSetOld
) {
const {
slot,
token: tokenAddress,
amount: amountWithFee,
} = actionEvent.args;
const convertedSlot = toNumber(slot);
const existingPayouts =
updatedSlots.find((slot) => slot.id === convertedSlot)?.payouts ?? [];
const [amountLessFee, feeAmount] = await splitAmountAndFee(amountWithFee);
const updatedPayouts: ExpenditurePayout[] = [
...existingPayouts.filter(
(payout) => payout.tokenAddress !== tokenAddress,
),
{
tokenAddress,
amount: amountLessFee,
networkFee: feeAmount,
isClaimed: false,
},
];
updatedSlots = getUpdatedExpenditureSlots(updatedSlots, convertedSlot, {
payouts: updatedPayouts,
});
shouldCreateAction = true;
}
}
await amplifyClient.mutate<
UpdateExpenditureMutation,
UpdateExpenditureMutationVariables
>(UpdateExpenditureDocument, {
input: {
id: databaseId,
slots: updatedSlots,
status: updatedStatus,
},
});
if (shouldCreateAction) {
const { agent: initiatorAddress } = event.args;
await writeActionFromEvent(event, colonyAddress, {
type: ColonyActionType.EditExpenditure,
initiatorAddress,
expenditureId: databaseId,
expenditureSlotChanges: {
oldSlots: expenditure.slots,
newSlots: updatedSlots,
},
});
}
};