| 
23 | 23 |     rewardRecipient: ?Address  | 
24 | 24 |     configuration: ?MarketplaceConfig  | 
25 | 25 |     requestCache: LruCache[string, StorageRequest]  | 
 | 26 | +    allowanceLock: AsyncLock  | 
26 | 27 | 
 
  | 
27 | 28 |   MarketSubscription = market.Subscription  | 
28 | 29 |   EventSubscription = ethers.Subscription  | 
@@ -76,18 +77,30 @@ proc config(  | 
76 | 77 | 
 
  | 
77 | 78 |   return resolvedConfig  | 
78 | 79 | 
 
  | 
 | 80 | +template withAllowanceLock*(market: OnChainMarket, body: untyped) =  | 
 | 81 | +  if market.allowanceLock.isNil:  | 
 | 82 | +    market.allowanceLock = newAsyncLock()  | 
 | 83 | +  await market.allowanceLock.acquire()  | 
 | 84 | +  try:  | 
 | 85 | +    body  | 
 | 86 | +  finally:  | 
 | 87 | +    try:  | 
 | 88 | +      market.allowanceLock.release()  | 
 | 89 | +    except AsyncLockError as error:  | 
 | 90 | +      raise newException(Defect, error.msg, error)  | 
 | 91 | + | 
79 | 92 | proc approveFunds(  | 
80 | 93 |     market: OnChainMarket, amount: UInt256  | 
81 | 94 | ) {.async: (raises: [CancelledError, MarketError]).} =  | 
82 | 95 |   debug "Approving tokens", amount  | 
83 | 96 |   convertEthersError("Failed to approve funds"):  | 
84 | 97 |     let tokenAddress = await market.contract.token()  | 
85 | 98 |     let token = Erc20Token.new(tokenAddress, market.signer)  | 
86 |  | -    let currentAllowance =  | 
87 |  | -      await token.allowance(await market.getSigner(), market.contract.address())  | 
88 |  | -    discard await token  | 
89 |  | -    .approve(market.contract.address(), currentAllowance + amount)  | 
90 |  | -    .confirm(1)  | 
 | 99 | +    let owner = await market.signer.getAddress()  | 
 | 100 | +    let spender = market.contract.address  | 
 | 101 | +    market.withAllowanceLock:  | 
 | 102 | +      let allowance = await token.allowance(owner, spender)  | 
 | 103 | +      discard await token.approve(spender, allowance + amount).confirm(1)  | 
91 | 104 | 
 
  | 
92 | 105 | method loadConfig*(  | 
93 | 106 |     market: OnChainMarket  | 
 | 
0 commit comments