2022-08-18 05:55:53 +00:00
|
|
|
import std/hashes
|
2022-03-17 13:15:04 +00:00
|
|
|
import pkg/contractabi
|
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-01-25 00:22:58 +00:00
|
|
|
import pkg/nimcrypto
|
2022-03-28 15:24:28 +00:00
|
|
|
import pkg/ethers/fields
|
|
|
|
import pkg/questionable/results
|
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-01-25 00:22:58 +00:00
|
|
|
|
2022-03-23 08:11:15 +00:00
|
|
|
export contractabi
|
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-01-25 00:22:58 +00:00
|
|
|
|
|
|
|
type
|
2022-03-23 13:20:36 +00:00
|
|
|
StorageRequest* = object
|
|
|
|
client*: Address
|
2022-04-11 18:03:55 +00:00
|
|
|
ask*: StorageAsk
|
|
|
|
content*: StorageContent
|
|
|
|
expiry*: UInt256
|
2022-08-17 02:29:44 +00:00
|
|
|
nonce*: Nonce
|
2022-04-11 18:03:55 +00:00
|
|
|
StorageAsk* = object
|
2022-08-02 12:21:12 +00:00
|
|
|
slots*: uint64
|
|
|
|
slotSize*: UInt256
|
2022-04-11 18:03:55 +00:00
|
|
|
duration*: UInt256
|
2022-03-23 13:20:36 +00:00
|
|
|
proofProbability*: UInt256
|
2022-07-20 12:11:00 +00:00
|
|
|
reward*: UInt256
|
2022-08-26 04:08:02 +00:00
|
|
|
maxSlotLoss*: uint64
|
2022-04-11 18:03:55 +00:00
|
|
|
StorageContent* = object
|
|
|
|
cid*: string
|
|
|
|
erasure*: StorageErasure
|
|
|
|
por*: StoragePoR
|
|
|
|
StorageErasure* = object
|
|
|
|
totalChunks*: uint64
|
|
|
|
StoragePoR* = object
|
|
|
|
u*: seq[byte]
|
|
|
|
publicKey*: seq[byte]
|
|
|
|
name*: seq[byte]
|
2022-08-18 05:55:53 +00:00
|
|
|
SlotId* = distinct array[32, byte]
|
|
|
|
RequestId* = distinct array[32, byte]
|
|
|
|
Nonce* = distinct array[32, byte]
|
|
|
|
|
|
|
|
proc `==`*(x, y: Nonce): bool {.borrow.}
|
|
|
|
proc `==`*(x, y: RequestId): bool {.borrow.}
|
|
|
|
proc `==`*(x, y: SlotId): bool {.borrow.}
|
|
|
|
proc hash*(x: SlotId): Hash {.borrow.}
|
|
|
|
|
|
|
|
func toArray*(id: RequestId | SlotId | Nonce): array[32, byte] =
|
|
|
|
array[32, byte](id)
|
2022-03-23 13:20:36 +00:00
|
|
|
|
2022-03-28 15:24:28 +00:00
|
|
|
func fromTuple(_: type StorageRequest, tupl: tuple): StorageRequest =
|
|
|
|
StorageRequest(
|
|
|
|
client: tupl[0],
|
2022-04-11 18:03:55 +00:00
|
|
|
ask: tupl[1],
|
|
|
|
content: tupl[2],
|
|
|
|
expiry: tupl[3],
|
|
|
|
nonce: tupl[4]
|
|
|
|
)
|
|
|
|
|
|
|
|
func fromTuple(_: type StorageAsk, tupl: tuple): StorageAsk =
|
|
|
|
StorageAsk(
|
2022-08-02 12:21:12 +00:00
|
|
|
slots: tupl[0],
|
|
|
|
slotSize: tupl[1],
|
|
|
|
duration: tupl[2],
|
|
|
|
proofProbability: tupl[3],
|
2022-08-26 04:08:02 +00:00
|
|
|
reward: tupl[4],
|
|
|
|
maxSlotLoss: tupl[5]
|
2022-04-11 18:03:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func fromTuple(_: type StorageContent, tupl: tuple): StorageContent =
|
|
|
|
StorageContent(
|
|
|
|
cid: tupl[0],
|
|
|
|
erasure: tupl[1],
|
|
|
|
por: tupl[2]
|
|
|
|
)
|
|
|
|
|
|
|
|
func fromTuple(_: type StorageErasure, tupl: tuple): StorageErasure =
|
|
|
|
StorageErasure(
|
2022-07-20 12:18:12 +00:00
|
|
|
totalChunks: tupl[0]
|
2022-04-11 18:03:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func fromTuple(_: type StoragePoR, tupl: tuple): StoragePoR =
|
|
|
|
StoragePoR(
|
|
|
|
u: tupl[0],
|
|
|
|
publicKey: tupl[1],
|
|
|
|
name: tupl[2]
|
2022-03-23 13:20:36 +00:00
|
|
|
)
|
|
|
|
|
2022-04-11 18:03:55 +00:00
|
|
|
func solidityType*(_: type StoragePoR): string =
|
|
|
|
solidityType(StoragePoR.fieldTypes)
|
|
|
|
|
|
|
|
func solidityType*(_: type StorageErasure): string =
|
|
|
|
solidityType(StorageErasure.fieldTypes)
|
|
|
|
|
|
|
|
func solidityType*(_: type StorageContent): string =
|
|
|
|
solidityType(StorageContent.fieldTypes)
|
|
|
|
|
|
|
|
func solidityType*(_: type StorageAsk): string =
|
|
|
|
solidityType(StorageAsk.fieldTypes)
|
|
|
|
|
2022-03-23 13:20:36 +00:00
|
|
|
func solidityType*(_: type StorageRequest): string =
|
2022-03-28 15:24:28 +00:00
|
|
|
solidityType(StorageRequest.fieldTypes)
|
2022-03-23 13:20:36 +00:00
|
|
|
|
2022-08-18 05:55:53 +00:00
|
|
|
func solidityType*[T: RequestId | SlotId | Nonce](_: type T): string =
|
|
|
|
solidityType(array[32, byte])
|
|
|
|
|
2022-04-11 18:03:55 +00:00
|
|
|
func encode*(encoder: var AbiEncoder, por: StoragePoR) =
|
|
|
|
encoder.write(por.fieldValues)
|
|
|
|
|
|
|
|
func encode*(encoder: var AbiEncoder, erasure: StorageErasure) =
|
|
|
|
encoder.write(erasure.fieldValues)
|
|
|
|
|
|
|
|
func encode*(encoder: var AbiEncoder, content: StorageContent) =
|
|
|
|
encoder.write(content.fieldValues)
|
|
|
|
|
|
|
|
func encode*(encoder: var AbiEncoder, ask: StorageAsk) =
|
|
|
|
encoder.write(ask.fieldValues)
|
|
|
|
|
2022-08-18 05:55:53 +00:00
|
|
|
func encode*(encoder: var AbiEncoder, id: RequestId | SlotId | Nonce) =
|
|
|
|
encoder.write(id.toArray)
|
|
|
|
|
2022-03-23 13:20:36 +00:00
|
|
|
func encode*(encoder: var AbiEncoder, request: StorageRequest) =
|
2022-03-28 15:24:28 +00:00
|
|
|
encoder.write(request.fieldValues)
|
|
|
|
|
2022-08-18 05:55:53 +00:00
|
|
|
func decode*[T: RequestId | SlotId | Nonce](decoder: var AbiDecoder,
|
|
|
|
_: type T): ?!T =
|
|
|
|
let nonce = ?decoder.read(type array[32, byte])
|
|
|
|
success T(nonce)
|
|
|
|
|
2022-04-11 18:03:55 +00:00
|
|
|
func decode*(decoder: var AbiDecoder, T: type StoragePoR): ?!T =
|
|
|
|
let tupl = ?decoder.read(StoragePoR.fieldTypes)
|
|
|
|
success StoragePoR.fromTuple(tupl)
|
|
|
|
|
|
|
|
func decode*(decoder: var AbiDecoder, T: type StorageErasure): ?!T =
|
|
|
|
let tupl = ?decoder.read(StorageErasure.fieldTypes)
|
|
|
|
success StorageErasure.fromTuple(tupl)
|
|
|
|
|
|
|
|
func decode*(decoder: var AbiDecoder, T: type StorageContent): ?!T =
|
|
|
|
let tupl = ?decoder.read(StorageContent.fieldTypes)
|
|
|
|
success StorageContent.fromTuple(tupl)
|
|
|
|
|
|
|
|
func decode*(decoder: var AbiDecoder, T: type StorageAsk): ?!T =
|
|
|
|
let tupl = ?decoder.read(StorageAsk.fieldTypes)
|
|
|
|
success StorageAsk.fromTuple(tupl)
|
|
|
|
|
2022-03-28 15:24:28 +00:00
|
|
|
func decode*(decoder: var AbiDecoder, T: type StorageRequest): ?!T =
|
|
|
|
let tupl = ?decoder.read(StorageRequest.fieldTypes)
|
|
|
|
success StorageRequest.fromTuple(tupl)
|
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-01-25 00:22:58 +00:00
|
|
|
|
2022-08-17 02:29:44 +00:00
|
|
|
func id*(request: StorageRequest): RequestId =
|
2022-04-11 18:03:55 +00:00
|
|
|
let encoding = AbiEncoder.encode((request, ))
|
2022-08-18 05:55:53 +00:00
|
|
|
RequestId(keccak256.digest(encoding).data)
|
2022-07-20 13:30:13 +00:00
|
|
|
|
2022-08-17 02:29:44 +00:00
|
|
|
func slotId*(requestId: RequestId, slot: UInt256): SlotId =
|
2022-07-27 14:54:34 +00:00
|
|
|
let encoding = AbiEncoder.encode((requestId, slot))
|
2022-08-18 05:55:53 +00:00
|
|
|
SlotId(keccak256.digest(encoding).data)
|
2022-07-20 13:30:13 +00:00
|
|
|
|
2022-08-17 02:29:44 +00:00
|
|
|
func slotId*(request: StorageRequest, slot: UInt256): SlotId =
|
2022-07-27 14:54:34 +00:00
|
|
|
slotId(request.id, slot)
|
|
|
|
|
2022-08-01 12:25:32 +00:00
|
|
|
func pricePerSlot*(ask: StorageAsk): UInt256 =
|
|
|
|
ask.duration * ask.reward
|
|
|
|
|
|
|
|
func price*(ask: StorageAsk): UInt256 =
|
|
|
|
ask.slots.u256 * ask.pricePerSlot
|
|
|
|
|
2022-07-20 13:30:13 +00:00
|
|
|
func price*(request: StorageRequest): UInt256 =
|
2022-08-01 12:25:32 +00:00
|
|
|
request.ask.price
|
2022-08-02 12:21:12 +00:00
|
|
|
|
|
|
|
func size*(ask: StorageAsk): UInt256 =
|
|
|
|
ask.slots.u256 * ask.slotSize
|