Created
May 11, 2019 07:58
-
-
Save justinbarry/558d76a313b89c9a07c401f0c9fa0954 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type SubscriptionKey = string; | |
interface Connector { | |
trackAccount(account:string); | |
}; | |
interface EventEmitter { | |
emit(key, payload); | |
subscribe():SubscriptionKey | |
unsubscribe(subscriptionKey):boolean; | |
}; | |
interface Signer { | |
getAddress():string; | |
}; | |
export class API implements EventEmitter { | |
// Assume connector contains the provider for this exercise. | |
_connector: Connector | null; | |
_signer: Signer | null; | |
// This would be used for initialization and reinitialization (switching accounts) | |
initialize(signer, connector?: Connector) { | |
if (connector) { | |
this._connector = connector; | |
} | |
this.switchAccount(signer); | |
}; | |
async switchAccount(signer: Signer) { | |
this._connector.trackAccount(signer.getAddress()); | |
const prev = this._signer;; | |
this._signer= signer; | |
this.emit('LoggedOut', prev); | |
this.emit('LoggedIn', signer); | |
} | |
getAccount() { | |
// return account class with account specific apis. | |
} | |
// The rest of the api goes here.... | |
getOrders(maketId: number) { | |
// make calls to lower level stuff to make it happen. Note the consumer of this class doesn't need to know contract addresses. | |
} | |
emit(key, payload) {} | |
subscribe(): {} | |
unsubscribe(subscriptionKey): boolean { | |
return true; | |
} | |
} | |
export const api = new API(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment