diff --git a/packages/example-web-app/src/App.tsx b/packages/example-web-app/src/App.tsx index 243b5a78..c713204e 100644 --- a/packages/example-web-app/src/App.tsx +++ b/packages/example-web-app/src/App.tsx @@ -1,6 +1,6 @@ import React, { useEffect, useState } from 'react'; -import test from '@cerc-io/nitro-client'; +import { test } from '@cerc-io/nitro-client'; import logo from './logo.svg'; import './App.css'; diff --git a/packages/nitro-client/.eslintrc.json b/packages/nitro-client/.eslintrc.json index 4677afc5..af53ccf4 100644 --- a/packages/nitro-client/.eslintrc.json +++ b/packages/nitro-client/.eslintrc.json @@ -13,6 +13,8 @@ { "devDependencies": ["webpack.*.ts", "test/**/*.test.ts"] } - ] + ], + "max-classes-per-file": "off", + "import/prefer-default-export": "off" } } diff --git a/packages/nitro-client/src/channel/consensus_channel/consensus_channel.ts b/packages/nitro-client/src/channel/consensus_channel/consensus_channel.ts new file mode 100644 index 00000000..a4b7293d --- /dev/null +++ b/packages/nitro-client/src/channel/consensus_channel/consensus_channel.ts @@ -0,0 +1,5 @@ +// Proposal is a proposal either to add or to remove a guarantee. +// +// Exactly one of {toAdd, toRemove} should be non nil. +// TODO Implement +export class Proposal {} diff --git a/packages/nitro-client/src/client/README.md b/packages/nitro-client/src/client/README.md new file mode 100644 index 00000000..e03a47e3 --- /dev/null +++ b/packages/nitro-client/src/client/README.md @@ -0,0 +1 @@ +# Client diff --git a/packages/nitro-client/src/client/client.ts b/packages/nitro-client/src/client/client.ts new file mode 100644 index 00000000..8eee11bf --- /dev/null +++ b/packages/nitro-client/src/client/client.ts @@ -0,0 +1,18 @@ +import { MessageService } from './engine/messageservice/messageservice'; +import { ChainService } from './engine/chainservice/chainservice'; +import { Store } from './engine/store/store'; +import { PolicyMaker } from './engine/policy-maker'; +import { VoucherManager } from '../payments/voucher-manager'; +import { Engine } from './engine/engine'; + +export class Client { + // The core business logic of the client + private engine: Engine; + + private vm: VoucherManager; + + constructor(msg: MessageService, chain: ChainService, store: Store, policymaker: PolicyMaker) { + this.vm = new VoucherManager(); + this.engine = new Engine(this.vm, msg, chain, store, policymaker); + } +} diff --git a/packages/nitro-client/src/client/engine/chainservice/chainservice.ts b/packages/nitro-client/src/client/engine/chainservice/chainservice.ts new file mode 100644 index 00000000..3a2a92b4 --- /dev/null +++ b/packages/nitro-client/src/client/engine/chainservice/chainservice.ts @@ -0,0 +1,26 @@ +import { GoReceivingChannelPlaceholder } from '../../../go-channel'; +import { ChainTransaction } from '../../../protocols/interfaces'; + +// Event dictates which methods all chain events must implement +// TODO: Add methods +export interface Event {} + +// TODO: Add eth chainservice implementation +export interface ChainService { + eventFeed (): GoReceivingChannelPlaceholder; + + // TODO: Use protocols chain transaction type + // TODO: Can throw an error + sendTransaction (tx: ChainTransaction): void; + + // TODO: Use Address type + getConsensusAppAddress (): string; + + getVirtualPaymentAppAddress (): string; + + // TODO: Can throw an error + getChainId (): number; + + // TODO: Can throw an error + close (): void; +} diff --git a/packages/nitro-client/src/client/engine/engine.ts b/packages/nitro-client/src/client/engine/engine.ts new file mode 100644 index 00000000..6542cfb9 --- /dev/null +++ b/packages/nitro-client/src/client/engine/engine.ts @@ -0,0 +1,66 @@ +import { GoChannelPlaceholder, GoReceivingChannelPlaceholder } from '../../go-channel'; +import { MessageService } from './messageservice/messageservice'; +import { ChainService, Event as ChainEvent } from './chainservice/chainservice'; +import { Store } from './store/store'; +import { PolicyMaker } from './policy-maker'; +import { MetricsRecorder } from './metrics'; +import { VoucherManager } from '../../payments/voucher-manager'; +import { ObjectiveRequest } from '../../protocols/interfaces'; +import { Message } from '../../protocols/messages'; +import { Proposal } from '../../channel/consensus_channel/consensus_channel'; + +export class Engine { + objectiveRequestsFromAPI?: GoChannelPlaceholder; + + paymentRequestsFromAPI?: GoChannelPlaceholder; + + private fromChain?: GoReceivingChannelPlaceholder; + + private fromMsg?: GoReceivingChannelPlaceholder; + + private fromLedger?: GoChannelPlaceholder; + + private toApi?: GoChannelPlaceholder; + + private stop?: GoChannelPlaceholder; + + private msg: MessageService; + + private chain: ChainService; + + // A Store for persisting and restoring important data + private store: Store; + + // A PolicyMaker decides whether to approve or reject objectives + private policymaker: PolicyMaker; + + // logger zerolog.Logger + + private metrics?: MetricsRecorder; + + private vm: VoucherManager; + + constructor( + vm: VoucherManager, + msg: MessageService, + chain: ChainService, + store: Store, + policymaker: PolicyMaker, + ) { + this.vm = vm; + + this.msg = msg; + this.chain = chain; + this.store = store; + + this.policymaker = policymaker; + } +} + +export type PaymentRequest = { + channelId: string + amount: number +}; + +// TODO Implement +export class EngineEvent {} diff --git a/packages/nitro-client/src/client/engine/messageservice/messageservice.ts b/packages/nitro-client/src/client/engine/messageservice/messageservice.ts new file mode 100644 index 00000000..06153574 --- /dev/null +++ b/packages/nitro-client/src/client/engine/messageservice/messageservice.ts @@ -0,0 +1,19 @@ +import { GoReceivingChannelPlaceholder } from '../../../go-channel'; +import { Message } from '../../../protocols/messages'; + +// TODO: Add p2p implementation +// TODO: Add tests +export interface MessageService { + // TODO: Update comments + + // Out returns a chan for receiving messages from the message service + out (): GoReceivingChannelPlaceholder; + + // Send is for sending messages with the message service + // TODO: Use protocols message type + send (msg: Message): void; + + // Close closes the message service + // TODO: Can throw an error + close (): void; +} diff --git a/packages/nitro-client/src/client/engine/metrics.ts b/packages/nitro-client/src/client/engine/metrics.ts new file mode 100644 index 00000000..9a3cba86 --- /dev/null +++ b/packages/nitro-client/src/client/engine/metrics.ts @@ -0,0 +1,2 @@ +// TODO: Implement +export class MetricsRecorder {} diff --git a/packages/nitro-client/src/client/engine/policy-maker.ts b/packages/nitro-client/src/client/engine/policy-maker.ts new file mode 100644 index 00000000..fea33eba --- /dev/null +++ b/packages/nitro-client/src/client/engine/policy-maker.ts @@ -0,0 +1,2 @@ +// TODO: Add methods +export interface PolicyMaker {} diff --git a/packages/nitro-client/src/client/engine/store/store.ts b/packages/nitro-client/src/client/engine/store/store.ts new file mode 100644 index 00000000..023d7d85 --- /dev/null +++ b/packages/nitro-client/src/client/engine/store/store.ts @@ -0,0 +1,2 @@ +// TODO: Add methods +export interface Store {} diff --git a/packages/nitro-client/src/go-channel.ts b/packages/nitro-client/src/go-channel.ts new file mode 100644 index 00000000..72ee2747 --- /dev/null +++ b/packages/nitro-client/src/go-channel.ts @@ -0,0 +1,6 @@ +/* eslint max-classes-per-file: 0 */ +/* eslint @typescript-eslint/no-unused-vars: 0 */ + +// TODO: Replace +export class GoChannelPlaceholder {} +export class GoReceivingChannelPlaceholder {} diff --git a/packages/nitro-client/src/index.ts b/packages/nitro-client/src/index.ts index c0169849..d96f8c03 100644 --- a/packages/nitro-client/src/index.ts +++ b/packages/nitro-client/src/index.ts @@ -1,8 +1,8 @@ -const test = (): string => { +export const test = (): string => { // eslint-disable-next-line no-console console.log('Test from nitro-client'); return 'test output'; }; -export default test; +export { Client } from './client/client'; diff --git a/packages/nitro-client/src/payments/voucher-manager.ts b/packages/nitro-client/src/payments/voucher-manager.ts new file mode 100644 index 00000000..ac09d40d --- /dev/null +++ b/packages/nitro-client/src/payments/voucher-manager.ts @@ -0,0 +1,2 @@ +// TODO: Implement +export class VoucherManager {} diff --git a/packages/nitro-client/src/protocols/interfaces.ts b/packages/nitro-client/src/protocols/interfaces.ts new file mode 100644 index 00000000..b25749cb --- /dev/null +++ b/packages/nitro-client/src/protocols/interfaces.ts @@ -0,0 +1,7 @@ +// ChainTransaction defines the interface that every transaction must implement +// TODO: Add methods +export interface ChainTransaction {} + +// ObjectiveRequest is a request to create a new objective. +// TODO: Add methods +export interface ObjectiveRequest {} diff --git a/packages/nitro-client/src/protocols/messages.ts b/packages/nitro-client/src/protocols/messages.ts new file mode 100644 index 00000000..c9f6f529 --- /dev/null +++ b/packages/nitro-client/src/protocols/messages.ts @@ -0,0 +1,2 @@ +// TODO Implement +export class Message {} diff --git a/packages/nitro-client/test/index.test.ts b/packages/nitro-client/test/index.test.ts index cf8aa724..006894d9 100644 --- a/packages/nitro-client/test/index.test.ts +++ b/packages/nitro-client/test/index.test.ts @@ -1,5 +1,5 @@ import { expect } from 'chai'; -import test from '../src/index'; +import { test } from '../src/index'; describe('testFunction', () => { it('should return the expected result', () => {