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

Commit

Permalink
Merge pull request #105 from xJeneKx/master
Browse files Browse the repository at this point in the history
Configure minimum channel amount in Machinomy constructor
  • Loading branch information
ukstv committed Nov 15, 2017
2 parents eccd33e + 1065ca0 commit f562e80
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
8 changes: 5 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export default class Machinomy {
private web3: Web3
private engine: string
private databaseFile: string
private minimumChannelAmount?: BigNumber

/**
* Create an instance of Machinomy.
Expand All @@ -101,10 +102,11 @@ export default class Machinomy {
* @param account - Ethereum account address that sends the money. Make sure it is managed by Web3 instance passed as `web3` param.
* @param web3 - Prebuilt web3 instance that manages the account and signs payments.
*/
constructor (account: string, web3: Web3, options: MachinomyOptions) {
constructor (account: string, web3: Web3, options: MachinomyOptions, minimumChannelAmount?: number | BigNumber) {
this.account = account
this.web3 = web3
this.engine = options.engine || 'nedb'
if (minimumChannelAmount) this.minimumChannelAmount = new BigNumber(minimumChannelAmount)
if (options.databaseFile) {
this.databaseFile = options.databaseFile
} else {
Expand Down Expand Up @@ -132,7 +134,7 @@ export default class Machinomy {
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)
let client = new Sender(this.web3, this.account, contract, _transport, s, this.minimumChannelAmount)
return client.buyMeta(options).then((res: any) => {
return { channelId: res.payment.channelId, token: res.token }
})
Expand Down Expand Up @@ -232,7 +234,7 @@ export default class Machinomy {
}

/**
* @deprecated Use {@link Machinomy.paymentById} to find information about payment and verify it.
* @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)
Expand Down
14 changes: 11 additions & 3 deletions lib/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ export default class Sender {
contract: ChannelContract
transport: Transport
storage: Storage
minimumChannelAmount: BigNumber

constructor (web3: Web3, account: string, contract: ChannelContract, transport: Transport, storage: Storage) {
constructor (web3: Web3, account: string, contract: ChannelContract, transport: Transport, storage: Storage, minimumChannelAmount: BigNumber = new BigNumber(0)) {
this.web3 = web3
this.account = account
this.contract = contract
this.transport = transport
this.storage = storage
this.minimumChannelAmount = minimumChannelAmount
}

/**
Expand Down Expand Up @@ -144,7 +146,10 @@ export default class Sender {
if (paymentChannel) {
return this.existingChannel(uri, paymentRequired, paymentChannel)
} else {
let value = paymentRequired.price.times(10) // FIXME Total value of the channel
let value = paymentRequired.price.times(10)
if (!this.minimumChannelAmount.equals(0) && this.minimumChannelAmount.greaterThan(value)) {
value = this.minimumChannelAmount
}
return this.freshChannel(uri, paymentRequired, value, opts) // Build new channel
}
})
Expand Down Expand Up @@ -220,7 +225,10 @@ export default class Sender {
if (paymentChannel) {
return this.existingChannel(uri, paymentRequired, paymentChannel)
} else {
let value = paymentRequired.price.times(10) // FIXME Total value of the channel
let value = paymentRequired.price.times(10)
if (!this.minimumChannelAmount.equals(0) && this.minimumChannelAmount.greaterThan(value)) {
value = this.minimumChannelAmount
}
return this.freshChannel(uri, paymentRequired, value) // Build new channel
}
})
Expand Down

0 comments on commit f562e80

Please sign in to comment.