refactor: marketplace configuration is cached (#1029)

This commit is contained in:
Adam Uhlíř 2024-12-12 13:57:34 +01:00 committed by GitHub
parent da234d503b
commit d10072bf67
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 4 deletions

View File

@ -20,6 +20,8 @@ type
contract: Marketplace contract: Marketplace
signer: Signer signer: Signer
rewardRecipient: ?Address rewardRecipient: ?Address
configuration: ?MarketplaceConfig
MarketSubscription = market.Subscription MarketSubscription = market.Subscription
EventSubscription = ethers.Subscription EventSubscription = ethers.Subscription
OnChainMarketSubscription = ref object of MarketSubscription OnChainMarketSubscription = ref object of MarketSubscription
@ -48,6 +50,14 @@ template convertEthersError(body) =
except EthersError as error: except EthersError as error:
raiseMarketError(error.msgDetail) 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.} = proc approveFunds(market: OnChainMarket, amount: UInt256) {.async.} =
debug "Approving tokens", amount debug "Approving tokens", amount
convertEthersError: convertEthersError:
@ -56,7 +66,7 @@ proc approveFunds(market: OnChainMarket, amount: UInt256) {.async.} =
discard await token.increaseAllowance(market.contract.address(), amount).confirm(1) discard await token.increaseAllowance(market.contract.address(), amount).confirm(1)
method getZkeyHash*(market: OnChainMarket): Future[?string] {.async.} = method getZkeyHash*(market: OnChainMarket): Future[?string] {.async.} =
let config = await market.contract.configuration() let config = await market.config()
return some config.proofs.zkeyHash return some config.proofs.zkeyHash
method getSigner*(market: OnChainMarket): Future[Address] {.async.} = 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.} = method periodicity*(market: OnChainMarket): Future[Periodicity] {.async.} =
convertEthersError: convertEthersError:
let config = await market.contract.configuration() let config = await market.config()
let period = config.proofs.period let period = config.proofs.period
return Periodicity(seconds: period) return Periodicity(seconds: period)
method proofTimeout*(market: OnChainMarket): Future[UInt256] {.async.} = method proofTimeout*(market: OnChainMarket): Future[UInt256] {.async.} =
convertEthersError: convertEthersError:
let config = await market.contract.configuration() let config = await market.config()
return config.proofs.timeout return config.proofs.timeout
method proofDowntime*(market: OnChainMarket): Future[uint8] {.async.} = method proofDowntime*(market: OnChainMarket): Future[uint8] {.async.} =
convertEthersError: convertEthersError:
let config = await market.contract.configuration() let config = await market.config()
return config.proofs.downtime return config.proofs.downtime
method getPointer*(market: OnChainMarket, slotId: SlotId): Future[uint8] {.async.} = method getPointer*(market: OnChainMarket, slotId: SlotId): Future[uint8] {.async.} =

View File

@ -66,6 +66,11 @@ ethersuite "On-Chain Market":
): ):
await advanceToNextPeriod() 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": test "fails to instantiate when contract does not have a signer":
let storageWithoutSigner = marketplace.connect(ethProvider) let storageWithoutSigner = marketplace.connect(ethProvider)
expect AssertionDefect: expect AssertionDefect: