Skip to content

Commit

Permalink
Commit top-up only if it passed the init period
Browse files Browse the repository at this point in the history
  • Loading branch information
r-czajkowski committed Jul 31, 2020
1 parent b5f765b commit 02ffda2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
3 changes: 3 additions & 0 deletions solidity/dashboard/src/css/buttons.less
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ button, .btn {

&:hover:disabled, &hover[disabled] {
cursor: not-allowed;
&::after {
opacity: 0
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion solidity/dashboard/src/pages/TokensPageContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ const useSubscribeToTopUpsEvents = () => {
if (delegation) {
dispatch({ type: TOP_UP_INITIATED, payload: event.returnValues })
}
if (!delegation.isFromGrant) {
if (delegation && !delegation.isFromGrant) {
refreshKeepTokenBalance()
}
}
Expand Down
15 changes: 9 additions & 6 deletions solidity/dashboard/src/reducers/tokens-page.reducer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { add, sub, gt } from "../utils/arithmetics.utils"
import { findIndexAndObject, compareEthAddresses } from "../utils/array.utils"
import { isSameEthAddress } from "../utils/general.utils"
import moment from "moment"

export const REFRESH_KEEP_TOKEN_BALANCE = "REFRESH_KEEP_TOKEN_BALANCE"
export const REFRESH_GRANT_TOKEN_BALANCE = "REFRESH_GRANT_TOKEN_BALANCE"
Expand Down Expand Up @@ -123,10 +124,7 @@ const tokensPageReducer = (state, action) => {
({ operatorAddress }) =>
!isSameEthAddress(operatorAddress, action.payload.operator)
),
delegations: updateDelegationAmount(
[...state.delegations],
action.payload
),
delegations: topUpCompleted([...state.delegations], action.payload),
}
case GRANT_DEPOSITED:
return {
Expand Down Expand Up @@ -231,7 +229,11 @@ const topUpInitiated = (topUps, { operator, topUp }) => {
)
if (indexInArray === null) {
return [
{ operatorAddress: operator, availableTopUpAmount: topUp },
{
operatorAddress: operator,
availableTopUpAmount: topUp,
createdAt: moment.unix(),
},
...topUps,
]
}
Expand All @@ -240,12 +242,13 @@ const topUpInitiated = (topUps, { operator, topUp }) => {
topUpToUpdate.availableTopUpAmount,
topUp
)
topUpToUpdate.createdAt = moment.unix()
topUps[indexInArray] = topUpToUpdate

return topUps
}

const updateDelegationAmount = (delegations, { operator, newAmount }) => {
const topUpCompleted = (delegations, { operator, newAmount }) => {
const { indexInArray, obj: delegationsToUpdate } = findIndexAndObject(
"operatorAddress",
operator,
Expand Down
22 changes: 14 additions & 8 deletions solidity/dashboard/src/services/top-ups.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const commitTopUp = async (operator, onTransactionHashCallback) => {
.on("transactionHash", onTransactionHashCallback)
}

export const fetchAvailableTopUps = async (_, operators) => {
export const fetchAvailableTopUps = async (web3Context, operators) => {
const availableTopUps = []

if (isEmptyArray(operators)) {
Expand All @@ -31,27 +31,33 @@ export const fetchAvailableTopUps = async (_, operators) => {
).reduce(reduceByOperator, {})

for (const operator of operators) {
const topUpInitiated = toupUpsInitiatedByOperator[operator]
const topUpCompleted = topUpsCompletedByOperator[operator]
const topUpsInitiated = toupUpsInitiatedByOperator[operator]
const topUpsCompleted = topUpsCompletedByOperator[operator]

const latestTopUpCompletedEvent = !isEmptyArray(topUpCompleted)
? [...topUpCompleted].pop()
const latestTopUpCompletedEvent = !isEmptyArray(topUpsCompleted)
? [...topUpsCompleted].pop()
: undefined

if (!isEmptyArray(topUpInitiated)) {
if (!isEmptyArray(topUpsInitiated)) {
const availableOperatorTopUps = latestTopUpCompletedEvent
? topUpInitiated.filter(
? topUpsInitiated.filter(
filterByAfterLatestCompletedTopUp(latestTopUpCompletedEvent)
)
: topUpInitiated
: topUpsInitiated
const availableTopUpAmount = availableOperatorTopUps.reduce(
reduceAmount,
0
)
const createdAt = (
await web3Context.eth.getBlock(
topUpsInitiated[topUpsInitiated.length - 1].blockNumber
)
).timestamp
if (availableTopUpAmount > 0)
availableTopUps.push({
operatorAddress: operator,
availableTopUpAmount,
createdAt,
})
}
}
Expand Down

0 comments on commit 02ffda2

Please sign in to comment.