1
1
import { Address , Transaction , parseGwei , parseUnits } from "viem" ;
2
2
import {
3
- TokenTransfer ,
4
- TransactionMeta ,
3
+ TokenTransfer ,
4
+ TransactionMeta ,
5
5
} from "../domain/transaction/transaction" ;
6
6
7
7
const ROOT = "https://eth.blockscout.com/api/" ;
8
8
export const invokeApi = async ( endpoint : string , body ?: any ) => {
9
- return fetch ( endpoint , {
10
- method : "GET" ,
11
- headers : {
12
- "Content-Type" : "application/json" ,
13
- } ,
14
- } ) . then ( ( res ) => {
15
- return res . json ( ) ;
16
- } ) ;
9
+ return fetch ( endpoint , {
10
+ method : "GET" ,
11
+ headers : {
12
+ "Content-Type" : "application/json" ,
13
+ } ,
14
+ } ) . then ( ( res ) => {
15
+ return res . json ( ) ;
16
+ } ) ;
17
17
} ;
18
18
19
19
// TODO better pairing types
20
20
export enum BlockscoutEndpoint {
21
- Transaction = "transaction" ,
22
- Address = "address" ,
21
+ Transaction = "transaction" ,
22
+ Address = "address" ,
23
23
}
24
24
export interface BlockscoutEndpointParams {
25
- address ?: Address ;
26
- txnHash ?: string ;
25
+ address ?: Address ;
26
+ txnHash ?: string ;
27
27
}
28
28
29
29
export const ENDPOINT_STRATEGIES = {
30
- [ BlockscoutEndpoint . Address ] : ( params : BlockscoutEndpointParams ) => {
31
- const { address } = params ;
32
- return ROOT + "v1/address/" + address ;
33
- } ,
34
- [ BlockscoutEndpoint . Transaction ] : ( params : BlockscoutEndpointParams ) => {
35
- const { txnHash } = params ;
36
- return ROOT + "v2/transactions/" + txnHash ;
37
- } ,
30
+ [ BlockscoutEndpoint . Address ] : ( params : BlockscoutEndpointParams ) => {
31
+ const { address } = params ;
32
+ return ROOT + "v1/address/" + address ;
33
+ } ,
34
+ [ BlockscoutEndpoint . Transaction ] : ( params : BlockscoutEndpointParams ) => {
35
+ const { txnHash } = params ;
36
+ return ROOT + "v2/transactions/" + txnHash ;
37
+ } ,
38
38
} ;
39
39
40
40
// TODO enum type
41
41
export const getEndpoint = (
42
- endpoint : BlockscoutEndpoint ,
43
- params : BlockscoutEndpointParams ,
42
+ endpoint : BlockscoutEndpoint ,
43
+ params : BlockscoutEndpointParams ,
44
44
) => {
45
- const strategy = ENDPOINT_STRATEGIES [ endpoint ] ;
46
- if ( ! strategy ) {
47
- throw new Error ( "" ) ;
48
- }
45
+ const strategy = ENDPOINT_STRATEGIES [ endpoint ] ;
46
+ if ( ! strategy ) {
47
+ throw new Error ( "" ) ;
48
+ }
49
49
50
- return strategy ( params ) ;
50
+ return strategy ( params ) ;
51
51
} ;
52
52
53
53
export const getAddressInfo = async ( address : Address ) => {
54
- const endpoint = getEndpoint ( BlockscoutEndpoint . Address , { address } ) ;
55
- return invokeApi ( endpoint ) ;
54
+ const endpoint = getEndpoint ( BlockscoutEndpoint . Address , { address } ) ;
55
+ return invokeApi ( endpoint ) ;
56
56
} ;
57
57
58
58
export const getTransaction = async ( txnHash : string ) => {
59
- const endpoint = getEndpoint ( BlockscoutEndpoint . Transaction , {
60
- txnHash,
61
- } ) ;
62
- return invokeApi ( endpoint ) ;
59
+ const endpoint = getEndpoint ( BlockscoutEndpoint . Transaction , {
60
+ txnHash,
61
+ } ) ;
62
+ return invokeApi ( endpoint ) ;
63
63
} ;
64
64
65
65
export const findDisplayedTxType = ( transaction_types : any [ ] ) : string => {
66
- if ( transaction_types . includes ( "contract_call" ) ) {
67
- return "contract_call" ;
68
- }
69
- if ( transaction_types . includes ( "coin_transfer" ) ) {
70
- return "coin_transfer" ;
71
- }
72
- return "native_transfer" ;
66
+ if ( transaction_types . includes ( "contract_call" ) ) {
67
+ return "contract_call" ;
68
+ }
69
+ if ( transaction_types . includes ( "coin_transfer" ) ) {
70
+ return "coin_transfer" ;
71
+ }
72
+ return "native_transfer" ;
73
73
} ;
74
74
75
75
export const asTokenTransfer = ( transfer : any ) : TokenTransfer => {
76
- const { token } = transfer ;
77
- return {
78
- ...token ,
79
- // imageUrl: '',
80
- name : transfer . token . name ,
81
- amount : parseUnits ( transfer . total . value , transfer . total . decimals ) ,
82
- } ;
76
+ const { token } = transfer ;
77
+ return {
78
+ ...token ,
79
+ // imageUrl: '',
80
+ name : transfer . token . name ,
81
+ amount : parseUnits ( transfer . total . value , transfer . total . decimals ) ,
82
+ } ;
83
83
} ;
84
84
85
85
/**
@@ -88,15 +88,15 @@ export const asTokenTransfer = (transfer: any): TokenTransfer => {
88
88
*
89
89
*/
90
90
export const asTransactionMeta = ( res : any ) : TransactionMeta => {
91
- return {
92
- hash : res . hash ,
93
- blockHash : res . blockHash ,
94
- from : res . from . hash as Address ,
95
- to : res . to . hash as Address ,
96
- isSuccess : res . success ,
97
- displayedTxType : findDisplayedTxType ( res . tx_types ) ,
98
- value : parseUnits ( res . value , res . decimals ) ,
99
- tokenTransfers : res . token_transfers . map ( asTokenTransfer ) ,
100
- gas : res . gas_used ,
101
- } ;
91
+ return {
92
+ hash : res . hash ,
93
+ blockHash : res . blockHash ,
94
+ from : res . from . hash as Address ,
95
+ to : res . to . hash as Address ,
96
+ isSuccess : res . success ,
97
+ displayedTxType : findDisplayedTxType ( res . tx_types ) ,
98
+ value : parseUnits ( res . value , res . decimals ) ,
99
+ tokenTransfers : res . token_transfers . map ( asTokenTransfer ) ,
100
+ gas : res . gas_used ,
101
+ } ;
102
102
} ;
0 commit comments