forked from ToucanProtocol/toucan-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
1268 lines (1155 loc) · 36.6 KB
/
index.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import "isomorphic-unfetch";
import { Client, createClient, gql } from "@urql/core";
import {
BigNumber,
Contract,
ContractReceipt,
ContractTransaction,
ethers,
} from "ethers";
import {
IToucanCarbonOffsets,
IToucanContractRegistry,
IToucanPoolToken,
OffsetHelper,
} from "./typechain";
import { Network, poolSymbol } from "./types";
import {
fetchAggregationsMethod,
fetchAllTCO2TokensMethod,
fetchBridgedBatchTokensMethod,
fetchBridgedBatchTokensResult,
fetchCustomQueryMethod,
fetchPoolContentsMethod,
fetchProjectByIdMethod,
fetchRedeemsMethod,
fetchTCO2TokenByFullSymbolMethod,
fetchTCO2TokenByIdMethod,
fetchTCO2TokenResult,
fetchUserBatchesMethod,
fetchUserRedeemsMethod,
fetchUserRetirementsMethod,
} from "./types/methods";
import { PairSchema } from "./types/schemas";
import { GAS_LIMIT } from "./utils";
import {
offsetHelperABI,
poolTokenABI,
tco2ABI,
toucanContractRegistryABI,
} from "./utils/ABIs";
import addresses, { IfcOneNetworksAddresses } from "./utils/addresses";
import { MUMBAI_GRAPH_API_URL, POLYGON_GRAPH_API_URL } from "./utils/graphAPIs";
class ToucanClient {
provider: ethers.providers.Provider;
signer: ethers.Wallet | ethers.Signer;
network: Network;
addresses: IfcOneNetworksAddresses;
offsetHelper: OffsetHelper;
bct: IToucanPoolToken;
nct: IToucanPoolToken;
ToucanContractRegistry: IToucanContractRegistry;
TCO2: IToucanCarbonOffsets | undefined;
graphClient: Client;
/**
*
* @param network network that you want to work on
* @param provider web3 or jsonRpc provider
* @param signer signer
*/
constructor(
network: Network,
provider: ethers.providers.Provider,
signer: ethers.Wallet | ethers.Signer
) {
this.network = network;
this.provider = provider;
this.signer = signer;
this.addresses =
this.network == "polygon" ? addresses.polygon : addresses.mumbai;
// @ts-ignore
this.offsetHelper = new ethers.Contract(
this.addresses.offsetHelper,
offsetHelperABI,
this.signer
);
// @ts-ignore
this.bct = new ethers.Contract(
this.addresses.bct,
poolTokenABI,
this.signer
);
// @ts-ignore
this.nct = new ethers.Contract(
this.addresses.nct,
poolTokenABI,
this.signer
);
// @ts-ignore
this.ToucanContractRegistry = new ethers.Contract(
this.addresses.toucanContractRegistry,
toucanContractRegistryABI,
this.signer
);
this.graphClient = createClient({
url:
this.network == "polygon"
? POLYGON_GRAPH_API_URL
: MUMBAI_GRAPH_API_URL,
requestPolicy: "network-only",
fetch: fetch,
});
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// TCO2 related methods
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
/**
*
* @description stores the ethers.Contract to a TCO2
* @param address address of TCO2 ethers.Contract to insantiate
*/
instantiateTCO2 = async (address: string): Promise<void> => {
if (!this.checkIfTCO2(address))
throw new Error(`${address} is not a TCO2 address`);
// @ts-ignore
this.TCO2 = new ethers.Contract(address, tco2ABI, this.signer);
};
/**
*
* @description retires/burns an amount of TCO2s (each represents 1 ton of CO2) to achieve offset
* @param amount amount of TCO2 to retire
* @returns retirement transaction
*/
retire = async (amount: BigNumber): Promise<ContractReceipt> => {
if (!this.TCO2)
throw new Error("You need to instantiate a TCO2 contract first");
const retirementTxn: ContractTransaction = await this.TCO2.retire(amount, {
gasLimit: GAS_LIMIT,
});
// TODO get retirementEventId ?
return await retirementTxn.wait();
};
/**
*
* @description retires/burns an amount of TCO2s & mints the NFT certificate for it within the same transaction
* @param retirementEntityName name of the entity that does the retirement (you)
* @param beneficiaryAddress address of the beneficiary (in case you're retiring for someone else)
* @param beneficiaryName name of the beneficiary
* @param retirementMessage retirement message
* @param amount amount of TCO2 to retire
* @returns retirement transaction
*/
retireAndMintCertificate = async (
retirementEntityName: string,
beneficiaryAddress: string,
beneficiaryName: string,
retirementMessage: string,
amount: BigNumber
): Promise<ContractReceipt> => {
if (!this.TCO2)
throw new Error("You need to instantiate a TCO2 contract first");
const retirementTxn: ContractTransaction =
await this.TCO2.retireAndMintCertificate(
retirementEntityName,
beneficiaryAddress,
beneficiaryName,
retirementMessage,
amount,
{ gasLimit: GAS_LIMIT }
);
return await retirementTxn.wait();
};
/**
*
* @description retires/burns an amount of TCO2s from a different address/wallet
* @notice requires approval from the address you're trying to retire from
* @param amount amount of TCO2 to retire
* @param address address of the account to retire from
* @returns retirement transaction
*/
retireFrom = async (
amount: BigNumber,
address: string
): Promise<ContractReceipt> => {
if (!this.TCO2)
throw new Error("You need to instantiate a TCO2 contract first");
const retirementTxn: ContractTransaction = await this.TCO2.retireFrom(
address,
amount,
{
gasLimit: GAS_LIMIT,
}
);
// TODO get retirementEventId ?
return await retirementTxn.wait();
};
/**
*
* @description gets the cap for TCO2s based on `totalVintageQuantity`
* @returns BigNumber representing the cap
*/
getDepositCap = async (): Promise<BigNumber> => {
if (!this.TCO2)
throw new Error("You need to instantiate a TCO2 contract first");
return await this.TCO2.getDepositCap();
};
/**
*
* @description gets the attributes of the project represented by the TCO2
* @returns an array of attributes
*/
getAttributes = async () => {
// TODO: a return TS type
if (!this.TCO2)
throw new Error("You need to instantiate a TCO2 contract first");
return await this.TCO2.getAttributes();
};
/**
*
* @description gets the remaining space in TCO2 contract before hitting the cap
* @returns BigNumber representing the remaining space
*/
getTCO2Remaining = async (): Promise<BigNumber> => {
if (!this.TCO2)
throw new Error("You need to instantiate a TCO2 contract first");
return await this.TCO2.getRemaining();
};
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Pool related methods
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
/**
*
* @description deposits TCO2s in the pool which mints a pool token for the user
* @param pool symbol of the pool (token) to use
* @param amount amount of TCO2s to deposit
* @param tco2 contract of TCO2 to deposit
* @returns deposit transaction
*/
depositTCO2 = async (
pool: poolSymbol,
amount: BigNumber,
tco2: IToucanCarbonOffsets
): Promise<ContractReceipt> => {
const poolToken = this.getPoolContract(pool);
const approveTxn: ContractTransaction = await tco2.approve(
poolToken.address,
amount
);
await approveTxn.wait();
const depositTxn: ContractTransaction = await poolToken.deposit(
tco2.address,
amount,
{ gasLimit: GAS_LIMIT }
);
return await depositTxn.wait();
};
/**
*
* @description checks if TCO2 is eligible for pool
* @param pool symbol of the pool (token) to use
* @param tco2 address of TCO2 to deposit
* @returns boolean
*/
checkEligible = async (pool: poolSymbol, tco2: string): Promise<boolean> => {
const poolToken = this.getPoolContract(pool);
return await poolToken.checkEligible(tco2);
};
/**
*
* @description calculates the fees to selectively redeem pool tokens for TCO2s
* @param pool symbol of the pool (token) to use
* @param tco2s array of TCO2 contract addresses
* @param amounts array of amounts to redeem for each tco2s
* @notice tco2s must match amounts; amounts[0] is the amount of tco2[0] token to redeem for
* @returns amount (BigNumber) of fees it will cost to redeem
*/
calculateRedeemFees = async (
pool: poolSymbol,
tco2s: string[],
amounts: BigNumber[]
): Promise<BigNumber> => {
const poolToken = this.getPoolContract(pool);
return await poolToken.calculateRedeemFees(tco2s, amounts);
};
/**
*
* @description selectively redeems pool tokens for TCO2s
* @param pool symbol of the pool (token) to use
* @param tco2s array of TCO2 contract addresses
* @param amounts array of amounts to redeem for each tco2s
* @notice tco2s must match amounts; amounts[0] is the amount of tco2[0] token to redeem for
* @returns redeem transaction
*/
redeemMany = async (
pool: poolSymbol,
tco2s: string[],
amounts: BigNumber[]
): Promise<ContractReceipt> => {
const poolToken = this.getPoolContract(pool);
const redeemTxn: ContractTransaction = await poolToken.redeemMany(
tco2s,
amounts,
{ gasLimit: GAS_LIMIT }
);
return await redeemTxn.wait();
};
/**
*
* @description automatically redeems pool tokens for TCO2s
* @param pool symbol of the pool (token) to use
* @param amount amount to redeem
* @returns redeem transaction
*/
redeemAuto = async (
pool: poolSymbol,
amount: BigNumber
): Promise<ContractReceipt> => {
const poolToken = this.getPoolContract(pool);
const redeemTxn: ContractTransaction = await poolToken.redeemAuto(amount, {
gasLimit: GAS_LIMIT,
});
return await redeemTxn.wait();
};
/**
*
* @description automatically redeems pool tokens for TCO2s
* @notice costs more gas than redeemAuto()
* @param pool symbol of the pool (token) to use
* @param amount amount to redeem
* @returns array containing tco2 addresses (string) and amounts (BigNumber)
*/
redeemAuto2 = async (
pool: poolSymbol,
amount: BigNumber
): Promise<{ address: string; amount: BigNumber }[]> => {
const poolToken = this.getPoolContract(pool);
const redeemReceipt = await (
await poolToken.redeemAuto2(amount, { gasLimit: GAS_LIMIT })
).wait();
if (!redeemReceipt.events) {
throw new Error("No events to get tco2 addresses and amounts from");
}
return redeemReceipt.events
.filter((event) => {
return (
event.event == "Redeemed" && event.args?.erc20 && event.args?.amount
);
})
.map((event) => {
return { address: event.args?.erc20, amount: event.args?.amount };
});
};
/**
*
* @description gets the remaining space in pool contract before hitting the cap
* @param tokenSymbol symbol of the token to use
* @returns BigNumber representing the remaining space
*/
getPoolRemaining = async (tokenSymbol: poolSymbol): Promise<BigNumber> => {
const poolToken = tokenSymbol == "BCT" ? this.bct : this.nct;
return await poolToken.getRemaining();
};
/**
*
* @description gets an array of scored TCO2s; scoredTCO2s[0] is lowest ranked
* @param pool symbol of the pool (token) to use
* @returns array of TCO2 addresses by rank
*/
getScoredTCO2s = async (pool: poolSymbol): Promise<string[]> => {
const poolToken = this.getPoolContract(pool);
return await poolToken.getScoredTCO2s();
};
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Contract registry related methods
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
/**
*
* @description checks if an address represents a TCO2
* @param address address of contract to check
* @returns boolean
*/
checkIfTCO2 = async (address: string): Promise<boolean> => {
return await this.ToucanContractRegistry.checkERC20(address);
};
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// OffsetHelper related methods
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
/**
*
* @description allows user to retire carbon using carbon pool tokens from his wallet
* @notice this method may take up to even 1 minute to give a result
* @param pool symbol of the pool (token) to use
* @param amount amount of CO2 tons to offset
* @returns offset transaction
*/
autoOffsetUsingPoolToken = async (
pool: poolSymbol,
amount: BigNumber
): Promise<ContractReceipt> => {
const poolToken = this.getPoolContract(pool);
const approveTxn: ContractTransaction = await poolToken.approve(
this.addresses.offsetHelper,
amount
);
await approveTxn.wait();
const offsetTxn: ContractTransaction =
await this.offsetHelper.autoOffsetUsingPoolToken(
this.addresses.nct,
amount,
{ gasLimit: GAS_LIMIT }
);
return await offsetTxn.wait();
};
/**
*
* @description swaps given token for carbon pool tokens and uses them to retire carbon
* @notice this method may take up to even 1 minute to give a result
* @param pool symbol of the pool (token) to use
* @param amount amount of CO2 tons to offset
* @param swapToken portal for the token to swap into pool tokens (only accepts WETH, WMATIC and USDC)
* @returns offset transaction
*/
autoOffsetUsingSwapToken = async (
pool: poolSymbol,
amount: BigNumber,
swapToken: Contract
): Promise<ContractReceipt> => {
const poolToken = this.getPoolContract(pool);
const approveTxn: ContractTransaction = await swapToken.approve(
this.addresses.offsetHelper,
await this.offsetHelper.calculateNeededTokenAmount(
swapToken.address,
poolToken.address,
amount
)
);
await approveTxn.wait();
const offsetTxn: ContractTransaction =
await this.offsetHelper.autoOffsetUsingToken(
swapToken.address,
poolToken.address,
amount,
{ gasLimit: GAS_LIMIT }
);
return await offsetTxn.wait();
};
/**
*
* @description swaps ETH for carbon pool tokens and uses them to retire carbon
* @notice this method may take up to even 1 minute to give a result
* @param pool symbol of the pool (token) to use
* @param amount amount of CO2 tons to offset
* @returns offset transaction
*/
autoOffsetUsingETH = async (
pool: poolSymbol,
amount: BigNumber
): Promise<ContractReceipt> => {
const poolToken = this.getPoolContract(pool);
const offsetTxn: ContractTransaction =
await this.offsetHelper.autoOffsetUsingETH(poolToken.address, amount, {
gasLimit: GAS_LIMIT,
value: await this.offsetHelper.calculateNeededETHAmount(
poolToken.address,
amount
),
});
return await offsetTxn.wait();
};
/**
*
* @description calculates the needed amount of tokens to send to offset
* @param pool symbol of the pool (token) to use
* @param amount amount of CO2 tons to calculate for
* @param swapToken contract of the token to use in swap
* @returns amount (BigNumber) of swapToken needed to deposit
*/
calculateNeededTokenAmount = async (
pool: poolSymbol,
amount: BigNumber,
swapToken: Contract
): Promise<BigNumber> => {
const poolToken = this.getPoolContract(pool);
return await this.offsetHelper.calculateNeededTokenAmount(
swapToken.address,
poolToken.address,
amount
);
};
/**
*
* @description calculates the needed amount of ETH to send to offset; ETH = native currency of network you are on
* @param pool symbol of the pool (token) to use
* @param amount amount of CO2 tons to calculate for
* @returns amount (BigNumber) of ETH needed to deposit; ETH = native currency of network you are on
*/
calculateNeededETHAmount = async (
pool: poolSymbol,
amount: BigNumber
): Promise<BigNumber> => {
const poolToken = this.getPoolContract(pool);
return await this.offsetHelper.calculateNeededETHAmount(
poolToken.address,
amount
);
};
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
// Subgraph related methods
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
/**
*
* Note: It's very important that whenever you change the gql query of any existent
* methods, you also change the return type of the method (in types/methods.ts) to
* match it.
*
*/
// --------------------------------------------------------------------------------
// Batches Subgraph Methods
// --------------------------------------------------------------------------------
/**
*
* @description fetches the batches of a user
* @param walletAddress address of user to query for
* @returns an array of BatchTokens (they contain different properties of the Batch)
*/
fetchUserBatches: fetchUserBatchesMethod = async (walletAddress) => {
const query = gql`
query ($walletAddress: String) {
users(id: $walletAddress) {
batchesOwned(orderBy: id, orderDirection: desc) {
id
tx
serialNumber
quantity
confirmationStatus
comments {
id
comment
sender {
id
}
}
creator {
id
}
}
}
}
`;
const result = await this.graphClient
.query(query, { walletAddress: walletAddress })
.toPromise();
if (result.error) throw result.error;
if (result.data?.users[0]?.batchesOwned)
return result.data.users[0].batchesOwned;
return [];
};
// --------------------------------------------------------------------------------
// TCO2Tokens Subgraph Methods
// --------------------------------------------------------------------------------
/**
*
* @description fetches properties of a TCO2
* @param id id of the TCO2 to query for; the id happens to be the same as the address e.g.: "0x004090eef602e024b2a6cb7f0c1edda992382994"
* @returns a TCO2Detail object with properties of the TCO2 (name, address, etc)
*/
fetchTCO2TokenById: fetchTCO2TokenByIdMethod = async (id) => {
const query = gql`
query ($id: String) {
tco2Token(id: $id) {
id
name
symbol
address
projectVintage {
name
project {
projectId
}
}
}
}
`;
const result = await this.graphClient.query(query, { id: id }).toPromise();
if (result.error) throw result.error;
if (result.data?.tco2Tokens) return result.data.tco2Tokens;
return;
};
/**
*
* @description fetches properties of a TCO2
* @param symbol full symbol of the TCO2 to query for e.g.: "TCO2-VCS-1718-2013"
* @returns a TCO2Detail object with properties of the TCO2 (name, address, etc)
*/
fetchTCO2TokenByFullSymbol: fetchTCO2TokenByFullSymbolMethod = async (
symbol: string
) => {
const query = gql`
query ($symbol: String) {
tco2Tokens(where: { symbol: $symbol }) {
id
name
symbol
address
projectVintage {
name
project {
projectId
}
}
}
}
`;
const result = await this.graphClient
.query(query, { symbol: symbol })
.toPromise();
if (result.error) throw result.error;
if (result.data?.tco2Tokens[0]) return result.data.tco2Tokens[0];
return;
};
/**
*
* @description fetches TCO2Details of all TCO2s
* @returns an array of TCO2Detail objects with properties of the TCO2s (name, address, etc)
*/
fetchAllTCO2Tokens: fetchAllTCO2TokensMethod = async () => {
let TCO2Tokens: fetchTCO2TokenResult[] = [];
let skip = 0;
const first = 1000;
for (;;) {
const query = gql`
query ($first: Int, $skip: Int) {
tco2Tokens(first: $first, skip: $skip) {
name
symbol
address
projectVintage {
name
project {
projectId
}
}
}
}
`;
const result = await this.graphClient
.query(query, { first: first, skip: skip })
.toPromise();
if (result.error) throw result.error;
if (result.data?.tco2Tokens) {
TCO2Tokens = TCO2Tokens.concat(result.data.tco2Tokens);
if (result.data.tco2Tokens.length === first) {
skip += first;
continue;
}
}
break;
}
return TCO2Tokens;
};
// --------------------------------------------------------------------------------
// BatchTokens Subgraph Methods
// --------------------------------------------------------------------------------
/**
*
* @description fetches data about BatchTokens that have been bridged
* @returns an array of BatchTokens containing different properties like id, serialNumber or quantity
*/
fetchBridgedBatchTokens: fetchBridgedBatchTokensMethod = async () => {
let BridgedBatchTokens: fetchBridgedBatchTokensResult[] = [];
let skip = 0;
const first = 1000;
for (;;) {
const query = gql`
query ($retirementStatus: Int, $first: Int, $skip: Int) {
batchTokens(
where: { confirmationStatus: $retirementStatus }
orderBy: timestamp
first: $first
skip: $skip
) {
id
serialNumber
quantity
creator {
id
}
timestamp
tx
}
}
`;
const result = await this.graphClient
.query(query, {
retirementStatus: 2, // RetirementStatus.Confirmed = 2
first: first,
skip: skip,
})
.toPromise();
if (result.error) throw result.error;
if (result.data?.batchTokens) {
BridgedBatchTokens = BridgedBatchTokens.concat(result.data.batchTokens);
if (result.data.batchTokens.length === first) {
skip += first;
continue;
}
}
break;
}
return BridgedBatchTokens;
};
// --------------------------------------------------------------------------------
// Retirements Subgraph Methods
// --------------------------------------------------------------------------------
/**
*
* @description fetches retirements made by a user
* @param walletAddress address of the user/wallet to query for
* @param first how many retirements you want fetched; defaults to 100
* @param skip how many (if any) retirements you want skipped; defaults to 0
* @returns an array of objects containing properties of the retirements like id, creationTx, amount and more
*/
fetchUserRetirements: fetchUserRetirementsMethod = async (
walletAddress,
first = 100,
skip = 0
) => {
const query = gql`
query ($walletAddress: String, $first: Int, $skip: Int) {
user(id: $walletAddress) {
retirementsCreated(
first: $first
skip: $skip
orderBy: timestamp
orderDirection: desc
) {
id
creationTx
amount
timestamp
token {
symbol
name
address
projectVintage {
name
project {
projectId
}
}
}
certificate {
id
retiringEntity {
id
}
beneficiary {
id
}
retiringEntityString
beneficiaryString
retirementMessage
createdAt
}
}
}
}
`;
const result = await this.graphClient
.query(query, { walletAddress: walletAddress, first: first, skip: skip })
.toPromise();
if (result.error) throw result.error;
if (result.data?.user?.retirementsCreated)
return result.data.user.retirementsCreated;
return [];
};
// --------------------------------------------------------------------------------
// Redeems Subgraph Methods
// --------------------------------------------------------------------------------
/**
*
* @description fetches redeems of a given pool
* @param pool symbol of pool to fetch for
* @param first how many redeems you want fetched; defaults to 100
* @param skip how many (if any) redeems you want skipped; defaults to 0
* @returns an array of objects with properties of the redeems like id, amount, timestamp and more
*/
fetchRedeems: fetchRedeemsMethod = async (pool, first = 100, skip = 0) => {
const poolAddress = this.getPoolContract(pool).address;
const query = gql`
query ($poolAddress: String, $first: Int, $skip: Int) {
redeems(
where: { pool: $poolAddress }
first: $first
skip: $skip
orderBy: timestamp
orderDirection: desc
) {
id
amount
timestamp
creator {
id
}
token {
symbol
name
address
projectVintage {
name
project {
projectId
}
}
}
}
}
`;
const result = await this.graphClient
.query(query, { poolAddress: poolAddress, first: first, skip: skip })
.toPromise();
if (result.error) throw result.error;
if (result.data?.redeems) return result.data.redeems;
return [];
};
/**
*
* @description fetches redeems of a given pool and user
* @param walletAddress address of the user/wallet to query for
* @param pool symbol of pool to fetch for
* @param first how many redeems you want fetched; defaults to 100
* @param skip how many (if any) redeems you want skipped; defaults to 0
* @returns an array of objects with properties of the redeems like id, amount, timestamp and more
*/
fetchUserRedeems: fetchUserRedeemsMethod = async (
walletAddress,
pool,
first = 100,
skip = 0
) => {
const poolAddress = this.getPoolContract(pool).address;
const query = gql`
query (
$walletAddress: String
$poolAddress: String
$first: Int
$skip: Int
) {
user(id: $walletAddress) {
redeemsCreated(
where: { pool: $poolAddress }
first: $first
skip: $skip
orderBy: timestamp
orderDirection: desc
) {
id
amount
timestamp
creator {
id
}
token {
symbol
name
address
projectVintage {
name
project {
projectId
}
}
}
}
}
}
`;
const result = await this.graphClient
.query(query, {
walletAddress: walletAddress,
poolAddress: poolAddress,
first: first,
skip: skip,
})
.toPromise();
if (result.error) throw result.error;
if (result.data?.user?.redeemsCreated)
return result.data.user.redeemsCreated;
return [];
};
// --------------------------------------------------------------------------------
// PooledTCO2Tokens Subgraph Methods
// --------------------------------------------------------------------------------
/**
*
* @description fetches TCO2 tokens that are part of the given pool
* @param pool symbol of the pool to fetch for
* @param first how many TCO2 tokens you want fetched; defaults to 1000
* @param skip how many (if any) retirements you want skipped; defaults to 0
* @returns an array of objects representing TCO2 tokens and containing properties like name, amount, methodology and more
*/
fetchPoolContents: fetchPoolContentsMethod = async (
pool,
first = 1000,
skip = 0
) => {
const poolAddress = this.getPoolContract(pool).address;
const query = gql`
query ($poolAddress: String, $first: Int, $skip: Int) {
pooledTCO2Tokens(
where: { poolAddress: $poolAddress }
first: $first
skip: $skip
orderBy: amount
orderDirection: desc
) {