Skip to content

Commit

Permalink
[marketplace] support latest contracts changes (#327)
Browse files Browse the repository at this point in the history
* [marketplace] support removal of Storage contract

* [marketplace] change submod dep dagger-contracts to codex-contracts-eth
  • Loading branch information
Eric Mastro authored Jan 19, 2023
1 parent b1cf8e5 commit df729be
Show file tree
Hide file tree
Showing 19 changed files with 171 additions and 168 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
run: make -j${ncpu} test

- name: Start Ethereum node with Codex contracts
working-directory: vendor/dagger-contracts
working-directory: vendor/codex-contracts-eth
run: |
if [[ '${{ matrix.os }}' == 'windows' ]]; then
export PATH="${PATH}:/c/program files/nodejs"
Expand Down
7 changes: 3 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@
url = https://github.com/status-im/nim-websock.git
ignore = untracked
branch = master
[submodule "vendor/dagger-contracts"]
path = vendor/dagger-contracts
url = https://github.com/status-im/dagger-contracts
ignore = dirty
[submodule "vendor/nim-contract-abi"]
path = vendor/nim-contract-abi
url = https://github.com/status-im/nim-contract-abi
Expand Down Expand Up @@ -182,3 +178,6 @@
[submodule "vendor/nim-eth"]
path = vendor/nim-eth
url = https://github.com/status-im/nim-eth
[submodule "vendor/codex-contracts-eth"]
path = vendor/codex-contracts-eth
url = https://github.com/status-im/codex-contracts-eth
2 changes: 1 addition & 1 deletion BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ $ nvm install --lts

In that same terminal run
```text
$ cd repos/nim-codex/vendor/dagger-contracts && npm install && npm start
$ cd repos/nim-codex/vendor/codex-contracts-eth && npm install && npm start
```

Those commands install and launch a [Hardhat](https://hardhat.org/) environment with nim-codex's Ethereum contracts.
Expand Down
4 changes: 2 additions & 2 deletions codex/contracts.nim
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import contracts/requests
import contracts/storage
import contracts/marketplace
import contracts/deployment
import contracts/market
import contracts/proofs
import contracts/interactions

export requests
export storage
export marketplace
export deployment
export market
export proofs
Expand Down
8 changes: 4 additions & 4 deletions codex/contracts/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import ethers
let address = # fill in address where the contract was deployed
let provider = JsonRpcProvider.new("ws://localhost:8545")
let storage = Storage.new(address, provider)
let marketplace = Marketplace.new(address, provider)
```

Setup client and host so that they can sign transactions; here we use the first
Expand All @@ -39,7 +39,7 @@ Hosts need to put up collateral before participating in storage contracts.

A host can learn about the amount of collateral that is required:
```nim
let collateralAmount = await storage.collateralAmount()
let collateral = await marketplace.collateral()
```

The host then needs to prepare a payment to the smart contract by calling the
Expand All @@ -50,7 +50,7 @@ After preparing the payment, the host can deposit collateral:
```nim
await storage
.connect(host)
.deposit(collateralAmount)
.deposit(collateral)
```

When a host is not participating in storage offers or contracts, it can withdraw
Expand Down Expand Up @@ -176,6 +176,6 @@ await storage
.markProofAsMissing(id, period)
```

[1]: https://github.com/status-im/dagger-contracts/
[1]: https://github.com/status-im/codex-contracts-eth/
[2]: https://ethereum.org/en/developers/docs/standards/tokens/erc-20/
[3]: https://github.com/status-im/codex-research/blob/main/design/storage-proof-timing.md
2 changes: 1 addition & 1 deletion codex/contracts/deployment.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import pkg/questionable
type Deployment* = object
json: JsonNode

const defaultFile = "vendor" / "dagger-contracts" / "deployment-localhost.json"
const defaultFile = "vendor" / "codex-contracts-eth" / "deployment-localhost.json"

## Reads deployment information from a json file. It expects a file that has
## been exported with Hardhat deploy.
Expand Down
8 changes: 4 additions & 4 deletions codex/contracts/interactions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ../purchasing
import ../sales
import ../proving
import ./deployment
import ./storage
import ./marketplace
import ./market
import ./proofs
import ./clock
Expand All @@ -25,11 +25,11 @@ proc new*(_: type ContractInteractions,
signer: Signer,
deployment: Deployment): ?ContractInteractions =

without address =? deployment.address(Storage):
error "Unable to determine address of the Storage smart contract"
without address =? deployment.address(Marketplace):
error "Unable to determine address of the Marketplace smart contract"
return none ContractInteractions

let contract = Storage.new(address, signer)
let contract = Marketplace.new(address, signer)
let market = OnChainMarket.new(contract)
let proofs = OnChainProofs.new(contract)
let clock = OnChainClock.new(signer.provider)
Expand Down
8 changes: 4 additions & 4 deletions codex/contracts/market.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import pkg/ethers/testing
import pkg/upraises
import pkg/questionable
import ../market
import ./storage
import ./marketplace

export market

type
OnChainMarket* = ref object of Market
contract: Storage
contract: Marketplace
signer: Signer
MarketSubscription = market.Subscription
EventSubscription = ethers.Subscription
OnChainMarketSubscription = ref object of MarketSubscription
eventSubscription: EventSubscription

func new*(_: type OnChainMarket, contract: Storage): OnChainMarket =
func new*(_: type OnChainMarket, contract: Marketplace): OnChainMarket =
without signer =? contract.signer:
raiseAssert("Storage contract should have a signer")
raiseAssert("Marketplace contract should have a signer")
OnChainMarket(
contract: contract,
signer: signer,
Expand Down
62 changes: 62 additions & 0 deletions codex/contracts/marketplace.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import pkg/ethers
import pkg/json_rpc/rpcclient
import pkg/stint
import pkg/chronos
import ../clock
import ./requests

export stint
export ethers

type
Marketplace* = ref object of Contract
StorageRequested* = object of Event
requestId*: RequestId
ask*: StorageAsk
SlotFilled* = object of Event
requestId* {.indexed.}: RequestId
slotIndex* {.indexed.}: UInt256
slotId*: SlotId
RequestFulfilled* = object of Event
requestId* {.indexed.}: RequestId
RequestCancelled* = object of Event
requestId* {.indexed.}: RequestId
RequestFailed* = object of Event
requestId* {.indexed.}: RequestId
ProofSubmitted* = object of Event
id*: SlotId
proof*: seq[byte]


proc collateral*(marketplace: Marketplace): UInt256 {.contract, view.}
proc slashMisses*(marketplace: Marketplace): UInt256 {.contract, view.}
proc slashPercentage*(marketplace: Marketplace): UInt256 {.contract, view.}
proc minCollateralThreshold*(marketplace: Marketplace): UInt256 {.contract, view.}

proc deposit*(marketplace: Marketplace, amount: UInt256) {.contract.}
proc withdraw*(marketplace: Marketplace) {.contract.}
proc balanceOf*(marketplace: Marketplace, account: Address): UInt256 {.contract, view.}

proc requestStorage*(marketplace: Marketplace, request: StorageRequest) {.contract.}
proc fillSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256, proof: seq[byte]) {.contract.}
proc withdrawFunds*(marketplace: Marketplace, requestId: RequestId) {.contract.}
proc freeSlot*(marketplace: Marketplace, id: SlotId) {.contract.}
proc getRequest*(marketplace: Marketplace, id: RequestId): StorageRequest {.contract, view.}
proc getHost*(marketplace: Marketplace, id: SlotId): Address {.contract, view.}

proc myRequests*(marketplace: Marketplace): seq[RequestId] {.contract, view.}
proc state*(marketplace: Marketplace, requestId: RequestId): RequestState {.contract, view.}
proc requestEnd*(marketplace: Marketplace, requestId: RequestId): SecondsSince1970 {.contract, view.}

proc proofPeriod*(marketplace: Marketplace): UInt256 {.contract, view.}
proc proofTimeout*(marketplace: Marketplace): UInt256 {.contract, view.}

proc proofEnd*(marketplace: Marketplace, id: SlotId): UInt256 {.contract, view.}
proc missingProofs*(marketplace: Marketplace, id: SlotId): UInt256 {.contract, view.}
proc isProofRequired*(marketplace: Marketplace, id: SlotId): bool {.contract, view.}
proc willProofBeRequired*(marketplace: Marketplace, id: SlotId): bool {.contract, view.}
proc getChallenge*(marketplace: Marketplace, id: SlotId): array[32, byte] {.contract, view.}
proc getPointer*(marketplace: Marketplace, id: SlotId): uint8 {.contract, view.}

proc submitProof*(marketplace: Marketplace, id: SlotId, proof: seq[byte]) {.contract.}
proc markProofAsMissing*(marketplace: Marketplace, id: SlotId, period: UInt256) {.contract.}
20 changes: 10 additions & 10 deletions codex/contracts/proofs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import std/strutils
import pkg/ethers
import pkg/ethers/testing
import ../storageproofs/timing/proofs
import ./storage
import ./marketplace

export proofs

type
OnChainProofs* = ref object of Proofs
storage: Storage
marketplace: Marketplace
pollInterval*: Duration
ProofsSubscription = proofs.Subscription
EventSubscription = ethers.Subscription
Expand All @@ -17,17 +17,17 @@ type

const DefaultPollInterval = 3.seconds

proc new*(_: type OnChainProofs, storage: Storage): OnChainProofs =
OnChainProofs(storage: storage, pollInterval: DefaultPollInterval)
proc new*(_: type OnChainProofs, marketplace: Marketplace): OnChainProofs =
OnChainProofs(marketplace: marketplace, pollInterval: DefaultPollInterval)

method periodicity*(proofs: OnChainProofs): Future[Periodicity] {.async.} =
let period = await proofs.storage.proofPeriod()
let period = await proofs.marketplace.proofPeriod()
return Periodicity(seconds: period)

method isProofRequired*(proofs: OnChainProofs,
id: SlotId): Future[bool] {.async.} =
try:
return await proofs.storage.isProofRequired(id)
return await proofs.marketplace.isProofRequired(id)
except ProviderError as e:
if e.revertReason.contains("Slot empty"):
return false
Expand All @@ -36,7 +36,7 @@ method isProofRequired*(proofs: OnChainProofs,
method willProofBeRequired*(proofs: OnChainProofs,
id: SlotId): Future[bool] {.async.} =
try:
return await proofs.storage.willProofBeRequired(id)
return await proofs.marketplace.willProofBeRequired(id)
except ProviderError as e:
if e.revertReason.contains("Slot empty"):
return false
Expand All @@ -45,7 +45,7 @@ method willProofBeRequired*(proofs: OnChainProofs,
method getProofEnd*(proofs: OnChainProofs,
id: SlotId): Future[UInt256] {.async.} =
try:
return await proofs.storage.proofEnd(id)
return await proofs.marketplace.proofEnd(id)
except ProviderError as e:
if e.revertReason.contains("Slot empty"):
return 0.u256
Expand All @@ -54,14 +54,14 @@ method getProofEnd*(proofs: OnChainProofs,
method submitProof*(proofs: OnChainProofs,
id: SlotId,
proof: seq[byte]) {.async.} =
await proofs.storage.submitProof(id, proof)
await proofs.marketplace.submitProof(id, proof)

method subscribeProofSubmission*(proofs: OnChainProofs,
callback: OnProofSubmitted):
Future[ProofsSubscription] {.async.} =
proc onEvent(event: ProofSubmitted) {.upraises: [].} =
callback(event.id, event.proof)
let subscription = await proofs.storage.subscribe(ProofSubmitted, onEvent)
let subscription = await proofs.marketplace.subscribe(ProofSubmitted, onEvent)
return OnChainProofsSubscription(eventSubscription: subscription)

method unsubscribe*(subscription: OnChainProofsSubscription) {.async, upraises:[].} =
Expand Down
62 changes: 0 additions & 62 deletions codex/contracts/storage.nim

This file was deleted.

22 changes: 11 additions & 11 deletions tests/contracts/testCollateral.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ import ../ethertest

ethersuite "Collateral":

let collateralAmount = 100.u256
let collateral = 100.u256

var storage: Storage
var marketplace: Marketplace
var token: TestToken

setup:
let deployment = deployment()
storage = Storage.new(!deployment.address(Storage), provider.getSigner())
marketplace = Marketplace.new(!deployment.address(Marketplace), provider.getSigner())
token = TestToken.new(!deployment.address(TestToken), provider.getSigner())
await token.mint(accounts[0], 1000.u256)

test "increases collateral":
await token.approve(storage.address, collateralAmount)
await storage.deposit(collateralAmount)
let collateral = await storage.balanceOf(accounts[0])
check collateral == collateralAmount
await token.approve(marketplace.address, collateral)
await marketplace.deposit(collateral)
let balance = await marketplace.balanceOf(accounts[0])
check balance == collateral

test "withdraws collateral":
await token.approve(storage.address, collateralAmount)
await storage.deposit(collateralAmount)
await token.approve(marketplace.address, collateral)
await marketplace.deposit(collateral)
let balanceBefore = await token.balanceOf(accounts[0])
await storage.withdraw()
await marketplace.withdraw()
let balanceAfter = await token.balanceOf(accounts[0])
check (balanceAfter - balanceBefore) == collateralAmount
check (balanceAfter - balanceBefore) == collateral
Loading

0 comments on commit df729be

Please sign in to comment.