Skip to content

Commit

Permalink
balance check in status response for min 5 transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaamani committed Jul 25, 2023
1 parent d01546e commit d894ea4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ app.get('/status', async (req, res) => {
avatar: 'https://example.com/avatar.png',
})
const hasEnoughFunds = await joy.invitingAccountHasFundsToGift(
exampleGiftMembershipTx
exampleGiftMembershipTx,
5
)

if (!hasEnoughFunds) {
res.status(503).send({
isSynced: true,
hasEnoughFunds: false,
message: 'Inviting account does not have enough funds to gift membership',
message: 'Inviting account has limited funds for at most 5 new members.',
limit,
})
return
Expand Down Expand Up @@ -146,7 +147,7 @@ app.post('/register', async (req, res) => {
}

processingRequest.lock(async () => {
const end = metrics.response_time.startTimer()
const stopTimer = metrics.response_time.startTimer()
try {
await register(
req.ip,
Expand All @@ -170,7 +171,7 @@ app.post('/register', async (req, res) => {
1
)
}
metrics.response_time.observe(end())
stopTimer()
})
})

Expand Down
9 changes: 7 additions & 2 deletions src/joyApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,21 @@ export class JoyApi {
})
}

// Checks if balance has enough to gift N new memberships
async invitingAccountHasFundsToGift(
tx: SubmittableExtrinsic<'promise'>
tx: SubmittableExtrinsic<'promise'>,
N: number = 1
): Promise<boolean> {
const balance = await this.api.derive.balances.all(
this.signingPair!.address
)
const membershipFee = await this.api.query.members.membershipPrice()
const credit = createType('u128', BALANCE_CREDIT || 0)
const txFees = (await tx.paymentInfo(this.signingPair!.address)).partialFee
const requiredFunds = membershipFee.add(txFees).add(credit)
const requiredFunds = membershipFee
.add(txFees)
.add(credit)
.mul(createType('u128', N))
return balance.availableBalance.gt(requiredFunds)
}
}

0 comments on commit d894ea4

Please sign in to comment.