@@ -13,27 +13,64 @@ import {Push} from "../services/analytics/push";
1313import { AnalyticEventName } from "../services/analytics/types/events" ;
1414import updateSeoCardBounty from "src/modules/handle-seo-card" ;
1515import { savePointEvent } from "../modules/points-system/save-point-event" ;
16+ import calculateDistributedAmounts from "src/modules/calculate-distributed-amounts" ;
17+ import { Proposal } from "@taikai/dappkit" ;
18+ import { networksAttributes } from "src/db/models/networks" ;
1619
1720export const name = "getBountyClosedEvents" ;
1821export const schedule = "*/12 * * * *" ;
1922export const description = "Move to 'Closed' status the bounty" ;
2023export const author = "clarkjoao" ;
2124
22- async function updateUserPayments ( proposal , transactionHash , issueId , tokenAmount ) {
23- return Promise . all (
24- proposal . details . map ( async ( detail ) => {
25+ async function updateUserPayments ( proposal : Proposal ,
26+ transactionHash : string ,
27+ issueId : number ,
28+ tokenAmount : string | number ,
29+ chainId : number ,
30+ network : networksAttributes ,
31+ merger : string ) {
32+ const chain = await db . chains . findOne ( {
33+ where : {
34+ chainId : chainId ,
35+ }
36+ } ) ;
37+
38+ if ( ! chain ?. closeFeePercentage || ! network . mergeCreatorFeeShare || ! network . proposerFeeShare ) {
39+ logger . warn ( `Not possible to save payments for ${ issueId } on ${ chainId } as it's missing closeFeePercentage, mergeCreatorFeeShare or proposerFeeShare` ) ;
40+ return ;
41+ }
42+
43+ const distributedAmounts = calculateDistributedAmounts ( chain . closeFeePercentage ,
44+ network . mergeCreatorFeeShare ,
45+ network . proposerFeeShare ,
46+ tokenAmount ,
47+ proposal . details ) ;
48+ return Promise . all ( [
49+ db . users_payments . create ( {
50+ address : merger ,
51+ ammount : + distributedAmounts . mergerAmount . value || 0 ,
52+ issueId,
53+ transactionHash,
54+ } ) ,
55+ db . users_payments . create ( {
56+ address : proposal . creator ,
57+ ammount : + distributedAmounts . proposerAmount . value || 0 ,
58+ issueId,
59+ transactionHash,
60+ } ) ,
61+ ...distributedAmounts . proposals . map ( async ( distributed ) => {
2562 const payment = {
26- address : detail ?. [ "recipient" ] ,
27- ammount :
28- Number ( ( detail ?. [ "percentage" ] / 100 ) * + tokenAmount ) || 0 ,
63+ address : distributed . recipient ,
64+ ammount : + distributed . value || 0 ,
2965 issueId,
3066 transactionHash,
3167 }
3268 return db . users_payments . findOrCreate ( {
3369 where : payment ,
3470 defaults : payment
3571 } )
36- } ) )
72+ } )
73+ ] ) ;
3774}
3875
3976async function updateCuratorProposal ( address : string , networkId : number ) {
@@ -53,7 +90,7 @@ export async function action(block: DecodedLog, query?: EventsQuery): Promise<Ev
5390
5491 const network = await getNetwork ( chainId , address ) ;
5592 if ( ! network ) {
56- logger . warn ( NETWORK_NOT_FOUND ( name , address ) )
93+ logger . warn ( NETWORK_NOT_FOUND ( name , address ) ) ;
5794 return eventsProcessed ;
5895 }
5996
@@ -66,12 +103,12 @@ export async function action(block: DecodedLog, query?: EventsQuery): Promise<Ev
66103 } ) ;
67104
68105 if ( ! dbBounty ) {
69- logger . warn ( DB_BOUNTY_NOT_FOUND ( name , bounty . cid , network . id ) )
106+ logger . warn ( DB_BOUNTY_NOT_FOUND ( name , bounty . cid , network . id ) ) ;
70107 return eventsProcessed ;
71108 }
72109
73110 if ( dbBounty . state === "closed" ) {
74- logger . warn ( DB_BOUNTY_ALREADY_CLOSED ( name , bounty . cid , network . id ) )
111+ logger . warn ( DB_BOUNTY_ALREADY_CLOSED ( name , bounty . cid , network . id ) ) ;
75112 return eventsProcessed ;
76113 }
77114
@@ -108,7 +145,13 @@ export async function action(block: DecodedLog, query?: EventsQuery): Promise<Ev
108145
109146 sendMessageToTelegramChannels ( BOUNTY_CLOSED ( dbBounty , dbProposal , proposalId ) ) ;
110147
111- await updateUserPayments ( bounty . proposals [ + proposalId ] , block . transactionHash , dbBounty . id , bounty . tokenAmount ) ;
148+ await updateUserPayments ( bounty . proposals [ + proposalId ] ,
149+ block . transactionHash ,
150+ dbBounty . id ,
151+ bounty . tokenAmount ,
152+ chainId ,
153+ network ,
154+ transaction . from ) ;
112155 await updateCuratorProposal ( bounty . proposals [ + proposalId ] . creator , network ?. id )
113156 updateLeaderboardNfts ( )
114157 updateLeaderboardBounties ( "closed" ) ;
0 commit comments