Skip to content

Commit 0450323

Browse files
author
Isaac Herrera
authored
Merge pull request #13 from getsumer/ih/sdk-api-routes-refactor
Ih/sdk api routes refactor
2 parents 050c6a3 + b839518 commit 0450323

File tree

10 files changed

+526
-405
lines changed

10 files changed

+526
-405
lines changed

package-lock.json

Lines changed: 429 additions & 352 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sumer-sdk",
3-
"version": "4.2.4",
3+
"version": "4.3.0",
44
"description": "A package that allows devs to track the errors that may occur in EVM-based Dapps",
55
"main": "build/module/index.js",
66
"typings": "build/module/index.d.ts",
@@ -35,17 +35,12 @@
3535
"dependencies": {
3636
"@ethersproject/abi": "^5.7.0",
3737
"@ethersproject/providers": "^5.7.0",
38-
"axios": "^1.3.3",
39-
"babel-jest": "^27.1.5",
4038
"bowser": "^2.11.0",
41-
"ethers": "^5.7.2",
42-
"uuid": "^9.0.0"
39+
"ethers": "^5.7.2"
4340
},
4441
"bundleDependencies": [
4542
"@ethersproject/abi",
4643
"@ethersproject/providers",
47-
"axios",
48-
"babel-jest",
4944
"bowser",
5045
"ethers"
5146
],

src/Errors/ContractError.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ interface ContractErrorArguments {
77
}
88

99
export class ContractError {
10-
public contractAddress: string
11-
public signerOrProviderAddress: string
12-
public name: string
13-
public args: any[]
14-
public reason: string
10+
public readonly contractAddress: string
11+
public readonly signerOrProviderAddress: string
12+
public readonly name: string
13+
public readonly args: any[]
14+
public readonly reason: string
15+
public readonly type = 'CONTRACT'
1516

1617
constructor({
1718
contractAddress,

src/Errors/ProviderError.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ interface ProviderErrorArguments {
77
}
88

99
export class ProviderError {
10-
public message: string
11-
public code: number
12-
public address: string
13-
public eip: EipError
10+
public readonly message: string
11+
public readonly code: number
12+
public readonly address: string
13+
public readonly eip: EipError
14+
public readonly type = 'WALLET'
1415

1516
constructor({ message, code, address }: ProviderErrorArguments) {
1617
this.message = message

src/Notify/NotifyService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { ContractError, ProviderError } from '../Errors'
2-
import { TransactionData } from '../Types/TransactionData'
2+
import { Transaction } from '../Types/Transaction'
33

44
export interface NotifyService {
55
trackError(error: ContractError | ProviderError): Promise<void>
6-
trackTransaction(transactionData: TransactionData): Promise<void>
6+
trackTransaction(transaction: Transaction): Promise<void>
77
checkConnection(): Promise<void>
88
}

src/Notify/NotifyServiceApi.ts

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,42 @@
1-
import axios, { RawAxiosRequestHeaders } from 'axios'
2-
import { v4 } from 'uuid'
3-
import bowser from 'bowser'
1+
import bowser, { Parser } from 'bowser'
42
import { NotifyService } from './NotifyService'
53
import { 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

831
export 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
}

src/Notify/NotifyServiceLog.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NotifyService } from './NotifyService'
22
import { ContractError, ProviderError } from '../Errors'
3-
import { TransactionData } from '../Types/TransactionData'
3+
import { Transaction } from '../Types/Transaction'
44

55
export class NotifyServiceLog implements NotifyService {
66
private _chainId: number
@@ -9,8 +9,8 @@ export class NotifyServiceLog implements NotifyService {
99
return this._chainId
1010
}
1111

12-
public async trackTransaction(transactionData: TransactionData): Promise<void> {
13-
console.info('trackTransaction Log:', transactionData)
12+
public async trackTransaction(transaction: Transaction): Promise<void> {
13+
console.info('trackTransaction Log:', transaction)
1414
}
1515

1616
public async trackError(msg: ContractError | ProviderError): Promise<void> {

src/SumerContract.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Fragment, JsonFragment } from '@ethersproject/abi'
33
import { Provider } from '@ethersproject/providers'
44
import { NotifyService } from './Notify'
55
import { ContractError } from './Errors'
6+
import { Transaction } from './Types/Transaction'
67

78
interface SumerContractArguments {
89
addressOrName: string
@@ -38,13 +39,13 @@ export class SumerContract {
3839
return async (...args: any) => {
3940
try {
4041
const result = await method.apply(this, args)
41-
42-
notifyService.trackTransaction({
42+
const transaction = new Transaction({
4343
chainId,
4444
txHash: result.hash,
4545
functionName: prop,
4646
args: args,
4747
})
48+
notifyService.trackTransaction(transaction)
4849
return result
4950
} catch (err) {
5051
let signerOrProviderAddress: string

src/Types/Transaction.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
interface TransactionArguments {
2+
chainId: number
3+
txHash: string
4+
functionName?: string
5+
args?: any[]
6+
}
7+
8+
export class Transaction {
9+
public readonly chainId: number
10+
public readonly txHash: string
11+
public readonly functionName?: string
12+
public readonly args?: any[]
13+
14+
constructor({ chainId, txHash, functionName, args }: TransactionArguments) {
15+
this.chainId = chainId
16+
this.txHash = txHash
17+
this.functionName = functionName
18+
this.args = args
19+
}
20+
}

src/Types/TransactionData.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)