Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
Fix multple storage
Browse files Browse the repository at this point in the history
  • Loading branch information
burtovoy committed Nov 17, 2017
1 parent f562e80 commit 93a06fa
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Web3 = require('web3')
import * as transport from './lib/transport'
import * as storage from './lib/storage'
import {default as Storage, engine, build, channels as storageChannels} from './lib/storage'
import * as channel from './lib/channel'
import { default as Sender } from './lib/sender'
import { ChannelContract, contract } from './lib/channel'
Expand Down Expand Up @@ -88,6 +88,7 @@ export default class Machinomy {
private engine: string
private databaseFile: string
private minimumChannelAmount?: BigNumber
private storage: Storage

/**
* Create an instance of Machinomy.
Expand All @@ -112,6 +113,7 @@ export default class Machinomy {
} else {
this.databaseFile = 'machinomy'
}
this.storage = build(this.web3, this.databaseFile, 'shared', false, this.engine)
}

/**
Expand All @@ -133,8 +135,7 @@ export default class Machinomy {
buy (options: BuyOptions): Promise<BuyResult> {
let _transport = transport.build()
let contract = channel.contract(this.web3)
let s = storage.build(this.web3, this.databaseFile, 'shared', false, this.engine)
let client = new Sender(this.web3, this.account, contract, _transport, s, this.minimumChannelAmount)
let client = new Sender(this.web3, this.account, contract, _transport, this.storage, this.minimumChannelAmount)
return client.buyMeta(options).then((res: any) => {
return { channelId: res.payment.channelId, token: res.token }
})
Expand All @@ -156,8 +157,7 @@ export default class Machinomy {
let _value = new BigNumber(value)
let channelContract = contract(this.web3)
return new Promise((resolve, reject) => {
let s = storage.build(this.web3, this.databaseFile, 'shared', false, this.engine)
s.channels.firstById(channelId).then((paymentChannel) => {
this.storage.channels.firstById(channelId).then((paymentChannel) => {
if (paymentChannel) {
channelContract.deposit(this.account, paymentChannel, _value).then(() => {
resolve()
Expand All @@ -173,9 +173,8 @@ export default class Machinomy {
channels (): Promise<PaymentChannel[]> {
const namespace = 'shared'
return new Promise((resolve, reject) => {
let _storage = storage.build(this.web3, this.databaseFile, 'shared', false, this.engine)
let engine = storage.engine(this.databaseFile, false, this.engine)
storage.channels(this.web3, engine, namespace).all().then(found => {
let _engine = engine(this.databaseFile, false, this.engine)
storageChannels(this.web3, _engine, namespace).all().then(found => {
found = found.filter((ch) => {
if (ch.state < 2) {
return true
Expand All @@ -201,10 +200,12 @@ export default class Machinomy {
* For more details on how payment channels work refer to a website.
*/
close (channelId: string): Promise<void> {
if (typeof channelId !== 'string') {
throw 'channelId must be a string'
}
let channelContract = contract(this.web3)
return new Promise((resolve, reject) => {
let s = storage.build(this.web3, this.databaseFile, 'shared', false, this.engine)
s.channels.firstById(channelId).then((paymentChannel) => {
this.storage.channels.firstById(channelId).then((paymentChannel) => {
if (paymentChannel) {
if (paymentChannel.sender === this.account) {
this.settle(channelContract, paymentChannel).then(resolve).catch(reject)
Expand All @@ -220,25 +221,22 @@ export default class Machinomy {
* Save payment into the storage and return an id of the payment. The id can be used by {@link Machinomy.paymentById}.
*/
acceptPayment (payment: Payment): Promise <string> {
let s = storage.build(this.web3, this.databaseFile, 'shared', false, this.engine)
let server = receiver.build(this.web3, this.account, s)
let server = receiver.build(this.web3, this.account, this.storage)
return server.acceptPayment(payment)
}

/**
* Return information about the payment by id.
*/
paymentById (id: string): Promise <Payment | null> {
let s = storage.build(this.web3, this.databaseFile, 'shared', false, this.engine)
return s.payments.findByToken(id)
return this.storage.payments.findByToken(id)
}

/**
* @deprecated Use {@link Machinomy.paymentById} to find information about payment and verify it.
*/
verifyToken (token: string): Promise <boolean> {
let s = storage.build(this.web3, this.databaseFile, 'shared', false, this.engine)
let server = receiver.build(this.web3, this.account, s)
let server = receiver.build(this.web3, this.account, this.storage)
return server.acceptToken(token)
}

Expand Down Expand Up @@ -270,8 +268,7 @@ export default class Machinomy {
private claim (channelContract: ChannelContract, paymentChannel: PaymentChannel): Promise<void> {
return new Promise((resolve, reject) => {
const channelId = paymentChannel.channelId
let s = storage.build(this.web3, this.databaseFile, 'shared', false, this.engine)
s.payments.firstMaximum(channelId).then((paymentDoc: any) => {
this.storage.payments.firstMaximum(channelId).then((paymentDoc: any) => {
channelContract.claim(paymentChannel.receiver, paymentChannel, paymentDoc.value, Number(paymentDoc.v), paymentDoc.r, paymentDoc.s).then(value => {
resolve()
}).catch(reject)
Expand Down

0 comments on commit 93a06fa

Please sign in to comment.