|
| 1 | +import { constants } from 'ethers'; |
| 2 | + |
| 3 | +import { mutate, query } from '~amplifyClient'; |
| 4 | +import { ContractEvent } from '~types'; |
| 5 | +import { output, verbose, notNull, writeActionFromEvent } from '~utils'; |
| 6 | +import { |
| 7 | + UpdateColonyFundsClaimDocument, |
| 8 | + UpdateColonyFundsClaimMutation, |
| 9 | + UpdateColonyFundsClaimMutationVariables, |
| 10 | + GetColonyUnclaimedFundsDocument, |
| 11 | + GetColonyUnclaimedFundsQuery, |
| 12 | + GetColonyUnclaimedFundsQueryVariables, |
| 13 | + ColonyActionType, |
| 14 | +} from '~graphql'; |
| 15 | + |
| 16 | +export default async (event: ContractEvent): Promise<void> => { |
| 17 | + const { contractAddress: colonyAddress, blockNumber } = event; |
| 18 | + const { |
| 19 | + token: tokenAddress, |
| 20 | + payoutRemainder, |
| 21 | + agent: initiatorAddress, |
| 22 | + } = event.args; |
| 23 | + |
| 24 | + verbose('Processing ColonyFundsClaimed event'); |
| 25 | + verbose('Colony Address:', colonyAddress); |
| 26 | + verbose('Token Address:', tokenAddress); |
| 27 | + verbose('Payout Remainder:', payoutRemainder.toString()); |
| 28 | + verbose('Initiator Address:', initiatorAddress); |
| 29 | + |
| 30 | + if (tokenAddress !== constants.AddressZero) { |
| 31 | + verbose('Processing non-native token claim'); |
| 32 | + const { items: unclaimedFunds } = |
| 33 | + ( |
| 34 | + await query< |
| 35 | + GetColonyUnclaimedFundsQuery, |
| 36 | + GetColonyUnclaimedFundsQueryVariables |
| 37 | + >(GetColonyUnclaimedFundsDocument, { |
| 38 | + colonyAddress, |
| 39 | + tokenAddress, |
| 40 | + upToBlock: blockNumber, |
| 41 | + }) |
| 42 | + )?.data?.listColonyFundsClaims ?? {}; |
| 43 | + |
| 44 | + const colonyHasUnclaimedFunds = unclaimedFunds?.length; |
| 45 | + output( |
| 46 | + 'Found new Transfer Claim for Token:', |
| 47 | + tokenAddress, |
| 48 | + 'by Colony:', |
| 49 | + colonyAddress, |
| 50 | + !colonyHasUnclaimedFunds |
| 51 | + ? 'but not acting upon it since all existing non-zero transactions were claimed for this token' |
| 52 | + : '', |
| 53 | + ); |
| 54 | + |
| 55 | + if (colonyHasUnclaimedFunds) { |
| 56 | + verbose('Colony has unclaimed funds. Updating claim status.'); |
| 57 | + await Promise.all( |
| 58 | + unclaimedFunds.filter(notNull).map(({ id }) => { |
| 59 | + verbose('Updating claim status for id:', id); |
| 60 | + return mutate< |
| 61 | + UpdateColonyFundsClaimMutation, |
| 62 | + UpdateColonyFundsClaimMutationVariables |
| 63 | + >(UpdateColonyFundsClaimDocument, { |
| 64 | + input: { id, isClaimed: true }, |
| 65 | + }); |
| 66 | + }), |
| 67 | + ); |
| 68 | + verbose('All unclaimed funds have been updated'); |
| 69 | + } else { |
| 70 | + verbose('No unclaimed funds found for this colony and token'); |
| 71 | + } |
| 72 | + } else { |
| 73 | + verbose('Native token claim detected. Not processing from this handler.'); |
| 74 | + output( |
| 75 | + 'Found new Transfer Claim for Token:', |
| 76 | + tokenAddress, |
| 77 | + 'by Colony:', |
| 78 | + colonyAddress, |
| 79 | + "but not acting upon it since it's a chain native token claim, and we're not handling these from here", |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + if (!payoutRemainder.isZero()) { |
| 84 | + verbose('Writing action for non-zero payout'); |
| 85 | + await writeActionFromEvent(event, colonyAddress, { |
| 86 | + type: ColonyActionType.ClaimFunds, |
| 87 | + initiatorAddress, |
| 88 | + tokenAddress, |
| 89 | + amount: payoutRemainder.toString(), |
| 90 | + }); |
| 91 | + verbose('Action written successfully'); |
| 92 | + } else { |
| 93 | + verbose('Payout remainder is zero. No action written.'); |
| 94 | + } |
| 95 | + |
| 96 | + verbose('ColonyFundsClaimed event processing completed'); |
| 97 | +}; |
0 commit comments