1
+ import { sumSingleBalance } from "../helper/generalUtil" ;
2
+ import { PeggedIssuanceAdapter , Balances , ChainBlocks , ChainContracts } from "../peggedAsset.type" ;
3
+ import axios from "axios" ;
4
+
5
+ const USDhContract = 'SPN5AKG35QZSK2M8GAMR4AFX45659RJHDW353HSG.usdh-token-v1' ;
6
+ const API_URL = 'https://api.mainnet.hiro.so/v2/contracts/call-read' ;
7
+
8
+ const chainContracts : ChainContracts = {
9
+ stacks : {
10
+ issued : [
11
+ USDhContract ,
12
+ ] ,
13
+ } ,
14
+ } ;
15
+
16
+ function parseClarityInt ( hexString : string ) : string {
17
+ const hex = hexString . startsWith ( "0x" ) ? hexString . slice ( 2 ) : hexString ;
18
+ const numberHex = hex . slice ( 4 ) ;
19
+ let bigIntValue = BigInt ( "0x" + numberHex ) ;
20
+
21
+ if ( bigIntValue > BigInt ( "0x7ffffffffffffffffffffffffffffffff" ) ) {
22
+ bigIntValue = bigIntValue - BigInt ( "0x100000000000000000000000000000000" ) ;
23
+ }
24
+
25
+ return bigIntValue . toString ( ) ;
26
+ }
27
+
28
+ async function fetchWithRetry ( url : string , data : any , retries = 3 ) : Promise < any > {
29
+ for ( let attempt = 1 ; attempt <= retries ; attempt ++ ) {
30
+ try {
31
+ return await axios . post ( url , data ) ;
32
+ } catch ( error ) {
33
+ if ( attempt === retries ) throw error ;
34
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 * attempt ) ) ; // Exponential backoff
35
+ }
36
+ }
37
+ }
38
+
39
+ async function hermeticaMinted ( decimals : number ) {
40
+ return async function (
41
+ _timestamp : number ,
42
+ _ethBlock : number ,
43
+ _chainBlocks : ChainBlocks
44
+ ) {
45
+ const balances = { } as Balances ;
46
+ const [ contractAddress , contractName ] = USDhContract . split ( '.' ) ;
47
+
48
+ const res = await fetchWithRetry ( `${ API_URL } /${ contractAddress } /${ contractName } /get-total-supply` , {
49
+ sender : contractAddress ,
50
+ arguments : [ ] ,
51
+ } ) ;
52
+
53
+ const result = res ?. data ?. result ;
54
+ if ( result ) {
55
+ const supply = Number ( parseClarityInt ( result ) ) / 10 ** decimals ;
56
+ sumSingleBalance ( balances , "peggedUSD" , supply , "issued" , false ) ;
57
+ }
58
+
59
+ return balances ;
60
+ } ;
61
+ }
62
+
63
+ const adapter : PeggedIssuanceAdapter = {
64
+ stacks : {
65
+ minted : hermeticaMinted ( 8 ) ,
66
+ } ,
67
+ } ;
68
+
69
+ export default adapter ;
0 commit comments