This repository has been archived by the owner on Dec 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into liam/appInstanceAdjudicator
- Loading branch information
Showing
58 changed files
with
2,050 additions
and
783 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { AppDefinition } from "./structs"; | ||
import { Provider } from "./provider"; | ||
import { Address, AppABIEncodings } from "./types"; | ||
|
||
export class AppFactory { | ||
constructor(readonly appDefinition: AppDefinition) {} | ||
constructor( | ||
readonly provider: Provider, | ||
readonly appId: Address, | ||
readonly encodings: AppABIEncodings | ||
) {} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { AppInstanceID } from "./simple-types"; | ||
import { AppInstanceID, AppInstanceInfo } from "./types"; | ||
|
||
export class AppInstance { | ||
constructor(readonly id: AppInstanceID) {} | ||
readonly id: AppInstanceID; | ||
constructor(readonly info: AppInstanceInfo) { | ||
this.id = info.id; | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,82 @@ | ||
import { | ||
INodeProvider, | ||
NodeMessage, | ||
NodeMessageType, | ||
NodeQueryData, | ||
QueryType | ||
} from "@counterfactual/node-provider"; | ||
|
||
import cuid from "cuid"; | ||
|
||
import { AppFactory } from "./app-factory"; | ||
import { AppInstance } from "./app-instance"; | ||
import { AppDefinition } from "./structs"; | ||
import { INodeProvider, Node } from "./types"; | ||
|
||
export enum CounterfactualEventType { | ||
INSTALL = "cf_install", | ||
PROPOSE_INSTALL = "cf_proposeInstall", | ||
REJECT_INSTALL = "cf_rejectInstall" | ||
} | ||
export import DappEventType = Node.EventName; | ||
|
||
export interface CounterfactualEvent { | ||
readonly eventType: CounterfactualEventType; | ||
export interface DappEvent { | ||
readonly type: DappEventType; | ||
readonly data: any; // TODO | ||
} | ||
|
||
const NODE_REQUEST_TIMEOUT = 1500; | ||
|
||
export class Provider { | ||
private readonly requestListeners: { | ||
[requestId: string]: (msg: NodeMessage) => void; | ||
[requestId: string]: (msg: Node.Message) => void; | ||
} = {}; | ||
|
||
constructor(readonly nodeProvider: INodeProvider) { | ||
this.nodeProvider.onMessage(this.onNodeMessage.bind(this)); | ||
} | ||
|
||
async getAppInstances(): Promise<AppInstance[]> { | ||
const response = await this.sendNodeRequest(NodeMessageType.QUERY, { | ||
queryType: QueryType.GET_APP_INSTANCES | ||
}); | ||
return (response.data as NodeQueryData).appInstances!.map( | ||
({ id }) => new AppInstance(id) | ||
const response = await this.callNodeMethod( | ||
Node.MethodName.GET_APP_INSTANCES, | ||
{} | ||
); | ||
const result = response.result as Node.GetAppInstancesResult; | ||
return result.appInstances.map(info => new AppInstance(info)); | ||
} | ||
|
||
createAppFactory(appDefinition: AppDefinition): AppFactory { | ||
return new AppFactory(appDefinition); | ||
} | ||
|
||
on( | ||
eventType: CounterfactualEventType, | ||
callback: (e: CounterfactualEvent) => void | ||
) { | ||
on(eventName: DappEventType, callback: (e: DappEvent) => void) { | ||
// TODO: support notification observers | ||
} | ||
|
||
private async sendNodeRequest( | ||
messageType: NodeMessageType, | ||
data: any | ||
): Promise<NodeMessage> { | ||
private async callNodeMethod( | ||
methodName: Node.MethodName, | ||
params: Node.MethodParams | ||
): Promise<Node.MethodResponse> { | ||
const requestId = cuid(); | ||
return new Promise<NodeMessage>((resolve, reject) => { | ||
this.requestListeners[requestId] = msg => { | ||
if (msg.messageType === NodeMessageType.ERROR) { | ||
return reject(msg); | ||
return new Promise<Node.MethodResponse>((resolve, reject) => { | ||
this.requestListeners[requestId] = response => { | ||
if (response.type === Node.ErrorType.ERROR) { | ||
return reject(response.data); | ||
} | ||
if (response.type !== methodName) { | ||
return reject({ | ||
errorName: "unexpected_message_type", | ||
message: `Unexpected response type. Expected ${methodName}, got ${ | ||
response.type | ||
}` | ||
}); | ||
} | ||
resolve(msg); | ||
resolve(response as Node.MethodResponse); | ||
}; | ||
setTimeout(() => { | ||
if (this.requestListeners[requestId] !== undefined) { | ||
reject(new Error(`Request timed out: ${requestId}`)); | ||
delete this.requestListeners[requestId]; | ||
} | ||
}, NODE_REQUEST_TIMEOUT); | ||
this.nodeProvider.postMessage({ | ||
this.nodeProvider.sendMessage({ | ||
requestId, | ||
messageType, | ||
data | ||
params, | ||
type: methodName | ||
}); | ||
}); | ||
} | ||
|
||
private onNodeMessage(message: NodeMessage) { | ||
const { requestId } = message; | ||
if (this.requestListeners[requestId]) { | ||
this.requestListeners[requestId](message); | ||
delete this.requestListeners[requestId]; | ||
private onNodeMessage(message: Node.Message) { | ||
const requestId = (message as Node.MethodResponse).requestId; | ||
if (requestId) { | ||
if (this.requestListeners[requestId]) { | ||
this.requestListeners[requestId](message); | ||
delete this.requestListeners[requestId]; | ||
} | ||
} else { | ||
// TODO: notify observers | ||
} | ||
// TODO: notify observers | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// https://github.com/counterfactual/monorepo/blob/master/packages/cf.js/API_REFERENCE.md#data-types | ||
import { BigNumber } from "ethers/utils"; | ||
|
||
import { ABIEncoding, Address } from "./simple-types"; | ||
|
||
export interface AppInstanceInfo { | ||
id: string; | ||
appId: Address; | ||
abiEncodings: AppABIEncodings; | ||
asset: BlockchainAsset; | ||
myDeposit: BigNumber; | ||
peerDeposit: BigNumber; | ||
timeout: BigNumber; | ||
} | ||
|
||
export interface AppABIEncodings { | ||
stateEncoding: ABIEncoding; | ||
actionEncoding?: ABIEncoding; | ||
} | ||
|
||
export enum AssetType { | ||
ETH = 0, | ||
ERC20 = 1, | ||
Other = 2 | ||
} | ||
|
||
export interface BlockchainAsset { | ||
assetType: AssetType; | ||
token?: Address; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { | ||
ABIEncoding, | ||
Address, | ||
AppAction, | ||
AppInstanceID, | ||
AppState, | ||
Bytes32 | ||
} from "./simple-types"; | ||
|
||
import { | ||
AppABIEncodings, | ||
AppInstanceInfo, | ||
AssetType, | ||
BlockchainAsset | ||
} from "./data-types"; | ||
|
||
import { INodeProvider, Node } from "./node-protocol"; | ||
|
||
export { | ||
INodeProvider, | ||
Node, | ||
AssetType, | ||
AppABIEncodings, | ||
BlockchainAsset, | ||
AppInstanceInfo, | ||
ABIEncoding, | ||
Address, | ||
AppAction, | ||
AppInstanceID, | ||
AppState, | ||
Bytes32 | ||
}; |
Oops, something went wrong.