nim-codex/dagger/contracts/storage.nim
Eric Mastro 2e5c28781c feat: integrate dagger contracts
Integrate dagger contracts from `nim-dagger-contracts` repo.

Add `dagger-contracts`, `nim-web3`, and all of `nim-web3`’s transitive deps as submodule deps to `nim-dagger`. Note: `nim-web3` and its transitive deps may no longer be needed when we switch to `nim-ethers`.

Add a `testContracts` nimble task to test all of the contracts functionality. Namely, this spins up an ethereum simulator, deploys the contracts (in `dagger-contracts`), runs the contract tests, and finally, regardless of success/error, kills the ethereum sim processes. The nimble task can be run with `./env.sh nimble testContracts`.

We also tested `nim-dagger-contracts` as a submodule dep of `nim-dagger`, and while the tests run as expected, the preference is to merge `nim-dagger-contracts` inside of `nim-dagger` for ease of parallel development. There’s also a high probability that `nim-dagger-contracts` is not being used as a dep by other projects. Are there any strong objections to this?

Co-authored-by: Michael Bradley <michaelsbradleyjr@gmail.com>
2022-02-04 15:34:56 +11:00

71 lines
2.5 KiB
Nim

import pkg/ethers
import pkg/json_rpc/rpcclient
import pkg/stint
import pkg/chronos
import ./marketplace
export stint
export contract
type
Storage* = ref object of Contract
Id = array[32, byte]
proc stakeAmount*(storage: Storage): UInt256 {.contract, view.}
proc increaseStake*(storage: Storage, amount: UInt256) {.contract.}
proc withdrawStake*(storage: Storage) {.contract.}
proc stake*(storage: Storage, account: Address): UInt256 {.contract, view.}
proc duration*(storage: Storage, id: Id): UInt256 {.contract, view.}
proc size*(storage: Storage, id: Id): UInt256 {.contract, view.}
proc contentHash*(storage: Storage, id: Id): array[32, byte] {.contract, view.}
proc proofPeriod*(storage: Storage, id: Id): UInt256 {.contract, view.}
proc proofTimeout*(storage: Storage, id: Id): UInt256 {.contract, view.}
proc price*(storage: Storage, id: Id): UInt256 {.contract, view.}
proc host*(storage: Storage, id: Id): Address {.contract, view.}
proc startContract*(storage: Storage, id: Id) {.contract.}
proc proofEnd*(storage: Storage, id: Id): UInt256 {.contract, view.}
proc isProofRequired*(storage: Storage,
id: Id,
blocknumber: UInt256): bool {.contract, view.}
proc submitProof*(storage: Storage,
id: Id,
blocknumber: UInt256,
proof: bool) {.contract.}
proc markProofAsMissing*(storage: Storage,
id: Id,
blocknumber: UInt256) {.contract.}
proc finishContract*(storage: Storage, id: Id) {.contract.}
proc newContract(storage: Storage,
duration: UInt256,
size: UInt256,
contentHash: array[32, byte],
proofPeriod: UInt256,
proofTimeout: UInt256,
nonce: array[32, byte],
price: UInt256,
host: Address,
bidExpiry: UInt256,
requestSignature: seq[byte],
bidSignature: seq[byte]) {.contract.}
proc newContract*(storage: Storage,
request: StorageRequest,
bid: StorageBid,
host: Address,
requestSignature: seq[byte],
bidSignature: seq[byte]) {.async.} =
await storage.newContract(
request.duration,
request.size,
request.contentHash,
request.proofPeriod,
request.proofTimeout,
request.nonce,
bid.price,
host,
bid.bidExpiry,
requestSignature,
bidSignature
)