Skip to content
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
14 changes: 14 additions & 0 deletions __mocks__/uuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const uuid = require('uuid');

// mock the v4 function of uuid lib to make sure it returns the fixed id for testing
const v4 = () => '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d';

module.exports.NIL = uuid.NIL;
module.exports.v1 = uuid.v1;
module.exports.v3 = uuid.v3;
module.exports.v5 = uuid.v5;
module.exports.parse = uuid.parse;
module.exports.validate = uuid.validate;
module.exports.stringify = uuid.stringify;

module.exports.v4 = v4;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
"web3-provider-engine": "^16.0.3"
},
"devDependencies": {
"@keystonehq/bc-ur-registry-eth": "^0.9.0",
"@keystonehq/metamask-airgapped-keyring": "^0.3.0",
"@lavamoat/allow-scripts": "^1.0.6",
"@metamask/auto-changelog": "^2.5.0",
"@metamask/eslint-config": "^9.0.0",
Expand Down
18 changes: 18 additions & 0 deletions src/assets/AccountTrackerController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { stub, spy } from 'sinon';
import HttpProvider from 'ethjs-provider-http';
import type { ContactEntry } from '../user/AddressBookController';
import { PreferencesController } from '../user/PreferencesController';
import * as utils from '../util';
import { AccountTrackerController } from './AccountTrackerController';

const provider = new HttpProvider(
Expand Down Expand Up @@ -44,6 +45,23 @@ describe('AccountTrackerController', () => {
expect(controller.state.accounts[address].balance).toBeDefined();
});

it('should sync balance with addresses', async () => {
const address = '0xc38bf1ad06ef69f0c04e29dbeb4152b4175f0a8d';
const queryStub = stub(utils, 'query');
const controller = new AccountTrackerController(
{
onPreferencesStateChange: stub(),
getIdentities: () => {
return {};
},
},
{ provider },
);
queryStub.returns(Promise.resolve('0x10'));
const result = await controller.syncBalanceWithAddresses([address]);
expect(result[address].balance).toBe('0x10');
});

it('should sync addresses', () => {
const controller = new AccountTrackerController(
{
Expand Down
41 changes: 38 additions & 3 deletions src/assets/AccountTrackerController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import EthQuery from 'eth-query';
import { Mutex } from 'async-mutex';
import { BaseController, BaseConfig, BaseState } from '../BaseController';
import { BaseConfig, BaseController, BaseState } from '../BaseController';
import { PreferencesState } from '../user/PreferencesController';
import { BNToHex, query, safelyExecuteWithTimeout } from '../util';

Expand Down Expand Up @@ -146,15 +146,50 @@ export class AccountTrackerController extends BaseController<
*/
refresh = async () => {
this.syncAccounts();
const { accounts } = this.state;
const accounts = { ...this.state.accounts };
for (const address in accounts) {
await safelyExecuteWithTimeout(async () => {
const balance = await query(this.ethQuery, 'getBalance', [address]);
accounts[address] = { balance: BNToHex(balance) };
this.update({ accounts: { ...accounts } });
});
}
this.update({ accounts });
};

/**
* Sync accounts balances with some additional addresses.
*
* @param addresses - the additional addresses, may be hardware wallet addresses.
* @returns accounts - addresses with synced balance
*/
async syncBalanceWithAddresses(
addresses: string[],
): Promise<Record<string, { balance: string }>> {
return await Promise.all(
addresses.map(
(address): Promise<[string, string] | undefined> => {
return safelyExecuteWithTimeout(async () => {
const balance = await query(this.ethQuery, 'getBalance', [address]);
return [address, balance];
});
},
),
).then((value) => {
return value.reduce((obj, item) => {
if (!item) {
return obj;
}

const [address, balance] = item;
return {
...obj,
[address]: {
balance,
},
};
}, {});
});
}
}

export default AccountTrackerController;
Loading