Skip to content

Commit 05a5702

Browse files
committed
Added account/balancemulti
1 parent 9600f7b commit 05a5702

File tree

4 files changed

+139
-3
lines changed

4 files changed

+139
-3
lines changed

src/Client.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { account } from './actions/account'
22
import { ClientAccountBalance } from './client/ClientAccountBalance'
3+
import { ClientAccountBalancemulti } from './client/ClientAccountBalancemulti'
34
import { Address } from './entities/Address'
45
import { ApiKey } from './entities/Apikey'
56

@@ -29,6 +30,10 @@ export class Client {
2930
const oAddress = new Address(address)
3031
return new ClientAccountBalance(this.apiKey, 'account', action, oAddress, tag)
3132
},
33+
balancemulti(address: string[], tag: string) {
34+
const oAddress = address.map((addresString) => new Address(addresString))
35+
return new ClientAccountBalancemulti(this.apiKey, 'account', action, oAddress, tag)
36+
},
3237
}
3338

3439
return actions[action]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import * as request from 'request-promise-native'
2+
import { Address } from '../entities/Address'
3+
import { ApiKey } from '../entities/Apikey'
4+
import { IClientAccountBalanceMultiRequest } from '../interfaces/Account'
5+
import { requestBuilder } from '../requestBuilder'
6+
7+
/**
8+
* Client for the account balance
9+
*/
10+
export class ClientAccountBalancemulti implements IClientAccountBalanceMultiRequest {
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+
* tag to limit the results
29+
*/
30+
tag: string
31+
32+
constructor(apiKey: ApiKey, module: string, action: string, address: Address[], tag: string) {
33+
this.apiKey = apiKey
34+
this.module = module
35+
this.action = action
36+
this.address = address
37+
this.tag = tag
38+
}
39+
/**
40+
* Returns the serice url
41+
* @returns url
42+
*/
43+
toUrl(): string {
44+
return requestBuilder(this.module, this.action, {
45+
address: this.address.toString(),
46+
apiKey: this.apiKey.toString(),
47+
tag: this.tag.toString(),
48+
})
49+
}
50+
/**
51+
* Dies the actual request to the server
52+
*/
53+
async request(): Promise<any> {
54+
const options = {
55+
uri: this.toUrl(),
56+
}
57+
return request.get(options)
58+
}
59+
}

src/interfaces/Account.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@ export interface IClientAccountBalanceRequest extends IClientRequest {
1515
tag: string
1616
}
1717

18-
/*
19-
18+
/**
19+
* Interface to Account/Balancemulti
20+
*/
2021
export interface IClientAccountBalanceMultiRequest extends IClientRequest {
21-
address: Address
22+
/**
23+
* Array of ethereum addresses
24+
*/
25+
address: Address[]
26+
/**
27+
* Tag to limit results
28+
*/
2229
tag: string
2330
}
2431

32+
/*
2533
export interface IClientAccountTxlistRequest extends IClientRequest {
2634
address: Address
2735
startblock: string
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import test from 'ava'
2+
import { ClientAccountBalancemulti } from '../../src/client/ClientAccountBalancemulti'
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 'querystring'
8+
import { parse as parseUrl } from 'url'
9+
import { readFile } from 'fs'
10+
import { promisify } from 'util'
11+
const _readFile = promisify(readFile)
12+
13+
const apiKey = 'TRU5Z5MNWIEYP4F6DPH2T53IJWZIZ5GT1W'
14+
const address = [
15+
'0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
16+
'0x63a9975ba31b0b9626b34300f7f627147df1f526',
17+
'0x198ef1ec325a96cc354c7266a038be8b5c558f67'
18+
]
19+
const tag = 'latest'
20+
const expectedUrl = 'https://api.etherscan.io/api?action=balancemulti&module=account&address=0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a,0x63a9975ba31b0b9626b34300f7f627147df1f526,0x198ef1ec325a96cc354c7266a038be8b5c558f67&tag=latest&apikey=YourApiKeyToken'
21+
const fixtureLocation= './test/fixtures/account/balancemulti/address-latest.json'
22+
23+
test('exists', t => {
24+
t.truthy(ClientAccountBalancemulti)
25+
})
26+
27+
test('can be instantiated', t => {
28+
const arrAddress = address.map(a => new Address(a))
29+
const oApiKey = new ApiKey(apiKey)
30+
new ClientAccountBalancemulti(oApiKey, 'account', 'balancemulti', arrAddress, tag)
31+
t.pass()
32+
})
33+
34+
test('generates the right url', t => {
35+
const arrAddress = address.map(a => new Address(a))
36+
const oApiKey = new ApiKey(apiKey)
37+
const c = new ClientAccountBalancemulti(oApiKey, 'account', 'balancemulti', arrAddress, tag)
38+
const url = c.toUrl()
39+
40+
const parsedUrl = parse(url)
41+
const parsedExpectedUrl = parse(expectedUrl)
42+
43+
t.is(parsedUrl.action, parsedExpectedUrl.action, 'actionnot as expected')
44+
t.is(parsedUrl.module, parsedExpectedUrl.module)
45+
t.is(parsedUrl.address, parsedExpectedUrl.address)
46+
t.is(parsedUrl.tag, parsedExpectedUrl.tag)
47+
})
48+
49+
50+
test('actually request and get an response', async t => {
51+
const arrAddress = address.map(a => new Address(a))
52+
const oApiKey = new ApiKey(apiKey)
53+
const c = new ClientAccountBalancemulti(oApiKey, 'account', 'balancemulti', arrAddress, tag)
54+
const url = c.toUrl()
55+
const parsedUrl = parseUrl(url)
56+
const resultFixture = await _readFile(fixtureLocation, 'utf-8')
57+
58+
nock(`${parsedUrl.protocol}//${parsedUrl.host}`)
59+
.get(parsedUrl.path)
60+
.reply(200, resultFixture)
61+
62+
const result = await c.request()
63+
t.truthy(result)
64+
})

0 commit comments

Comments
 (0)