Skip to content

Instantly share code, notes, and snippets.

@pgebheim
Forked from justinbarry/ExampleApi.ts
Created May 11, 2019 17:22
Show Gist options
  • Save pgebheim/05a26765728ee2d380e4199fea2dfe6d to your computer and use it in GitHub Desktop.
Save pgebheim/05a26765728ee2d380e4199fea2dfe6d 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