Skip to content

Commit 7774204

Browse files
committed
added account balancemulti and account txlist
1 parent 05a5702 commit 7774204

File tree

6 files changed

+217
-9
lines changed

6 files changed

+217
-9
lines changed

src/client/ClientAccountTxlist.ts

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

src/interfaces/Account.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,38 @@ export interface IClientAccountBalanceMultiRequest extends IClientRequest {
2929
tag: string
3030
}
3131

32-
/*
32+
/**
33+
* Interface to Account/txlist
34+
*/
3335
export interface IClientAccountTxlistRequest extends IClientRequest {
36+
/**
37+
* Ethereum address
38+
*/
3439
address: Address
40+
/**
41+
* Block to start reading data
42+
*/
3543
startblock: string
44+
/**
45+
* Read data to
46+
*/
3647
endblock: string
48+
/**
49+
* Paging actual page
50+
*/
3751
page?: string
52+
/**
53+
* Paging start
54+
*/
3855
offset?: string
56+
/**
57+
* Sort Parameter
58+
*/
3959
sort?: string
4060
}
61+
62+
/*
63+
4164
export interface IClientAccountTxlistInternalRequest extends IClientAccountTxlistRequest {
4265
txhash: string
4366
}

test/clients/ClientAccountBalancemultiTest.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { Address } from '../../src/entities/Address'
44
import { ApiKey } from '../../src/entities/Apikey'
55
import { decode } from 'querystring'
66
const nock = require('nock')
7-
import { parse } from 'querystring'
8-
import { parse as parseUrl } from 'url'
7+
import { parse } from 'url'
98
import { readFile } from 'fs'
109
import { promisify } from 'util'
1110
const _readFile = promisify(readFile)
@@ -37,22 +36,21 @@ test('generates the right url', t => {
3736
const c = new ClientAccountBalancemulti(oApiKey, 'account', 'balancemulti', arrAddress, tag)
3837
const url = c.toUrl()
3938

40-
const parsedUrl = parse(url)
41-
const parsedExpectedUrl = parse(expectedUrl)
39+
const parsedUrl = decode(url)
40+
const parsedExpectedUrl = decode(expectedUrl)
4241

4342
t.is(parsedUrl.action, parsedExpectedUrl.action, 'actionnot as expected')
4443
t.is(parsedUrl.module, parsedExpectedUrl.module)
4544
t.is(parsedUrl.address, parsedExpectedUrl.address)
4645
t.is(parsedUrl.tag, parsedExpectedUrl.tag)
4746
})
4847

49-
5048
test('actually request and get an response', async t => {
5149
const arrAddress = address.map(a => new Address(a))
5250
const oApiKey = new ApiKey(apiKey)
5351
const c = new ClientAccountBalancemulti(oApiKey, 'account', 'balancemulti', arrAddress, tag)
5452
const url = c.toUrl()
55-
const parsedUrl = parseUrl(url)
53+
const parsedUrl = parse(url)
5654
const resultFixture = await _readFile(fixtureLocation, 'utf-8')
5755

5856
nock(`${parsedUrl.protocol}//${parsedUrl.host}`)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import test from 'ava'
2+
import { ClientAccountTxlist } from '../../src/client/ClientAccountTxlist'
3+
import { Address } from '../../src/entities/Address'
4+
import { ApiKey } from '../../src/entities/Apikey'
5+
import { decode } from 'querystring'
6+
const nock = require('nock')
7+
import { parse } from 'url'
8+
import { readFile } from 'fs'
9+
import { promisify } from 'util'
10+
const _readFile = promisify(readFile)
11+
12+
const apiKey = 'TRU5Z5MNWIEYP4F6DPH2T53IJWZIZ5GT1W'
13+
const address = '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a'
14+
const startblock = '0'
15+
const endblock = '99999999'
16+
const expectedUrl = 'http://api.etherscan.io/api?module=account&action=txlist&address=0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a&startblock=0&endblock=99999999&sort=asc&apikey=YourApiKeyToken'
17+
const fixtureLocation= './test/fixtures/account/txlist/address-startblock-endblock.json'
18+
19+
test('exists', t => {
20+
t.truthy(ClientAccountTxlist)
21+
})
22+
23+
test('can be instantiated', t => {
24+
const oAddress = new Address(address)
25+
const oApiKey = new ApiKey(apiKey)
26+
new ClientAccountTxlist(oApiKey, 'account', 'balancemulti', oAddress, startblock, endblock)
27+
t.pass()
28+
})
29+
30+
31+
test('generates the right url', t => {
32+
const oAddress = new Address(address)
33+
const oApiKey = new ApiKey(apiKey)
34+
const c = new ClientAccountTxlist(oApiKey, 'account', 'txlist', oAddress, startblock, endblock)
35+
const url = c.toUrl()
36+
37+
const parsedUrl = decode(url)
38+
const parsedExpectedUrl = decode(expectedUrl)
39+
40+
t.is(parsedUrl.address, parsedExpectedUrl.address)
41+
t.is(parsedUrl.startblock, parsedExpectedUrl.startblock)
42+
t.is(parsedUrl.endblock, parsedExpectedUrl.endblock)
43+
})
44+
45+
test('sort', t => {
46+
const oAddress = new Address(address)
47+
const oApiKey = new ApiKey(apiKey)
48+
const c = new ClientAccountTxlist(oApiKey, 'account', 'txlist', oAddress, startblock, endblock, 'desc')
49+
const url = c.toUrl()
50+
const parsedUrl = decode(url)
51+
t.is(parsedUrl.sort, 'desc')
52+
})
53+
54+
test('start end endblock', t => {
55+
const oAddress = new Address(address)
56+
const oApiKey = new ApiKey(apiKey)
57+
const c = new ClientAccountTxlist(oApiKey, 'account', 'txlist', oAddress, startblock, endblock, 'desc', '10')
58+
const url = c.toUrl()
59+
const parsedUrl = decode(url)
60+
t.is(parsedUrl.page, '10')
61+
})
62+
63+
64+
test('actually request and get an response', async t => {
65+
const oAddress = new Address(address)
66+
const oApiKey = new ApiKey(apiKey)
67+
const c = new ClientAccountTxlist(oApiKey, 'account', 'txlist', oAddress, startblock, endblock, 'desc', '10')
68+
69+
const resultFixture = await _readFile(fixtureLocation, 'utf-8')
70+
const parsedUrl = parse(c.toUrl())
71+
72+
nock(`${parsedUrl.protocol}//${parsedUrl.host}`)
73+
.get(parsedUrl.path)
74+
.reply(200, resultFixture)
75+
76+
const result = await c.request()
77+
t.truthy(result)
78+
})

test/fixtures/account/txlist/address-startblock-endblock.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

test/requestBuilderTest.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ test('contains additional parameters', t => {
2525
t.regex(res, /bar/)
2626
})
2727

28-
29-
3028
test('throws on unknown module', t => {
3129
t.throws(()=> requestBuilder('foomodulenotexist', 'balance'))
3230
})

0 commit comments

Comments
 (0)