1- import axios , { RawAxiosRequestHeaders } from 'axios'
2- import { v4 } from 'uuid'
3- import bowser from 'bowser'
1+ import bowser , { Parser } from 'bowser'
42import { NotifyService } from './NotifyService'
53import { ProviderError , ContractError } from '../Errors'
6- import { TransactionData } from '../Types/TransactionData'
4+ import { Transaction } from '../Types/Transaction'
5+
6+ interface TransactionBody {
7+ chainId : number
8+ txHash : string
9+ functionName : string
10+ functionArgs : any
11+ metadata : Parser . ParsedResult | Record < string , string >
12+ }
13+
14+ interface ErrorBody {
15+ userAddress : string
16+ message : string
17+ errorType : string
18+ metadata : Parser . ParsedResult | Record < string , string >
19+ }
20+
21+ interface ContractErrorBody extends ErrorBody {
22+ contractAddress : string
23+ functionName : string
24+ args : any
25+ }
26+
27+ interface ProviderErrorBody extends ErrorBody {
28+ code : number
29+ }
730
831export class NotifyServiceApi implements NotifyService {
9- private headers : RawAxiosRequestHeaders
32+ private headers : HeadersInit
1033 private url : string
1134
1235 constructor ( apikey : string , chainId ?: number , dns ?: string ) {
1336 this . headers = {
14- Authorization : `${ apikey } ` ,
15- chainId : `${ chainId } ` ,
37+ authorization : `${ apikey } ` ,
38+ chainid : `${ chainId } ` ,
39+ 'Content-Type' : 'application/json' ,
1640 }
1741 this . url = dns ?? 'https://api.getsumer.com'
1842 }
@@ -22,51 +46,59 @@ export class NotifyServiceApi implements NotifyService {
2246 txHash,
2347 functionName,
2448 args,
25- } : TransactionData ) : Promise < void > {
26- const body = {
27- id : v4 ( ) ,
49+ } : Transaction ) : Promise < void > {
50+ this . fetchPost ( 'transactions' , {
2851 chainId,
2952 txHash,
3053 functionName,
3154 functionArgs : args ,
3255 metadata : this . meta ( ) ,
33- }
34- return axios . post ( `${ this . url } /tx/${ body . txHash } ` , body , { headers : this . headers } )
56+ } )
3557 }
3658
3759 public async trackError ( error : ContractError | ProviderError ) : Promise < void > {
60+ let body : ContractErrorBody | ProviderErrorBody
3861 if ( error instanceof ContractError ) {
39- const body = {
40- id : v4 ( ) ,
62+ body = {
4163 userAddress : error . signerOrProviderAddress ,
4264 contractAddress : error . contractAddress ,
4365 functionName : error . name ,
4466 args : error . args ,
4567 message : error . reason ,
68+ errorType : error . type ,
4669 metadata : this . meta ( ) ,
4770 }
48- return axios . post ( this . url + '/contract_errors' , body , { headers : this . headers } )
4971 } else {
50- const body = {
51- id : v4 ( ) ,
72+ body = {
5273 userAddress : error . address ,
5374 code : error . code ,
5475 message : error . message ,
76+ errorType : error . type ,
5577 metadata : this . meta ( ) ,
5678 }
57- return axios . post ( `${ this . url } /exception` , body , { headers : this . headers } )
5879 }
80+ this . fetchPost ( 'errors' , body )
5981 }
6082
6183 public async checkConnection ( ) : Promise < void > {
62- return axios . post (
63- `${ this . url } /set_status` ,
64- { status : 'provider detected' } ,
65- { headers : this . headers } ,
66- )
84+ fetch ( `${ this . url } /check` , {
85+ method : 'GET' ,
86+ headers : this . headers ,
87+ } )
88+ }
89+
90+ private fetchPost (
91+ uriPath : string ,
92+ body ?: TransactionBody | ContractErrorBody | ProviderErrorBody ,
93+ ) {
94+ fetch ( `${ this . url } /${ uriPath } ` , {
95+ method : 'POST' ,
96+ headers : this . headers ,
97+ body : body ? JSON . stringify ( body ) : undefined ,
98+ } )
6799 }
68100
69- private meta ( ) {
101+ private meta ( ) : Parser . ParsedResult | Record < string , string > {
70102 if ( window ?. navigator ?. userAgent ) {
71103 return bowser . parse ( window . navigator . userAgent )
72104 }
0 commit comments