Skip to content

Commit 77c45e4

Browse files
committed
Added Account/Tokentx
1 parent 1f9c448 commit 77c45e4

File tree

4 files changed

+162
-14
lines changed

4 files changed

+162
-14
lines changed

src/client/ClientAccountTokentx.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import * as request from 'request-promise-native'
2+
import { Address } from '../entities/Address'
3+
import { ApiKey } from '../entities/Apikey'
4+
import { Contractaddress } from '../entities/Contractaddress'
5+
import { IClientAccountTokentx } from '../interfaces/Account'
6+
import { requestBuilder } from '../requestBuilder'
7+
import { ClientBase } from './ClientBase'
8+
9+
/**
10+
* Client for the account balance
11+
*/
12+
export class ClientAccountTxlist extends ClientBase implements IClientAccountTokentx {
13+
/**
14+
* Api key to send with the request
15+
*/
16+
apiKey: ApiKey
17+
/**
18+
* Address to lookup the account balance
19+
*/
20+
address: Address
21+
/**
22+
* module of the etherscan api to request
23+
*/
24+
module: string
25+
/**
26+
* action of the etherscan api to request
27+
*/
28+
action: string
29+
/**
30+
* Block to start reading data
31+
*/
32+
startblock: string
33+
/**
34+
* Read data to
35+
*/
36+
endblock: string
37+
/**
38+
* Sort
39+
*/
40+
sort?: string
41+
/**
42+
* Page
43+
*/
44+
page?: string
45+
/**
46+
* startpage
47+
*/
48+
offset?: string
49+
50+
constructor(
51+
apiKey: ApiKey,
52+
module: string,
53+
action: string,
54+
address: Address | Contractaddress,
55+
startblock: string,
56+
endblock: string,
57+
sort?: string,
58+
page?: string,
59+
offset?: string) {
60+
super()
61+
this.apiKey = apiKey
62+
this.module = module
63+
this.action = action
64+
this.address = address
65+
this.startblock = startblock
66+
this.endblock = endblock
67+
this.sort = sort
68+
this.page = page
69+
this.offset = offset
70+
}
71+
/**
72+
* Returns the serice url
73+
* @returns url
74+
*/
75+
toUrl(): string {
76+
77+
let params = {
78+
address: this.address.toString(),
79+
apiKey: this.apiKey.toString(),
80+
endblock: this.endblock.toString(),
81+
startblock: this.startblock.toString(),
82+
}
83+
84+
if (this.sort) {
85+
params = Object.assign(params, {
86+
sort: this.sort,
87+
})
88+
}
89+
90+
if (this.page) {
91+
params = Object.assign(params, {
92+
page: this.page,
93+
})
94+
}
95+
96+
if (this.offset) {
97+
params = Object.assign(params, {
98+
offset: this.offset,
99+
})
100+
}
101+
return requestBuilder(this.module, this.action, params)
102+
}
103+
/**
104+
* Dies the actual request to the server
105+
*/
106+
async request(): Promise<any> {
107+
const options = {
108+
uri: this.toUrl(),
109+
}
110+
const res = await request.get(options)
111+
return this.processResult(res)
112+
}
113+
}

src/entities/Address.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ import { EntityBase } from './EntityBase'
66
* A ethereum address
77
*/
88
export class Address extends EntityBase implements IEntity {
9-
/**
10-
* Regular expression to validate a ethereum address
11-
*/
12-
private regex = /0x[a-fA-F0-9]{40}/
139

1410
constructor(address: string) {
1511
super(address)
@@ -19,6 +15,7 @@ export class Address extends EntityBase implements IEntity {
1915
* Checks validity of the address
2016
*/
2117
valid(): boolean {
22-
return this.value.match(this.regex) !== null
18+
const regex = /0x[a-fA-F0-9]{40}/
19+
return this.value.match(regex) !== null
2320
}
2421
}

src/entities/Contractaddress.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
import { IEntity } from '../interfaces/Entity'
3+
import { EntityBase } from './EntityBase'
4+
5+
/**
6+
* A ethereum address
7+
*/
8+
export class Contractaddress extends EntityBase implements IEntity {
9+
10+
constructor(address: string) {
11+
super(address)
12+
this.errorMessage = 'Invalid Address'
13+
}
14+
/**
15+
* Checks validity of the address
16+
*/
17+
valid(): boolean {
18+
const regex = /0x[a-fA-F0-9]{40}/
19+
return this.value.match(regex) !== null
20+
}
21+
}

src/interfaces/Account.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Address } from '../entities/Address'
2+
import { Contractaddress } from '../entities/Contractaddress'
23
import { IClientRequest } from './Entity'
34

45
/**
@@ -64,14 +65,14 @@ export interface IClientAccountTxlistInternalTxhash extends IClientRequest {
6465
* Block to start reading data
6566
*/
6667
startblock: string
67-
/**
68-
* hash of the transaction
69-
*/
70-
txhash: string
7168
/**
7269
* Read data to
7370
*/
7471
endblock: string
72+
/**
73+
* hash of the transaction
74+
*/
75+
txhash: string
7576
/**
7677
* Paging actual page
7778
*/
@@ -86,13 +87,29 @@ export interface IClientAccountTxlistInternalTxhash extends IClientRequest {
8687
sort?: string
8788
}
8889

89-
/*
9090
export interface IClientAccountTokentx extends IClientRequest {
91-
contractaddress: string
92-
startblock?: string
93-
endblock?: string
91+
/**
92+
* Ethereum address
93+
*/
94+
address?: Address | Contractaddress
95+
/**
96+
* Block to start reading data
97+
*/
98+
startblock: string
99+
/**
100+
* Read data to
101+
*/
102+
endblock: string
103+
/**
104+
* Paging actual page
105+
*/
94106
page?: string
107+
/**
108+
* Paging start
109+
*/
95110
offset?: string
111+
/**
112+
* Sort Parameter
113+
*/
96114
sort?: string
97115
}
98-
*/

0 commit comments

Comments
 (0)