Skip to content
This repository has been archived by the owner on Sep 6, 2024. It is now read-only.

feat: remove threshold parameter from getBalances() #481

Merged
merged 2 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/account/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export function createAccountWithPreset<X, Y, Z>(preset: AccountPreset<X, Y, Z>)
getTotalBalance: () => {
return persistence
.ready()
.then(() => network.getBalances(addresses, 100))
.then(() => network.getBalances(addresses))
.then(({ balances }) => balances.reduce((acc: number, b: number) => (acc += b), 0))
},
getAvailableBalance: () => {
Expand All @@ -257,7 +257,7 @@ export function createAccountWithPreset<X, Y, Z>(preset: AccountPreset<X, Y, Z>)
.then(currentTime => {
const depositsListCopy = [...depositsList]
return network
.getBalances(depositsListCopy.map(({ address }) => tritsToTrytes(address)), 100)
.getBalances(depositsListCopy.map(({ address }) => tritsToTrytes(address)))
.then(({ balances }: { balances: ReadonlyArray<number> }) => {
let acc = 0
depositsListCopy.forEach((input, i) => {
Expand Down Expand Up @@ -287,7 +287,7 @@ export function createAccountWithPreset<X, Y, Z>(preset: AccountPreset<X, Y, Z>)
)
.then(depositsListCopy =>
network
.getBalances(depositsListCopy.map(deposit => deposit.address), 100)
.getBalances(depositsListCopy.map(deposit => deposit.address))
.then(({ balances }: { balances: ReadonlyArray<number> }) =>
depositsListCopy.map((deposit, i) => ({
...deposit,
Expand Down
2 changes: 1 addition & 1 deletion packages/account/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function networkAdapter({ provider }: NetworkParams): Network {

return {
getTrytes: hashes => (hashes.length > 0 ? getTrytes(hashes) : Promise.resolve([])),
getBalance: (address): Promise<number> => getBalances([address], 100).then(({ balances }) => balances[0]),
getBalance: (address): Promise<number> => getBalances([address]).then(({ balances }) => balances[0]),
getBalances,
getConsistency: createCheckConsistency(httpClient),
getLatestInclusion: hashes => (hashes.length > 0 ? getLatestInclusion(hashes) : Promise.resolve([])),
Expand Down
1 change: 0 additions & 1 deletion packages/account/test/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const assertBalance = (address: Trytes, balance: number) =>
.post('/', {
command: IRICommand.GET_BALANCES,
addresses: [noChecksum(address)],
threshold: 100,
})
.times(1)
.reply(200, {
Expand Down
8 changes: 3 additions & 5 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ yarn add @iota/core

* [.getAccountData(seed, options, [callback])](#module_core.getAccountData)

* [.getBalances(addresses, threshold, [tips], [callback])](#module_core.getBalances)
* [.getBalances(addresses, [tips], [callback])](#module_core.getBalances)

* [.getBundle(tailTransactionHash, [callback])](#module_core.getBundle)

Expand Down Expand Up @@ -469,7 +469,7 @@ getAccountData(seed)
```
<a name="module_core.getBalances"></a>

### *core*.getBalances(addresses, threshold, [tips], [callback])
### *core*.getBalances(addresses, [tips], [callback])
**Summary**: Gets the confirmed balances of the given addresses.
**Fulfil**: <code>Balances</code> balances - Object that contains the following:
- balances.addresses: Array of balances in the same order as the `addresses` argument
Expand All @@ -478,13 +478,11 @@ getAccountData(seed)
- balances.duration: The number of milliseconds that it took for the node to return a response
**Reject**: <code>Error</code> error - An error that contains one of the following:
- `INVALID_HASH`: Make sure that the addresses contain only trytes
- `INVALID_THRESHOLD`: Make sure that the threshold is a number greater than zero
- Fetch error: The connected IOTA node's API returned an error. See the [list of error messages](https://docs.iota.org/docs/node-software/0.1/iri/references/api-errors)

| Param | Type | Description |
| --- | --- | --- |
| addresses | <code>Array.&lt;Hash&gt;</code> | Array of addresses |
| threshold | <code>number</code> | Not used, but should be set to 100 until [this issue](https://github.com/iotaledger/iri/issues/1723) is resolved. |
| [tips] | <code>Array.&lt;Hash&gt;</code> | Array of past transaction hashes from which to calculate the balances of the addresses. The balance will be calculated from the latest milestone that references these transactions. |
| [callback] | <code>Callback</code> | Optional callback function |

Expand All @@ -499,7 +497,7 @@ To find the balance of all addresses that belong to your seed, use the [`getAcco

**Example**
```js
getBalances([address], 100)
getBalances([address])
.then( balances => {
console.log(`Balance of the first address: `$balances.balances[0])
console.log(JSON.stringify(transactions));
Expand Down
32 changes: 16 additions & 16 deletions packages/core/src/createGetAccountData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ export const getAccountDataOptions = getOptionsWithDefaults(defaults)

/**
* @method createGetAccountData
*
*
* @summary Creates a new `getAccountData()` method, using a custom Provider instance.
*
* @memberof module:core
*
*
* @ignore
*
* @param {Provider} provider - The Provider object that the method should use to call the node's API endpoints.
Expand All @@ -67,21 +67,21 @@ export const createGetAccountData = (provider: Provider, caller?: string) => {

/**
* This method generates [addresses](https://docs.iota.org/docs/getting-started/0.1/clients/addresses) for a given seed, and searches the Tangle for data about those addresses such as transactions, inputs, and total balance.
*
*
* **Note:** The given seed is used to [generate addresses](https://docs.iota.org/docs/client-libraries/0.1/how-to-guides/js/generate-an-address) on your local device. It is never sent anywhere.
*
*
* If you don't pass an `options.end` argument to this method, it will continue to generate addresses until it finds an unspent one.
*
*
* **Note:** The total balance does not include IOTA tokens on [spent addresses](https://docs.iota.org/docs/getting-started/0.1/clients/addresses#spent-addresses).
*
*
* ## Related methods
*
*
* To find the balance of specific addresses, which don't have to belong to your seed, use the [`getBalances()`]{@link #module_core.getBalances} method.
*
*
* To find only inputs (objects that contain information about addresses with a postive balance), use the [`getInputs()`]{@link #module_core.getInputs} method.
*
*
* @method getAccountData
*
*
* @summary Searches the Tangle for transctions, addresses, and balances that are associated with a given seed.
*
* @memberof module:core
Expand All @@ -92,7 +92,7 @@ export const createGetAccountData = (provider: Provider, caller?: string) => {
* @param {number} [options.security=2] - The [security level](https://docs.iota.org/docs/getting-started/0.1/clients/security-levels) to use to generate the addresses
* @param {number} [options.end] - The key index at which to stop generating addresses
* @param {Callback} [callback] - Optional callback function
*
*
* @example
*
* ```js
Expand All @@ -108,7 +108,7 @@ export const createGetAccountData = (provider: Provider, caller?: string) => {
* ```
*
* @returns {Promise}
*
*
* @fulfil {AccountData} accountData - Object that contains the following:
* - accountData.transfers: (deprecated) Array of transaction objects that contain one of the seed's addresses
* - accountData.transactions: Array of transaction hashes for transactions that contain one of the seed's addresses
Expand All @@ -119,13 +119,13 @@ export const createGetAccountData = (provider: Provider, caller?: string) => {
* - accountData.inputs.security: Security level of the address
* - accountData.inputs.balance: Balance of the address
* - accountData.balance: The total balance of unspent addresses
*
*
* @reject {Error} error - An error that contains one of the following:
* - `INVALID_SEED`: Make sure that the seed contains only trytes
* - `INVALID_SECURITY_LEVEL`: Make sure that the security level is a number between 1 and 3
* - `INVALID_START_OPTION`: Make sure that the `options.start` argument is greater than zero
* - `INVALID_START_END_OPTIONS`: Make sure that the `options.end` argument is not greater than the `options.start` argument by more than 1,000`
* - Fetch error: The connected IOTA node's API returned an error. See the [list of error messages](https://docs.iota.org/docs/node-software/0.1/iri/references/api-errors)
* - Fetch error: The connected IOTA node's API returned an error. See the [list of error messages](https://docs.iota.org/docs/node-software/0.1/iri/references/api-errors)
*/
return (
seed: string,
Expand All @@ -138,7 +138,7 @@ export const createGetAccountData = (provider: Provider, caller?: string) => {
/* tslint:disable-next-line:no-console */
console.warn(
'The returned `accountData.transfers` field is deprecated, therefore do not rely on this field in your applications.\n' +
'Instead, you can get only the transactions that you need by using the transaction hashes returned in the `accountData.transactions` field.'
'Instead, you can get only the transactions that you need by using the transaction hashes returned in the `accountData.transactions` field.'
)
}

Expand Down Expand Up @@ -168,7 +168,7 @@ export const createGetAccountData = (provider: Provider, caller?: string) => {
Promise.all([
getBundlesFromAddresses(addresses, true),
// findTransactions({ addresses }), // Find transactions instead of getBundlesFromAddress as of v2.0.0
getBalances(addresses, 100),
getBalances(addresses),
wereAddressesSpentFrom(addresses),
addresses,
])
Expand Down
37 changes: 14 additions & 23 deletions packages/core/src/createGetBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { TRYTE_WIDTH } from '@iota/converter'
import { TRANSACTION_HASH_LENGTH } from '@iota/transaction'
import * as Promise from 'bluebird'
import * as errors from '../../errors'
import { getBalancesThresholdValidator, isHash, isTrytesOfExactLength, validate } from '../../guards'
import { isHash, isTrytesOfExactLength, validate } from '../../guards'
import { Balances, Callback, GetBalancesCommand, GetBalancesResponse, Hash, IRICommand, Provider } from '../../types'

/**
* @method createGetBalances
*
*
* @summary Creates a new `getBalances()` method, using a custom Provider instance.
*
* @memberof module:core
*
*
* @ignore
*
* @param {Provider} provider - The Provider object that the method should use to call the node's API endpoints.
Expand All @@ -25,25 +25,24 @@ export const createGetBalances = ({ send }: Provider) =>
*
* Any pending output transactions are not included in the balance.
* For example, if a pending output transaction deposits 10 Mi into an address that contains 50 Mi, this method will return a balance of 50 Mi not 60 Mi.
*
*
* ## Related methods
*
*
* To find the balance of all addresses that belong to your seed, use the [`getAccountData()`]{@link #module_core.getAccountData} method.
*
*
* @method getBalances
*
*
* @summary Gets the confirmed balances of the given addresses.
*
*
* @memberof module:core
*
* @param {Hash[]} addresses - Array of addresses
* @param {number} threshold - Not used, but should be set to 100 until [this issue](https://github.com/iotaledger/iri/issues/1723) is resolved.
* @param {Hash[]} [tips] - Array of past transaction hashes from which to calculate the balances of the addresses. The balance will be calculated from the latest milestone that references these transactions.
* @param {Callback} [callback] - Optional callback function
*
*
* @example
* ```js
* getBalances([address], 100)
* getBalances([address])
* .then( balances => {
* console.log(`Balance of the first address: `$balances.balances[0])
* console.log(JSON.stringify(transactions));
Expand All @@ -54,24 +53,18 @@ export const createGetBalances = ({ send }: Provider) =>
* ```
*
* @return {Promise}
*
*
* @fulfil {Balances} balances - Object that contains the following:
* - balances.addresses: Array of balances in the same order as the `addresses` argument
* - balances.references: Either the transaction hash of the latest milestone, or the transaction hashes that were passed to the `tips` argument
* - balances.milestoneIndex: The latest milestone index that confirmed the balance
* - balances.duration: The number of milliseconds that it took for the node to return a response
*
*
* @reject {Error} error - An error that contains one of the following:
* - `INVALID_HASH`: Make sure that the addresses contain only trytes
* - `INVALID_THRESHOLD`: Make sure that the threshold is a number greater than zero
* - Fetch error: The connected IOTA node's API returned an error. See the [list of error messages](https://docs.iota.org/docs/node-software/0.1/iri/references/api-errors)
* - Fetch error: The connected IOTA node's API returned an error. See the [list of error messages](https://docs.iota.org/docs/node-software/0.1/iri/references/api-errors)
*/
(
addresses: ReadonlyArray<Hash>,
threshold: number,
tips?: ReadonlyArray<Hash>,
callback?: Callback<Balances>
): Promise<Balances> => {
(addresses: ReadonlyArray<Hash>, tips?: ReadonlyArray<Hash>, callback?: Callback<Balances>): Promise<Balances> => {
// If no tips are provided, switch arguments
if (tips && typeof tips === 'function') {
callback = tips
Expand All @@ -81,7 +74,6 @@ export const createGetBalances = ({ send }: Provider) =>
return Promise.resolve(
validate(
[addresses, (arr: Hash[]) => arr.every(isHash), errors.INVALID_ADDRESS],
getBalancesThresholdValidator(threshold),
!!tips && [
tips,
(arr: Hash[]) => arr.every(h => isTrytesOfExactLength(h, TRANSACTION_HASH_LENGTH / TRYTE_WIDTH)),
Expand All @@ -93,7 +85,6 @@ export const createGetBalances = ({ send }: Provider) =>
send<GetBalancesCommand, GetBalancesResponse>({
command: IRICommand.GET_BALANCES,
addresses: addresses.map(removeChecksum), // Addresses passed to IRI should not have the checksum
threshold,
...(Array.isArray(tips) && tips.length && { tips }),
})
)
Expand Down
24 changes: 12 additions & 12 deletions packages/core/src/createGetInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ const defaults: GetInputsOptions = {

/**
* @method createGetInputs
*
*
* @summary Creates a new `getInputs()` method, using a custom Provider instance.
*
* @memberof module:core
*
*
* @ignore
*
* @param {Provider} provider - The Provider object that the method should use to call the node's API endpoints.
Expand All @@ -55,17 +55,17 @@ export const createGetInputs = (provider: Provider) => {

/**
* This method generates [addresses](https://docs.iota.org/docs/getting-started/0.1/clients/addresses) for a given seed and finds those that have a positive balance.
*
*
* **Note:** The given seed is used to [generate addresses](https://docs.iota.org/docs/client-libraries/0.1/how-to-guides/js/generate-an-address) on your local device. It is never sent anywhere.
*
*
* To find a certain amount of [IOTA tokens](https://docs.iota.org/docs/getting-started/0.1/clients/token) and return only the addresses that, when combined, contain that amount, pass it to the `options.threshold` argument.
*
*
* ## Related methods
*
*
* You may want to use this method to find inputs for the [`prepareTransfers()`]{@link #module_core.prepareTransfers} method.
*
*
* @method getInputs
*
*
* @summary Finds a seed's addresses that have a positive balance.
*
* @memberof module:core
Expand All @@ -77,7 +77,7 @@ export const createGetInputs = (provider: Provider) => {
* @param {number} [options.end] - The key index at which to stop generating addresses
* @param {number} [options.threshold] - The amount of IOTA tokens that you want to find
* @param {Callback} [callback] - Optional callback function
*
*
* @example
*
* ```js
Expand All @@ -104,7 +104,7 @@ export const createGetInputs = (provider: Provider) => {
* - input.security: The security level of the address
* - input.balance: The amount of IOTA tokens in the address
* - inputs.totalBalance: The combined balance of all addresses
*
*
* @reject {Error} error - An error that contains one of the following:
* - `INVALID_SEED`: Make sure that the seed contains only trytes
* - `INVALID_SECURITY_LEVEL`: Make sure that the security level is a number between 1 and 3
Expand All @@ -113,7 +113,7 @@ export const createGetInputs = (provider: Provider) => {
* - Fetch error: The connected IOTA node's API returned an error. See the [list of error messages](https://docs.iota.org/docs/node-software/0.1/iri/references/api-errors) `
* - `INVALID_THRESHOLD`: Make sure that the threshold is a number greater than zero
* - `INSUFFICIENT_BALANCE`: Make sure that the seed has addresses that contain IOTA tokens
* - Fetch error: The connected IOTA node's API returned an error. See the [list of error messages](https://docs.iota.org/docs/node-software/0.1/iri/references/api-errors)
* - Fetch error: The connected IOTA node's API returned an error. See the [list of error messages](https://docs.iota.org/docs/node-software/0.1/iri/references/api-errors)
*/
return (seed: Trytes, options: Partial<GetInputsOptions> = {}, callback?: Callback<Inputs>): Promise<Inputs> => {
const { start, end, security, threshold } = getInputsOptions(options)
Expand All @@ -123,7 +123,7 @@ export const createGetInputs = (provider: Provider) => {
.then(newAddressOptions => getNewAddress(seed, newAddressOptions))
.then(allAddresses => asArray(allAddresses))
.then(allAddresses =>
getBalances(allAddresses, 100)
getBalances(allAddresses)
.then(({ balances }) => createInputsObject(allAddresses, balances, start, security))
.then(res => filterByThreshold(res, threshold))
.tap(inputs => hasSufficientBalance(inputs, threshold))
Expand Down
Loading