Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tx local state logs #436

Merged
merged 16 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@metamask/controllers",
"version": "6.2.1",
"version": "6.2.2",
sethkfman marked this conversation as resolved.
Show resolved Hide resolved
"description": "Collection of platform-agnostic modules for creating secure data models for cryptocurrency wallets",
"license": "MIT",
"files": [
Expand Down
5 changes: 4 additions & 1 deletion src/assets/AccountTrackerController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('AccountTrackerController', () => {
controller.context = { PreferencesController: { state: { identities: { [address]: {} } } } } as any;
await controller.refresh();
expect(controller.state.accounts[address].balance).toBeDefined();
expect(controller.state.accounts[address].importTime).toBeDefined();
});

it('should sync addresses', () => {
Expand All @@ -39,7 +40,9 @@ describe('AccountTrackerController', () => {
);
controller.context = { PreferencesController: { state: { identities: { baz: {} } } } } as any;
controller.refresh();
expect(controller.state.accounts).toEqual({ baz: { balance: '0x0' } });
expect(controller.state.accounts.baz.balance).toEqual('0x0');
expect(controller.state.accounts.baz.importTime).toBeDefined();
expect(controller.state.accounts.baz.importTime).toBeLessThanOrEqual(Date.now());
});

it('should subscribe to new sibling preference controllers', async () => {
Expand Down
8 changes: 6 additions & 2 deletions src/assets/AccountTrackerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import { BNToHex, query, safelyExecuteWithTimeout } from '../util';
* Account information object
*
* @property balance - Hex string of an account balancec in wei
* @property importTime - Data time when an account as created/imported
sethkfman marked this conversation as resolved.
Show resolved Hide resolved
*/
export interface AccountInformation {
balance: string;
importTime?: number;
}

/**
Expand Down Expand Up @@ -58,7 +60,8 @@ export class AccountTrackerController extends BaseController<AccountTrackerConfi
const newAddresses = addresses.filter((address) => existing.indexOf(address) === -1);
const oldAddresses = existing.filter((address) => addresses.indexOf(address) === -1);
newAddresses.forEach((address) => {
accounts[address] = { balance: '0x0' };
console.log('New Address');
sethkfman marked this conversation as resolved.
Show resolved Hide resolved
accounts[address] = { balance: '0x0', importTime: Date.now() };
});
oldAddresses.forEach((address) => {
delete accounts[address];
Expand Down Expand Up @@ -142,7 +145,8 @@ export class AccountTrackerController extends BaseController<AccountTrackerConfi
for (const address in accounts) {
await safelyExecuteWithTimeout(async () => {
const balance = await query(this.ethQuery, 'getBalance', [address]);
accounts[address] = { balance: BNToHex(balance) };
const { importTime } = accounts[address];
accounts[address] = { balance: BNToHex(balance), importTime };
this.update({ accounts: { ...accounts } });
});
}
Expand Down
6 changes: 5 additions & 1 deletion src/transaction/TransactionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type TransactionMetaBase = {
transaction: Transaction;
transactionHash?: string;
blockNumber?: string;
confirmedLocal?: boolean;
};

/**
Expand All @@ -112,6 +113,7 @@ type TransactionMetaBase = {
* @property id - Generated UUID associated with this transaction
* @property networkID - Network code as per EIP-155 for this transaction
* @property origin - Origin this transaction was sent from
* @property confirmedLocal - Boolean to indicate if the transaction was confirmed on local device
* @property rawTransaction - Hex representation of the underlying transaction
* @property status - String status of this transaction
* @property time - Timestamp associated with this transaction
Expand Down Expand Up @@ -411,9 +413,10 @@ export class TransactionController extends BaseController<TransactionConfig, Tra
*
* @param transaction - Transaction object to add
* @param origin - Domain origin to append to the generated TransactionMeta
* @param confirmedLocalDevice - Boolean to indicate if the transaction was confirmed on local device and append to the generated TransactionMeta
* @returns - Object containing a promise resolving to the transaction hash if approved
*/
async addTransaction(transaction: Transaction, origin?: string): Promise<Result> {
async addTransaction(transaction: Transaction, origin?: string, confirmedLocalDevice?: boolean): Promise<Result> {
const network = this.context.NetworkController as NetworkController;
const { transactions } = this.state;
transaction = normalizeTransaction(transaction);
Expand All @@ -434,6 +437,7 @@ export class TransactionController extends BaseController<TransactionConfig, Tra
status: TransactionStatus.unapproved as TransactionStatus.unapproved,
time: Date.now(),
transaction,
confirmedLocal: confirmedLocalDevice,
sethkfman marked this conversation as resolved.
Show resolved Hide resolved
};

try {
Expand Down