Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Fixes contribution process
Browse files Browse the repository at this point in the history
Resolves #14018

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed May 4, 2018
1 parent 8f73858 commit d580e2c
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 46 deletions.
14 changes: 6 additions & 8 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -1675,12 +1675,6 @@ const getStateInfo = (state, parsedData) => {
reconcileStamp: parsedData.reconcileStamp
}

const oldReconcileStamp = ledgerState.getInfoProp(state, 'reconcileStamp')

if (oldReconcileStamp && newInfo.reconcileStamp > oldReconcileStamp) {
state = ledgerState.setAboutProp(state, 'status', ledgerStatuses.IN_PROGRESS)
}

try {
let passphrase = ledgerClient.prototype.getWalletPassphrase(parsedData)
if (passphrase) {
Expand Down Expand Up @@ -2035,6 +2029,10 @@ const onCallback = (state, result, delayTime) => {
return state
}

if (result.getIn(['currentReconcile', 'timestamp']) === 0) {
state = ledgerState.setAboutProp(state, 'status', ledgerStatuses.IN_PROGRESS)
}

const newAddress = result.getIn(['properties', 'wallet', 'addresses', 'BAT'])
const oldAddress = ledgerState.getInfoProps(state).getIn(['addresses', 'BAT'])

Expand Down Expand Up @@ -2462,8 +2460,8 @@ const run = (state, delayTime) => {
return
}

const publishers = ledgerState.getPublisher(state)
if (publishers.isEmpty() && client.isReadyToReconcile(synopsis)) {
const publishers = ledgerState.getAboutProp(state, 'synopsis') || Immutable.List()
if (isList(publishers) && publishers.isEmpty() && client.isReadyToReconcile(synopsis)) {
setNewTimeUntilReconcile()
}

Expand Down
4 changes: 2 additions & 2 deletions app/common/lib/ledgerUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const formattedDateFromTimestamp = (timestamp, dateFormat) => {
return format(new Date(timestamp), dateFormat, {locale: navigator.language})
}

const walletStatus = (ledgerData) => {
const walletStatus = (ledgerData, settings) => {
let status = {}

if (ledgerData == null) {
Expand All @@ -110,7 +110,7 @@ const walletStatus = (ledgerData) => {
const transactions = ledgerData.get('transactions')
const pendingFunds = Number(ledgerData.get('unconfirmed') || 0)
const balance = Number(ledgerData.get('balance') || 0)
const minBalance = ledgerState.getContributionAmount(null, ledgerData.get('contributionAmount'))
const minBalance = ledgerState.getContributionAmount(null, ledgerData.get('contributionAmount'), settings)

if (pendingFunds + balance < minBalance) {
status.id = 'insufficientFundsStatus'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ class EnabledContent extends ImmutableComponent {

render () {
const ledgerData = this.props.ledgerData
const walletStatusText = walletStatus(ledgerData)
const walletStatusText = walletStatus(ledgerData, this.props.settings)
const contributionAmount = ledgerState.getContributionAmount(null, ledgerData.get('contributionAmount'), this.props.settings)
const amountList = ledgerData.get('monthlyAmounts') || ledgerUtil.defaultMonthlyAmounts

Expand Down
3 changes: 2 additions & 1 deletion app/renderer/components/preferences/paymentsTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ class PaymentsTab extends ImmutableComponent {
</SectionTitleWrapper>
{
this.enabled
? <EnabledContent settings={this.props.settings}
? <EnabledContent
settings={this.props.settings}
onChangeSetting={this.props.onChangeSetting}
ledgerData={this.props.ledgerData}
showOverlay={this.props.showOverlay}
Expand Down
163 changes: 129 additions & 34 deletions test/unit/app/browser/api/ledgerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('ledger api unit tests', function () {
let walletPassphraseReturn
let updater
let aboutPreferencesState
let isReadyToReconcile = false

// constants
const videoId = 'youtube_kLiLOkzLetE'
Expand Down Expand Up @@ -159,7 +160,7 @@ describe('ledger api unit tests', function () {
getPromotion: () => {},
setPromotion: () => {},
setTimeUntilReconcile: () => {},
isReadyToReconcile: () => {}
isReadyToReconcile: () => isReadyToReconcile
}
window.getWalletPassphrase = (parsedData) => {
if (walletPassphraseReturn === 'error') {
Expand Down Expand Up @@ -2155,6 +2156,40 @@ describe('ledger api unit tests', function () {
assert.deepEqual(result.toJS(), expectedState.toJS())
})
})

describe('status', function () {
it('null case', function () {
const result = ledgerApi.onCallback(defaultAppState, null)
assert.deepEqual(result.toJS(), defaultAppState.toJS())
})

it('reconcile is not in progress', function () {
const result = ledgerApi.onCallback(defaultAppState, Immutable.fromJS({
properties: {}
}))
assert.deepEqual(result.toJS(), defaultAppState.toJS())
})

it('reconcile is in progress (first time)', function () {
const result = ledgerApi.onCallback(defaultAppState, Immutable.fromJS({
currentReconcile: {
timestamp: 0
}
}))
const expectedState = defaultAppState
.setIn(['ledger', 'about', 'status'], ledgerStatuses.IN_PROGRESS)
assert.deepEqual(result.toJS(), expectedState.toJS())
})

it('reconcile is in progress (second time)', function () {
const result = ledgerApi.onCallback(defaultAppState, Immutable.fromJS({
currentReconcile: {
timestamp: 1
}
}))
assert.deepEqual(result.toJS(), defaultAppState.toJS())
})
})
})

describe('uintKeySeed', function () {
Expand Down Expand Up @@ -2875,50 +2910,110 @@ describe('ledger api unit tests', function () {
})
})

describe('BSCrun', function () {
let clientBallotsSpy
describe('run', function () {
describe('ballots', function () {
let clientBallotsSpy

before(() => {
ledgerApi.setSynopsis({
toJSON: () => {
return {
publishers: {
'clifton.io': {
visits: 1
before(() => {
ledgerApi.setSynopsis({
toJSON: () => {
return {
publishers: {
'clifton.io': {
visits: 1
}
}
}
},
winners: () => {
return []
}
},
winners: () => {
return []
}
})
ledgerApi.setClient(ledgerClientObject)
clientBallotsSpy = sinon.spy(ledgerClientObject, 'ballots')
})
ledgerApi.setClient(ledgerClientObject)
clientBallotsSpy = sinon.spy(ledgerClientObject, 'ballots')
})

afterEach(() => {
clientBallotsSpy.reset()
})
afterEach(() => {
clientBallotsSpy.reset()
})

after(() => {
clientBallotsSpy.restore()
ledgerApi.setSynopsis(undefined)
})
after(() => {
clientBallotsSpy.restore()
ledgerApi.setSynopsis(undefined)
})

it('exits if state is undefined', function () {
ledgerApi.run(undefined, 10)
assert.equal(clientBallotsSpy.notCalled, true)
})
it('exits if state is undefined', function () {
ledgerApi.run(undefined, 10)
assert.equal(clientBallotsSpy.notCalled, true)
})

it('exits if delayTime is undefined', function () {
ledgerApi.run(defaultAppState)
assert.equal(clientBallotsSpy.notCalled, true)
})

it('exits if delayTime is undefined', function () {
ledgerApi.run(defaultAppState)
assert.equal(clientBallotsSpy.notCalled, true)
it('gets balance count from client', function () {
ledgerApi.run(defaultAppState, 10)
assert.equal(clientBallotsSpy.calledOnce, true)
})
})

it('gets balance count from client', function () {
ledgerApi.run(defaultAppState, 10)
assert.equal(clientBallotsSpy.calledOnce, true)
describe('publishers check', function () {
before(() => {
ledgerApi.setClient(ledgerClientObject)
ledgerSetTimeUntilReconcile.reset()
})

afterEach(() => {
ledgerSetTimeUntilReconcile.reset()
isReadyToReconcile = false
})

it('null check', function () {
ledgerApi.run(defaultAppState, 10)
assert(ledgerSetTimeUntilReconcile.notCalled)
})

it('synopsis is broken', function () {
const state = defaultAppState
.setIn(['ledger', 'about', 'synopsis'], 'test')
ledgerApi.run(state, 10)
assert(ledgerSetTimeUntilReconcile.notCalled)
})

it('table is empty and we are not ready to reconcile', function () {
const state = defaultAppState
.setIn(['ledger', 'about', 'synopsis'], Immutable.fromJS([{
publisherKey: 'clifton.io'
}]))
ledgerApi.run(state, 10)
assert(ledgerSetTimeUntilReconcile.notCalled)
})

it('table is not empty and we are not ready to reconcile', function () {
const state = defaultAppState
.setIn(['ledger', 'about', 'synopsis'], Immutable.fromJS([{
publisherKey: 'clifton.io'
}]))
ledgerApi.run(state, 10)
assert(ledgerSetTimeUntilReconcile.notCalled)
})

it('table is not empty and we are ready to reconcile', function () {
isReadyToReconcile = true
const state = defaultAppState
.setIn(['ledger', 'about', 'synopsis'], Immutable.fromJS([{
publisherKey: 'clifton.io'
}]))
ledgerApi.run(state, 10)
assert(ledgerSetTimeUntilReconcile.notCalled)
})

it('table is empty and we are ready to reconcile', function () {
isReadyToReconcile = true
ledgerApi.run(defaultAppState, 10)
assert(ledgerSetTimeUntilReconcile.calledOnce)
})
})
})

Expand Down

0 comments on commit d580e2c

Please sign in to comment.