-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add initial SmartTransactionsController #1
Changes from all commits
89290d2
d4f9ced
1917dcf
e4f2d9e
6dd2986
5e540a3
af08070
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { NetworkState } from '@metamask/controllers'; | ||
import SmartTransactionsController, { | ||
DEFAULT_INTERVAL, | ||
} from './SmartTransactionsController'; | ||
|
||
describe('SmartTransactionsController', () => { | ||
let smartTransactionsController: SmartTransactionsController; | ||
let networkListener: (networkState: NetworkState) => void; | ||
|
||
beforeEach(() => { | ||
smartTransactionsController = new SmartTransactionsController({ | ||
onNetworkStateChange: (listener) => { | ||
networkListener = listener; | ||
}, | ||
}); | ||
}); | ||
|
||
afterEach(async () => { | ||
jest.clearAllMocks(); | ||
await smartTransactionsController.stop(); | ||
}); | ||
|
||
it('should initialize with default config', () => { | ||
expect(smartTransactionsController.config).toStrictEqual({ | ||
interval: DEFAULT_INTERVAL, | ||
allowedNetworks: ['1'], | ||
chainId: '', | ||
}); | ||
}); | ||
|
||
it('should initialize with default state', () => { | ||
expect(smartTransactionsController.state).toStrictEqual({ | ||
smartTransactions: {}, | ||
userOptIn: undefined, | ||
}); | ||
}); | ||
|
||
describe('onNetworkChange', () => { | ||
it('should be triggered', () => { | ||
networkListener({ provider: { chainId: '52' } } as NetworkState); | ||
expect(smartTransactionsController.config.chainId).toBe('52'); | ||
}); | ||
|
||
it('should call poll', () => { | ||
const pollSpy = jest.spyOn(smartTransactionsController, 'poll'); | ||
networkListener({ provider: { chainId: '2' } } as NetworkState); | ||
expect(pollSpy).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('poll', () => { | ||
it('should poll with interval', async () => { | ||
const interval = 35000; | ||
const pollSpy = jest.spyOn(smartTransactionsController, 'poll'); | ||
const updateSmartTransactionsSpy = jest.spyOn( | ||
smartTransactionsController, | ||
'updateSmartTransactions', | ||
); | ||
expect(pollSpy).toHaveBeenCalledTimes(0); | ||
expect(updateSmartTransactionsSpy).toHaveBeenCalledTimes(0); | ||
networkListener({ provider: { chainId: '1' } } as NetworkState); | ||
expect(pollSpy).toHaveBeenCalledTimes(1); | ||
expect(updateSmartTransactionsSpy).toHaveBeenCalledTimes(1); | ||
await smartTransactionsController.stop(); | ||
jest.useFakeTimers(); | ||
await smartTransactionsController.poll(interval); | ||
expect(pollSpy).toHaveBeenCalledTimes(2); | ||
expect(updateSmartTransactionsSpy).toHaveBeenCalledTimes(2); | ||
jest.advanceTimersByTime(interval); | ||
expect(pollSpy).toHaveBeenCalledTimes(3); | ||
expect(updateSmartTransactionsSpy).toHaveBeenCalledTimes(3); | ||
await smartTransactionsController.stop(); | ||
jest.clearAllTimers(); | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it('should not updateSmartTransactions on unsupported networks', async () => { | ||
const updateSmartTransactionsSpy = jest.spyOn( | ||
smartTransactionsController, | ||
'updateSmartTransactions', | ||
); | ||
expect(updateSmartTransactionsSpy).not.toHaveBeenCalled(); | ||
networkListener({ provider: { chainId: '56' } } as NetworkState); | ||
expect(updateSmartTransactionsSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('setOptInState', () => { | ||
it('should set optIn state', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a little thing, but I like to avoid the word |
||
smartTransactionsController.setOptInState(true); | ||
expect(smartTransactionsController.state.userOptIn).toBe(true); | ||
smartTransactionsController.setOptInState(false); | ||
expect(smartTransactionsController.state.userOptIn).toBe(false); | ||
smartTransactionsController.setOptInState(undefined); | ||
expect(smartTransactionsController.state.userOptIn).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { | ||
BaseConfig, | ||
BaseController, | ||
BaseState, | ||
NetworkState, | ||
util, | ||
} from '@metamask/controllers'; | ||
|
||
export const DEFAULT_INTERVAL = 5 * 60 * 1000; | ||
|
||
export interface SmartTransactionsConfig extends BaseConfig { | ||
interval: number; | ||
chainId: string; | ||
allowedNetworks: string[]; | ||
} | ||
|
||
export interface SmartTransactionsState extends BaseState { | ||
smartTransactions: Record<string, any>; | ||
userOptIn: boolean | undefined; | ||
} | ||
|
||
export default class SmartTransactionsController extends BaseController< | ||
SmartTransactionsConfig, | ||
SmartTransactionsState | ||
> { | ||
private handle?: NodeJS.Timeout; | ||
|
||
constructor( | ||
{ | ||
onNetworkStateChange, | ||
}: { | ||
onNetworkStateChange: ( | ||
listener: (networkState: NetworkState) => void, | ||
) => void; | ||
}, | ||
config?: Partial<SmartTransactionsConfig>, | ||
state?: Partial<SmartTransactionsState>, | ||
) { | ||
super(config, state); | ||
this.defaultConfig = { | ||
interval: DEFAULT_INTERVAL, | ||
chainId: '', | ||
allowedNetworks: ['1'], | ||
}; | ||
|
||
this.defaultState = { | ||
smartTransactions: {}, | ||
userOptIn: undefined, | ||
}; | ||
this.initialize(); | ||
onNetworkStateChange(({ provider }) => { | ||
const { chainId } = provider; | ||
this.configure({ chainId }); | ||
this.poll(); | ||
}); | ||
this.poll(); | ||
} | ||
|
||
setOptInState(state: boolean | undefined): void { | ||
this.update({ userOptIn: state }); | ||
} | ||
|
||
async poll(interval?: number): Promise<void> { | ||
const { chainId, allowedNetworks } = this.config; | ||
interval && this.configure({ interval }, false, false); | ||
this.handle && clearTimeout(this.handle); | ||
if (!allowedNetworks.includes(chainId)) { | ||
return; | ||
} | ||
await util.safelyExecute(() => this.updateSmartTransactions()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
this.handle = setTimeout(() => { | ||
this.poll(this.config.interval); | ||
}, this.config.interval); | ||
} | ||
|
||
async stop() { | ||
this.handle && clearTimeout(this.handle); | ||
} | ||
|
||
async updateSmartTransactions() { | ||
// | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import greeter from '.'; | ||
import SmartTransactionsController from './SmartTransactionsController'; | ||
import DefaultExport from '.'; | ||
|
||
describe('Test', () => { | ||
it('greets', () => { | ||
const name = 'Huey'; | ||
const result = greeter(name); | ||
expect(result).toStrictEqual('Hello, Huey!'); | ||
describe('default export', () => { | ||
it('exports SmartTransactionsController', () => { | ||
expect( | ||
new DefaultExport({ | ||
onNetworkStateChange: jest.fn(), | ||
}), | ||
).toBeInstanceOf(SmartTransactionsController); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export default function greeter(name: string): string { | ||
return `Hello, ${name}!`; | ||
} | ||
import SmartTransactionsController from './SmartTransactionsController'; | ||
|
||
export default SmartTransactionsController; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1