Wait for sender balance update when paying for subscription

This commit is contained in:
silverpill 2022-07-18 20:39:26 +00:00
parent 00562b0dcf
commit db9afb4855
2 changed files with 15 additions and 8 deletions

View file

@ -14,7 +14,7 @@ const SECONDS_IN_MONTH = SECONDS_IN_DAY * 30
export interface SubscriptionToken { export interface SubscriptionToken {
address: string; address: string;
symbol: string; symbol: string;
decimals: string; decimals: number;
} }
export async function getSubscriptionToken( export async function getSubscriptionToken(

View file

@ -252,7 +252,8 @@ async function onMakeSubscriptionPayment() {
!instance?.blockchain_contract_address || !instance?.blockchain_contract_address ||
!recipientEthereumAddress || !recipientEthereumAddress ||
!walletAddress || !walletAddress ||
!subscription !subscription ||
!subscriptionState
) { ) {
return return
} }
@ -273,12 +274,18 @@ async function onMakeSubscriptionPayment() {
return return
} }
await transaction.wait() await transaction.wait()
subscriptionState = await getSubscriptionState( // Wait for sender balance update
// because JSON-RPC API can return outdated info on the first call
let newSubscriptionState
while (!newSubscriptionState || subscriptionState.senderBalance === newSubscriptionState.senderBalance) {
newSubscriptionState = await getSubscriptionState(
instance.blockchain_contract_address, instance.blockchain_contract_address,
signer, signer,
walletAddress, walletAddress,
recipientEthereumAddress, recipientEthereumAddress,
) )
}
subscriptionState = newSubscriptionState
tokenBalance = await getTokenBalance(signer, subscription.tokenAddress) tokenBalance = await getTokenBalance(signer, subscription.tokenAddress)
isLoading = false isLoading = false
} }