diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index 4e0c63a2..9cad47fd 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -2,12 +2,13 @@ import std/sequtils import std/strutils import std/sugar import pkg/chronicles -import pkg/ethers +import pkg/ethers except `%` import pkg/ethers/testing import pkg/upraises import pkg/questionable +import ../utils/json import ../market -import ./marketplace +import ./marketplace except `%` export market @@ -22,6 +23,7 @@ type EventSubscription = ethers.Subscription OnChainMarketSubscription = ref object of MarketSubscription eventSubscription: EventSubscription + OnChainMarketError = object of CatchableError func new*(_: type OnChainMarket, contract: Marketplace): OnChainMarket = without signer =? contract.signer: @@ -31,12 +33,20 @@ func new*(_: type OnChainMarket, contract: Marketplace): OnChainMarket = signer: signer, ) -proc approveFunds(market: OnChainMarket, amount: UInt256) {.async.} = +proc approveFunds(market: OnChainMarket, amount: UInt256, waitForConfirmations = 0) {.async.} = debug "Approving tokens", amount let tokenAddress = await market.contract.token() let token = Erc20Token.new(tokenAddress, market.signer) - discard await token.approve(market.contract.address(), amount).confirm(1) + var futResponse = token.approve(market.contract.address(), amount) + if waitForConfirmations > 0: + let receipt = await futResponse.confirm(waitForConfirmations) + if receipt.status != TransactionStatus.Success: + raise newException(OnChainMarketError, + "Transaction not successful, receipt: " & receipt.toJson) + + else: + discard await futResponse method getSigner*(market: OnChainMarket): Future[Address] {.async.} = return await market.signer.getAddress() @@ -122,10 +132,10 @@ method fillSlot*(market: OnChainMarket, slotIndex: UInt256, proof: seq[byte], collateral: UInt256) {.async.} = - trace "approving funds", amount = collateral, slotIndex, requestId - await market.approveFunds(collateral) - trace "filling slot", slotIndex, requestId - await market.contract.fillSlot(requestId, slotIndex, proof) + + await market.approveFunds(collateral, 1) + trace "calling contract fillSlot", slotIndex, requestId + discard await market.contract.fillSlot(requestId, slotIndex, proof).confirm(1) method freeSlot*(market: OnChainMarket, slotId: SlotId) {.async.} = await market.contract.freeSlot(slotId) diff --git a/codex/contracts/marketplace.nim b/codex/contracts/marketplace.nim index f09e8720..47b1a3d2 100644 --- a/codex/contracts/marketplace.nim +++ b/codex/contracts/marketplace.nim @@ -43,7 +43,7 @@ proc slashPercentage*(marketplace: Marketplace): UInt256 {.contract, view.} proc minCollateralThreshold*(marketplace: Marketplace): UInt256 {.contract, view.} proc requestStorage*(marketplace: Marketplace, request: StorageRequest) {.contract.} -proc fillSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256, proof: seq[byte]) {.contract.} +proc fillSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256, proof: seq[byte]): ?TransactionResponse {.contract.} proc withdrawFunds*(marketplace: Marketplace, requestId: RequestId) {.contract.} proc freeSlot*(marketplace: Marketplace, id: SlotId) {.contract.} proc getRequest*(marketplace: Marketplace, id: RequestId): StorageRequest {.contract, view.} diff --git a/codex/sales/states/filling.nim b/codex/sales/states/filling.nim index 59621b1c..fb9da4d6 100644 --- a/codex/sales/states/filling.nim +++ b/codex/sales/states/filling.nim @@ -37,5 +37,5 @@ method run(state: SaleFilling, machine: Machine): Future[?State] {.async.} = debug "Filling slot", requestId = $data.requestId, slotIndex market.cancelOnError: - discard await market.fillSlot(data.requestId, slotIndex, state.proof, collateral).confirm(1) + await market.fillSlot(data.requestId, slotIndex, state.proof, collateral) debug "Waiting for slot filled event...", requestId = $data.requestId, slotIndex