Skip to content

Commit

Permalink
Feature/tx local state logs (#436)
Browse files Browse the repository at this point in the history
* added property to transaction if it was confirmed locally

* added logic to capture the time an account was added/imported

* coverted confirmDevice to enum and removed console.logs

* updated package.json

* Update src/transaction/TransactionController.ts

Co-authored-by: Erik Marks <25517051+rekmarks@users.noreply.github.com>

* format

* updated variable name to make more sense

* Update src/transaction/TransactionController.ts

Updated string enum values

Co-authored-by: Erik Marks <25517051+rekmarks@users.noreply.github.com>

* reverted the version number

* move importTime to preferences controller and added test cases

* updated setting of import time and test cases

* updated test case

* updated format

* updated enum name per PR suggestion

* format

Co-authored-by: Erik Marks <25517051+rekmarks@users.noreply.github.com>
  • Loading branch information
2 people authored and MajorLift committed Oct 11, 2023
1 parent a152acd commit 773af0c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package-lock.json
# tarball from `yarn pack`
*.tgz

.DS_STORE
coverage
dist
docs
15 changes: 14 additions & 1 deletion src/transaction/TransactionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ export enum TransactionStatus {
unapproved = 'unapproved',
}

/**
* Options for wallet device.
*/
export enum WalletDevice {
MM_MOBILE = 'metamask_mobile',
MM_EXTENSION = 'metamask_extension',
OTHER = 'other_device',
}

type TransactionMetaBase = {
isTransfer?: boolean;
transferInformation?: {
Expand All @@ -101,6 +110,7 @@ type TransactionMetaBase = {
transaction: Transaction;
transactionHash?: string;
blockNumber?: string;
deviceConfirmedOn?: WalletDevice;
};

/**
Expand All @@ -112,6 +122,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 deviceConfirmedOn - string to indicate what device the transaction was confirmed
* @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 +422,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 deviceConfirmedOn - enum to indicate what device the transaction was confirmed to 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, deviceConfirmedOn?: WalletDevice): Promise<Result> {
const network = this.context.NetworkController as NetworkController;
const { transactions } = this.state;
transaction = normalizeTransaction(transaction);
Expand All @@ -434,6 +446,7 @@ export class TransactionController extends BaseController<TransactionConfig, Tra
status: TransactionStatus.unapproved as TransactionStatus.unapproved,
time: Date.now(),
transaction,
deviceConfirmedOn,
};

try {
Expand Down
2 changes: 2 additions & 0 deletions src/user/AddressBookController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import BaseController, { BaseConfig, BaseState } from '../BaseController';
*
* @property address - Hex address of a recipient account
* @property name - Nickname associated with this address
* @property importTime - Data time when an account as created/imported
*/
export interface ContactEntry {
address: string;
name: string;
importTime?: number;
}

/**
Expand Down
44 changes: 23 additions & 21 deletions src/user/PreferencesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ describe('PreferencesController', () => {
const controller = new PreferencesController();
controller.addIdentities(['foo']);
controller.addIdentities(['foo']);
expect(controller.state.identities).toEqual({
'0xfoO': {
address: '0xfoO',
name: 'Account 1',
},
});
expect(controller.state.identities['0xfoO'].address).toEqual('0xfoO');
expect(controller.state.identities['0xfoO'].name).toEqual('Account 1');
expect(controller.state.identities['0xfoO'].importTime).toBeLessThanOrEqual(Date.now());
});

it('should remove identity', () => {
Expand All @@ -49,24 +46,27 @@ describe('PreferencesController', () => {
const controller = new PreferencesController();
controller.addIdentities(['foo', 'bar']);
controller.syncIdentities(['foo', 'bar']);
expect(controller.state.identities).toEqual({
'0xbar': { address: '0xbar', name: 'Account 2' },
'0xfoO': { address: '0xfoO', name: 'Account 1' },
});
expect(controller.state.identities['0xfoO'].address).toEqual('0xfoO');
expect(controller.state.identities['0xfoO'].name).toEqual('Account 1');
expect(controller.state.identities['0xfoO'].importTime).toBeLessThanOrEqual(Date.now());
expect(controller.state.identities['0xbar'].address).toEqual('0xbar');
expect(controller.state.identities['0xbar'].name).toEqual('Account 2');
expect(controller.state.identities['0xbar'].importTime).toBeLessThanOrEqual(Date.now());
controller.syncIdentities(['foo']);
expect(controller.state.identities).toEqual({
'0xfoO': { address: '0xfoO', name: 'Account 1' },
});
expect(controller.state.identities['0xfoO'].address).toEqual('0xfoO');
expect(controller.state.identities['0xfoO'].name).toEqual('Account 1');
expect(controller.state.selectedAddress).toBe('0xfoO');
});

it('should add new identities', () => {
const controller = new PreferencesController();
controller.updateIdentities(['foo', 'bar']);
expect(controller.state.identities).toEqual({
'0xbar': { address: '0xbar', name: 'Account 2' },
'0xfoO': { address: '0xfoO', name: 'Account 1' },
});
expect(controller.state.identities['0xfoO'].address).toEqual('0xfoO');
expect(controller.state.identities['0xfoO'].name).toEqual('Account 1');
expect(controller.state.identities['0xfoO'].importTime).toBeLessThanOrEqual(Date.now());
expect(controller.state.identities['0xbar'].address).toEqual('0xbar');
expect(controller.state.identities['0xbar'].name).toEqual('Account 2');
expect(controller.state.identities['0xbar'].importTime).toBeLessThanOrEqual(Date.now());
});

it('should not update existing identities', () => {
Expand All @@ -75,10 +75,12 @@ describe('PreferencesController', () => {
{ identities: { '0xbar': { address: '0xbar', name: 'Custom name' } } },
);
controller.updateIdentities(['foo', 'bar']);
expect(controller.state.identities).toEqual({
'0xbar': { address: '0xbar', name: 'Custom name' },
'0xfoO': { address: '0xfoO', name: 'Account 1' },
});
expect(controller.state.identities['0xfoO'].address).toEqual('0xfoO');
expect(controller.state.identities['0xfoO'].name).toEqual('Account 1');
expect(controller.state.identities['0xfoO'].importTime).toBeLessThanOrEqual(Date.now());
expect(controller.state.identities['0xbar'].address).toEqual('0xbar');
expect(controller.state.identities['0xbar'].name).toEqual('Custom name');
expect(controller.state.identities['0xbar'].importTime).toBeUndefined();
});

it('should remove identities', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/user/PreferencesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class PreferencesController extends BaseController<BaseConfig, Preference
}
const identityCount = Object.keys(identities).length;

identities[address] = { name: `Account ${identityCount + 1}`, address };
identities[address] = { name: `Account ${identityCount + 1}`, address, importTime: Date.now() };
});
this.update({ identities: { ...identities } });
}
Expand Down Expand Up @@ -187,6 +187,7 @@ export class PreferencesController extends BaseController<BaseConfig, Preference
ids[address] = oldIdentities[address] || {
address,
name: `Account ${index + 1}`,
importTime: Date.now(),
};
return ids;
}, {});
Expand Down

0 comments on commit 773af0c

Please sign in to comment.