Skip to content

Instantly share code, notes, and snippets.

@brianosaurus
Last active May 7, 2019 16:58
Show Gist options
  • Save brianosaurus/6407318b3d7900fb992526866664dd69 to your computer and use it in GitHub Desktop.
Save brianosaurus/6407318b3d7900fb992526866664dd69 to your computer and use it in GitHub Desktop.
API ideas
Proposing this structure for getters and senders.
export interface Pong {
response: string;
}
export class Ping<TBigNumber> {
public static PingParams = t.type({});
public statuf PongParams = t.type({ respopnse: t.string });
@Getter()
public static async pingGetter<TBigNumber>(db: DB<TBigNumber>, params: t.TypeOf<typeof Ping.PingParams>): Promise<Pong> {
return {
response: "pong",
};
}
@Sender()
public static async pingSender<TBigNumber>(provider: AugurProvider, params: t.TypeOf<typeof Ping.PingParams>): Promise<Pong> {
return Ping.PongParams.decode(provider.send(params.encode());
}
}
The provider is responsible for the following operations:
{ subscribe, unsubscribe, send, connect, disconnect }
Event handlers can have a similar format
export PING_EVENT = "ping";
export class Ping<TBigNumber> {
public static PongParams = t.type({ response: t.string });
@HandleEvent()
public static async ping<TBigNumber>(params: any): Promise<Pong> {
return Ping.PongParams.decode(params);
}
}
@brianosaurus
Copy link
Author

brianosaurus commented May 6, 2019

  1. The Getter adds the method and the method's name (as the json-rpc method) to a map in API. It also ads the params type to the map so that the params can be type checked at runtime. When a json-rpc request comes in it looks up the getter in the map by the request name and routes it to that getter while converting the params to the right type.
  2. The @sender decorator would work like
const foo: returnType = API.send(PING_REQUEST, params_type);

API would look up the sender in a map in API and route it. When it gets routed the correct provider gets passed to the sender.

  1. Happy to use as different term other than provider. Connector?

@brianosaurus
Copy link
Author

Would be nice to make sending not need to know the actual API call name...def possible but have to think about it.

@adrake33
Copy link

adrake33 commented May 6, 2019

This format looks fine to me. Personally, I like Connector or Connection better than Provider. I can't really think of any other better names at the moment.

@bthaile
Copy link

bthaile commented May 7, 2019

What does the caller look like? What are the imports needed? Is each getter encapsulated with request, response and getter?

something like

const connector = new AugurConnector(...)

const ping = new Ping();
const param = new ping.pingParam()
const response: ping.PongParams = ping.Sender(connector, param);

@brianosaurus
Copy link
Author

I think the invocation will be more like

const response = API.send(Ping.PING_REQ, new Ping.PingParams({...});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment