diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index 9d0799f9..3708478f 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -23,6 +23,7 @@ type rewardRecipient: ?Address configuration: ?MarketplaceConfig requestCache: LruCache[string, StorageRequest] + allowanceLock: AsyncLock MarketSubscription = market.Subscription EventSubscription = ethers.Subscription @@ -76,6 +77,18 @@ proc config( return resolvedConfig +template withAllowanceLock*(market: OnChainMarket, body: untyped) = + if market.allowanceLock.isNil: + market.allowanceLock = newAsyncLock() + await market.allowanceLock.acquire() + try: + body + finally: + try: + market.allowanceLock.release() + except AsyncLockError as error: + raise newException(Defect, error.msg, error) + proc approveFunds( market: OnChainMarket, amount: UInt256 ) {.async: (raises: [CancelledError, MarketError]).} = @@ -83,7 +96,11 @@ proc approveFunds( convertEthersError("Failed to approve funds"): let tokenAddress = await market.contract.token() let token = Erc20Token.new(tokenAddress, market.signer) - discard await token.increaseAllowance(market.contract.address(), amount).confirm(1) + let owner = await market.signer.getAddress() + let spender = market.contract.address + market.withAllowanceLock: + let allowance = await token.allowance(owner, spender) + discard await token.approve(spender, allowance + amount).confirm(1) method loadConfig*( market: OnChainMarket diff --git a/vendor/codex-contracts-eth b/vendor/codex-contracts-eth index 0bf13851..470a4df4 160000 --- a/vendor/codex-contracts-eth +++ b/vendor/codex-contracts-eth @@ -1 +1 @@ -Subproject commit 0bf138512b7c1c3b8d77c48376e47f702e47106c +Subproject commit 470a4df4152b53a8065c2984d55efcd7350b549a