From d10072bf67ddab51a9084c8a7f875239153d7922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Thu, 12 Dec 2024 13:57:34 +0100 Subject: [PATCH] refactor: marketplace configuration is cached (#1029) --- codex/contracts/market.nim | 18 ++++++++++++++---- tests/contracts/testMarket.nim | 5 +++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index e1e36d9b..cc60e27c 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -20,6 +20,8 @@ type contract: Marketplace signer: Signer rewardRecipient: ?Address + configuration: ?MarketplaceConfig + MarketSubscription = market.Subscription EventSubscription = ethers.Subscription OnChainMarketSubscription = ref object of MarketSubscription @@ -48,6 +50,14 @@ template convertEthersError(body) = except EthersError as error: raiseMarketError(error.msgDetail) +proc config(market: OnChainMarket): Future[MarketplaceConfig] {.async.} = + without resolvedConfig =? market.configuration: + let fetchedConfig = await market.contract.configuration() + market.configuration = some fetchedConfig + return fetchedConfig + + return resolvedConfig + proc approveFunds(market: OnChainMarket, amount: UInt256) {.async.} = debug "Approving tokens", amount convertEthersError: @@ -56,7 +66,7 @@ proc approveFunds(market: OnChainMarket, amount: UInt256) {.async.} = discard await token.increaseAllowance(market.contract.address(), amount).confirm(1) method getZkeyHash*(market: OnChainMarket): Future[?string] {.async.} = - let config = await market.contract.configuration() + let config = await market.config() return some config.proofs.zkeyHash method getSigner*(market: OnChainMarket): Future[Address] {.async.} = @@ -65,18 +75,18 @@ method getSigner*(market: OnChainMarket): Future[Address] {.async.} = method periodicity*(market: OnChainMarket): Future[Periodicity] {.async.} = convertEthersError: - let config = await market.contract.configuration() + let config = await market.config() let period = config.proofs.period return Periodicity(seconds: period) method proofTimeout*(market: OnChainMarket): Future[UInt256] {.async.} = convertEthersError: - let config = await market.contract.configuration() + let config = await market.config() return config.proofs.timeout method proofDowntime*(market: OnChainMarket): Future[uint8] {.async.} = convertEthersError: - let config = await market.contract.configuration() + let config = await market.config() return config.proofs.downtime method getPointer*(market: OnChainMarket, slotId: SlotId): Future[uint8] {.async.} = diff --git a/tests/contracts/testMarket.nim b/tests/contracts/testMarket.nim index b967bc8c..8e4d013e 100644 --- a/tests/contracts/testMarket.nim +++ b/tests/contracts/testMarket.nim @@ -66,6 +66,11 @@ ethersuite "On-Chain Market": ): await advanceToNextPeriod() + test "caches marketplace configuration": + check isNone market.configuration + discard await market.periodicity() + check isSome market.configuration + test "fails to instantiate when contract does not have a signer": let storageWithoutSigner = marketplace.connect(ethProvider) expect AssertionDefect: