From 9ade3fdd37d305a297f52a24d3e6de83b9ceb3cd Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 23 Mar 2022 14:20:36 +0100 Subject: [PATCH] [contracts] StorageRequest/Offer is object instead of tuple --- dagger/contracts/offers.nim | 24 ++++++++++++++++++----- dagger/contracts/requests.nim | 36 ++++++++++++++++++++++++++--------- dagger/purchasing.nim | 2 +- tests/contracts/examples.nim | 4 ++-- 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/dagger/contracts/offers.nim b/dagger/contracts/offers.nim index 275f9d81..0bfdceda 100644 --- a/dagger/contracts/offers.nim +++ b/dagger/contracts/offers.nim @@ -4,11 +4,25 @@ import pkg/nimcrypto export contractabi type - StorageOffer* = tuple - host: Address - requestId: array[32, byte] - price: UInt256 - expiry: UInt256 + StorageOffer* = object + host*: Address + requestId*: array[32, byte] + price*: UInt256 + expiry*: UInt256 + +func toTuple(offer: StorageOffer): auto = + ( + offer.host, + offer.requestId, + offer.price, + offer.expiry + ) + +func solidityType*(_: type StorageOffer): string = + solidityType(typeof StorageOffer.default.toTuple) + +func encode*(encoder: var AbiEncoder, offer: StorageOffer) = + encoder.write(offer.toTuple) func id*(offer: StorageOffer): array[32, byte] = let encoding = AbiEncoder.encode(offer) diff --git a/dagger/contracts/requests.nim b/dagger/contracts/requests.nim index 468c2467..638fca3b 100644 --- a/dagger/contracts/requests.nim +++ b/dagger/contracts/requests.nim @@ -4,15 +4,33 @@ import pkg/nimcrypto export contractabi type - StorageRequest* = tuple - client: Address - duration: UInt256 - size: UInt256 - contentHash: array[32, byte] - proofProbability: UInt256 - maxPrice: UInt256 - expiry: UInt256 - nonce: array[32, byte] + StorageRequest* = object + client*: Address + duration*: UInt256 + size*: UInt256 + contentHash*: array[32, byte] + proofProbability*: UInt256 + maxPrice*: UInt256 + expiry*: UInt256 + nonce*: array[32, byte] + +func toTuple(request: StorageRequest): auto = + ( + request.client, + request.duration, + request.size, + request.contentHash, + request.proofProbability, + request.maxPrice, + request.expiry, + request.nonce + ) + +func solidityType*(_: type StorageRequest): string = + solidityType(typeof StorageRequest.default.toTuple) + +func encode*(encoder: var AbiEncoder, request: StorageRequest) = + encoder.write(request.toTuple) func id*(request: StorageRequest): array[32, byte] = let encoding = AbiEncoder.encode(request) diff --git a/dagger/purchasing.nim b/dagger/purchasing.nim index 655a8699..1ca842c3 100644 --- a/dagger/purchasing.nim +++ b/dagger/purchasing.nim @@ -41,7 +41,7 @@ proc getNonce(): array[32, byte] = doAssert randomBytes(result) == 32 proc purchase*(purchasing: Purchasing, request: PurchaseRequest): Purchase = - let request: StorageRequest = ( + let request = StorageRequest( client: Address.default, # TODO duration: request.duration, size: request.size, diff --git a/tests/contracts/examples.nim b/tests/contracts/examples.nim index 50fe2372..752b353a 100644 --- a/tests/contracts/examples.nim +++ b/tests/contracts/examples.nim @@ -11,7 +11,7 @@ proc example*(_: type Address): Address = Address(randomBytes(20)) proc example*(_: type StorageRequest): StorageRequest = - ( + StorageRequest( client: Address.example, duration: (10 * 60 * 60).u256, # 10 hours size: (1 * 1024 * 1024 * 1024).u256, # 1 Gigabyte @@ -23,7 +23,7 @@ proc example*(_: type StorageRequest): StorageRequest = ) proc example*(_: type StorageOffer): StorageOffer = - ( + StorageOffer( host: Address.example, requestId: StorageRequest.example.id, price: 42.u256,