Skip to content

Commit 6abc34c

Browse files
committed
feat(tokens): [Method] getHolders - Add docs + tests.
1 parent 17254fb commit 6abc34c

File tree

5 files changed

+791
-334
lines changed

5 files changed

+791
-334
lines changed

dist/web3data.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/api.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,7 @@ const velocity = await web3data.token.getVelocity('0x06012c8cf97bead5deae237070f
16331633
#### getHolders(hash[, filterOptions])
16341634

16351635

1636-
1636+
Retrieves the latest or historical token holders for the specified address.
16371637

16381638

16391639

@@ -1643,7 +1643,8 @@ const velocity = await web3data.token.getVelocity('0x06012c8cf97bead5deae237070f
16431643
| Name | Type | Description | |
16441644
| ---- | ---- | ----------- | -------- |
16451645
| hash | `string` | The address for which to retrieve token holders. |   |
1646-
| filterOptions | `object` | The filters associated with the request. | *Optional* |
1646+
| filterOptions | `object` | The filters associated with the request. See [docs](https://docs.amberdata.io/reference#gettokenvelocity) for more details. | *Optional* |
1647+
| filterOptions.holderAddresses | `object` | The address for which to retrieve token holders. | *Optional* |
16471648

16481649

16491650

@@ -1652,13 +1653,18 @@ const velocity = await web3data.token.getVelocity('0x06012c8cf97bead5deae237070f
16521653

16531654
```javascript
16541655

1656+
// Latest
1657+
const latestHodlers = await web3data.token.getHolders('0x06012c8cf97bead5deae237070f9587f8e7a266d');
1658+
1659+
// Historical
1660+
const historicalHodlers = await web3data.token.getHolders('0x06012c8cf97bead5deae237070f9587f8e7a266d', {holderAddresses: '0xbbf0cc1c63f509d48a4674e270d26d80ccaf6022'});
16551661
```
16561662

16571663

16581664
##### Returns
16591665

16601666

1661-
- `Promise.<object>`
1667+
- `Promise.<object>` The latest or historical token holders for the specified address.
16621668

16631669

16641670

@@ -1667,7 +1673,7 @@ const velocity = await web3data.token.getVelocity('0x06012c8cf97bead5deae237070f
16671673
#### getHoldersHistorical(hash[, filterOptions])
16681674

16691675

1670-
1676+
Retrieves the historical (time series) token holders for the specified token address. If the `holderAddresses` filter is present it will return historical data.
16711677

16721678

16731679

@@ -1685,14 +1691,14 @@ const velocity = await web3data.token.getVelocity('0x06012c8cf97bead5deae237070f
16851691
##### Examples
16861692

16871693
```javascript
1688-
1694+
const historicalHolders = getHoldersHistorical('0x06012c8cf97bead5deae237070f9587f8e7a266d', {holderAddresses: '0xbbf0cc1c63f509d48a4674e270d26d80ccaf6022'})
16891695
```
16901696

16911697

16921698
##### Returns
16931699

16941700

1695-
- `Promise.<object>`
1701+
- `Promise.<object>` The historical (time series) token holders for the specified token address.
16961702

16971703

16981704

src/token.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,37 +81,51 @@ class Token {
8181
}
8282

8383
/**
84+
* Retrieves the latest or historical token holders for the specified address.
85+
*
8486
* @param {string} hash - The address for which to retrieve token holders.
85-
* @param {object} [filterOptions] - The filters associated with the request.
86-
* @returns {Promise<object>}
87+
* @param {object} [filterOptions] - The filters associated with the request. See [docs](https://docs.amberdata.io/reference#get-token-velocity) for more details.
88+
* @param {object} [filterOptions.holderAddresses] - The address for which to retrieve token holders.
89+
* @returns {Promise<object>} The latest or historical token holders for the specified address.
8790
* @example
91+
*
92+
* // Latest
93+
* const latestHodlers = await web3data.token.getHolders('0x06012c8cf97bead5deae237070f9587f8e7a266d');
94+
*
95+
* // Historical
96+
* const historicalHodlers = await web3data.token.getHolders('0x06012c8cf97bead5deae237070f9587f8e7a266d', {holderAddresses: '0xbbf0cc1c63f509d48a4674e270d26d80ccaf6022'});
8897
*/
8998
getHolders(hash, filterOptions = {}) {
9099
throwIf(is.notHash(hash), NO_ADDRESS)
100+
let subendpoint = 'holders/latest'
101+
let formatter = {formatter: recordsFormatter}
102+
103+
// If this parameter is present then we want historical data
104+
if (filterOptions.holderAddresses) {
105+
subendpoint = 'holders/historical'
106+
formatter = {}
107+
}
108+
91109
return get(this.web3data, {
92110
hash,
93111
endpoint: ENDPOINT,
94-
subendpoint: 'holders/latest',
112+
subendpoint,
95113
filterOptions
96-
}).then(onFulfilled, onError)
114+
}).then(onFulfilled.bind(formatter), onError)
97115
}
98116

99117
/**
118+
* Retrieves the historical (time series) token holders for the specified token address. If the `holderAddresses` filter is present it will return historical data.
119+
*
100120
* @param {string} hash - The address for which to retrieve token holders.
101121
* @param {object} [filterOptions] - The filters associated with the request.
102-
* @returns {Promise<object>}
122+
* @returns {Promise<object>} The historical (time series) token holders for the specified token address.
103123
* @example
124+
* const historicalHolders = getHoldersHistorical('0x06012c8cf97bead5deae237070f9587f8e7a266d', {holderAddresses: '0xbbf0cc1c63f509d48a4674e270d26d80ccaf6022'})
104125
*/
105-
getHoldersHistorical(hash, filterOptions = {}) {
106-
throwIf(is.notHash(hash), NO_ADDRESS)
126+
getHoldersHistorical(hash, filterOptions) {
107127
throwIf(is.notInObject(filterOptions, 'holderAddresses'), NO_HOLDER_ADDRESS)
108-
109-
return get(this.web3data, {
110-
hash,
111-
endpoint: ENDPOINT,
112-
subendpoint: 'holders/historical',
113-
filterOptions
114-
}).then(onFulfilled, onError)
128+
return this.getHolders(hash, filterOptions)
115129
}
116130

117131
/**

test/recordings/token_2491017778/recording.har

Lines changed: 721 additions & 292 deletions
Large diffs are not rendered by default.

test/token.test.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,6 @@ returnsObjectWithField.title = (providedTitle = '', input) => `Successfully call
7171
* -------- Test Tokens -------- *
7272
**********************************/
7373

74-
test([rejectsPromise], {method: 'getHolders' }, NO_ADDRESS)
75-
76-
test('Successfully gets address Token Holders Historical', async t => {
77-
const filters = {holderAddresses: '0xbbf0cc1c63f509d48a4674e270d26d80ccaf6022'}
78-
const holdersHistorical = await t.context.web3data.token.getHoldersHistorical(TOKEN_ADDRESS, filters);
79-
t.true({}.hasOwnProperty.call(holdersHistorical.data[0], 'timestamp'))
80-
});
81-
82-
test('Rejects promise if no token address supplied', async t => {
83-
let filters = {holderAddresses: ADDRESS}
84-
await t.throwsAsync(async () => {
85-
await t.context.web3data.token.getHoldersHistorical('',filters);
86-
}, { instanceOf: Error, message: NO_ADDRESS })
87-
});
88-
89-
test('Rejects promise if no holder address supplied', async t => {
90-
await t.throwsAsync(async () => {
91-
await t.context.web3data.token.getHoldersHistorical(TOKEN_ADDRESS);
92-
}, { instanceOf: Error, message: NO_HOLDER_ADDRESS })
93-
});
94-
9574
/*********** Test getRankings() ***********/
9675
test('Successfully calls getRankings()', async t => {
9776
const {data: [rank1]} = await t.context.web3data.token.getRankings();
@@ -137,6 +116,35 @@ test('Successfully calls getVelocity() - with filters', async t => {
137116
t.true(isISOFormat(volume.metadata.startDate))
138117
})
139118

119+
/*********** Test getHolders() ***********/
120+
test([rejectsPromise], {method: 'getHolders' }, NO_ADDRESS)
121+
122+
test('Successfully calls getHolders() - latest', async t => {
123+
const [holder] = await t.context.web3data.token.getHolders(TOKEN_ADDRESS);
124+
t.true(holder.hasProp('holderAddress'))
125+
})
126+
127+
test('Successfully calls getHolders() - latest with filters', async t => {
128+
const [holder] = await t.context.web3data.token.getHolders(TOKEN_ADDRESS, {includePrice: true});
129+
t.true(holder.hasProp('price'))
130+
})
131+
132+
133+
test('Successfully calls getHolders() - historical', async t => {
134+
const holders = await t.context.web3data.token.getHolders(TOKEN_ADDRESS, {holderAddresses: '0xbbf0cc1c63f509d48a4674e270d26d80ccaf6022'});
135+
t.true(holders.hasProp('metadata'))
136+
t.true(holders.metadata.hasProp('columns'))
137+
t.is(holders.metadata.columns[0], 'holder*')
138+
})
139+
test('Successfully calls getHolders() - historical with filters', async t => {
140+
const holders = await t.context.web3data.token.getHolders(TOKEN_ADDRESS, {holderAddresses: '0xbbf0cc1c63f509d48a4674e270d26d80ccaf6022', includePrice: true, timeFormat: 'iso'});
141+
t.true(holders.hasProp('metadata'))
142+
t.true(holders.metadata.hasProp('columns'))
143+
t.is(holders.metadata.columns[0], 'holder*')
144+
t.true(isISOFormat(holders.data[0].timestamp))
145+
})
146+
147+
140148
/*********** Test getSupplies() ***********/
141149
test([rejectsPromise], {method: 'getSupplies'}, NO_ADDRESS)
142150
test('Successfully calls getSupplies() - latest', async t => {

0 commit comments

Comments
 (0)