Skip to content

Instantly share code, notes, and snippets.

@justinbarry
Created May 11, 2019 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save justinbarry/558d76a313b89c9a07c401f0c9fa0954 to your computer and use it in GitHub Desktop.
Save justinbarry/558d76a313b89c9a07c401f0c9fa0954 to your computer and use it in GitHub Desktop.
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