6
6
type RestClient ,
7
7
createDirectus ,
8
8
createItem ,
9
+ deleteItem ,
9
10
readItem ,
10
11
readItems ,
11
12
rest ,
@@ -47,6 +48,7 @@ const contributionsMutex = new Mutex();
47
48
* @param comment The comment of the contributor to the contribution.
48
49
*/
49
50
export async function processNewContribution (
51
+ sender : Address ,
50
52
txId : Hash ,
51
53
hypercertId : string ,
52
54
amount : number ,
@@ -56,7 +58,6 @@ export async function processNewContribution(
56
58
const client = getDirectusClient ( ) ;
57
59
58
60
// check if the transaction is already processed
59
- // TODO: This is failing build @baumstern
60
61
const response = await client . request (
61
62
readItems ( "contributions" , {
62
63
fields : [ "txid" ] ,
@@ -68,33 +69,8 @@ export async function processNewContribution(
68
69
} )
69
70
) ;
70
71
71
- // if the transaction is already processed, do not create a contribution
72
- if ( response . length > 0 ) {
73
- console . log (
74
- `[Directus] tx ${ txId } already processed, skipping contribution creation in Directus`
75
- ) ;
76
- return ;
77
- }
78
-
79
- // wait for the transaction to be included in a block
80
- console . log (
81
- `[Viem] waiting for tx ${ txId } to be included in a block . . .`
82
- ) ;
83
- const txReceipt = await getViemClient ( ) . waitForTransactionReceipt ( {
84
- hash : txId ,
85
- } ) ;
86
- console . log ( `[Viem] tx ${ txId } included in block ${ txReceipt . blockNumber } ` ) ;
87
-
88
- // if the transaction is reverted, do not create a contribution
89
- if ( txReceipt . status === "reverted" ) {
90
- console . log (
91
- `[Viem] tx ${ txId } reverted, skipping contribution creation in Directus`
92
- ) ;
93
- return ;
94
- }
95
-
96
72
const contribution = {
97
- sender : getAddress ( txReceipt . from ) ,
73
+ sender : getAddress ( sender ) ,
98
74
hypercert_id : hypercertId ,
99
75
amount : amount ,
100
76
txid : txId ,
@@ -153,6 +129,23 @@ export async function createContribution(contribution: Contribution) {
153
129
}
154
130
}
155
131
132
+ export async function removeContribution ( txid : Hash ) {
133
+
134
+ const client = getDirectusClient ( ) ;
135
+
136
+ try {
137
+ console . log ( `[Directus] remove contribution of tx ${ txid } . . .` ) ;
138
+
139
+ await client . request ( deleteItem ( "contributions" , txid ) ) ;
140
+ console . log (
141
+ `[Directus] contribution of tx ${ txid } removed successfully`
142
+ ) ;
143
+ } catch ( error ) {
144
+ console . error ( "[Directus] failed to remove contribution: " , error ) ;
145
+ throw new Error ( `[Directus] failed to remove contribution: ${ error } ` ) ;
146
+ }
147
+ }
148
+
156
149
/**
157
150
* Fetches the contents of the CMS `reports` collection.
158
151
* @returns A promise that resolves to an array of CMS contents.
@@ -436,10 +429,10 @@ export const getViemClient = (): PublicClient => {
436
429
if ( viemClient ) {
437
430
return viemClient ;
438
431
}
439
-
432
+
440
433
viemClient = createPublicClient ( {
441
434
chain : sepolia ,
442
- transport : http ( ) ,
435
+ transport : http ( process . env . JSON_RPC_ENDPOINT ? process . env . JSON_RPC_ENDPOINT : undefined ) ,
443
436
} ) ;
444
437
445
438
return viemClient ;
0 commit comments