2022-04-13 11:18:38 +00:00
|
|
|
import pkg/ethers
|
2022-04-25 13:12:37 +00:00
|
|
|
import pkg/chronicles
|
2022-04-13 11:18:38 +00:00
|
|
|
import ../purchasing
|
|
|
|
import ../sales
|
|
|
|
import ../proving
|
|
|
|
import ./deployment
|
|
|
|
import ./storage
|
|
|
|
import ./market
|
|
|
|
import ./proofs
|
2022-05-17 14:42:03 +00:00
|
|
|
import ./clock
|
2022-04-13 11:18:38 +00:00
|
|
|
|
|
|
|
export purchasing
|
|
|
|
export sales
|
|
|
|
export proving
|
2022-04-25 13:12:37 +00:00
|
|
|
export chronicles
|
2022-04-13 11:18:38 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
ContractInteractions* = ref object
|
|
|
|
purchasing*: Purchasing
|
|
|
|
sales*: Sales
|
|
|
|
proving*: Proving
|
2022-05-17 14:42:03 +00:00
|
|
|
clock: OnChainClock
|
2022-04-13 11:18:38 +00:00
|
|
|
|
|
|
|
proc new*(_: type ContractInteractions,
|
|
|
|
signer: Signer,
|
2022-04-25 13:12:37 +00:00
|
|
|
deployment: Deployment): ?ContractInteractions =
|
|
|
|
|
|
|
|
without address =? deployment.address(Storage):
|
|
|
|
error "Unable to determine address of the Storage smart contract"
|
|
|
|
return none ContractInteractions
|
|
|
|
|
|
|
|
let contract = Storage.new(address, signer)
|
2022-04-13 11:18:38 +00:00
|
|
|
let market = OnChainMarket.new(contract)
|
|
|
|
let proofs = OnChainProofs.new(contract)
|
2022-05-17 14:42:03 +00:00
|
|
|
let clock = OnChainClock.new(signer.provider)
|
2022-04-25 13:12:37 +00:00
|
|
|
some ContractInteractions(
|
2022-05-17 14:51:29 +00:00
|
|
|
purchasing: Purchasing.new(market, clock),
|
2022-05-17 15:02:03 +00:00
|
|
|
sales: Sales.new(market, clock),
|
2022-05-17 14:42:03 +00:00
|
|
|
proving: Proving.new(proofs, clock),
|
|
|
|
clock: clock
|
2022-04-13 11:18:38 +00:00
|
|
|
)
|
|
|
|
|
2022-04-14 08:34:42 +00:00
|
|
|
proc new*(_: type ContractInteractions,
|
|
|
|
providerUrl: string,
|
2022-04-25 13:12:37 +00:00
|
|
|
deploymentFile: string = string.default,
|
|
|
|
account = Address.default): ?ContractInteractions =
|
|
|
|
|
2022-04-14 08:34:42 +00:00
|
|
|
let provider = JsonRpcProvider.new(providerUrl)
|
2022-04-25 13:12:37 +00:00
|
|
|
|
2022-04-14 08:34:42 +00:00
|
|
|
var signer: Signer
|
|
|
|
if account == Address.default:
|
|
|
|
signer = provider.getSigner()
|
|
|
|
else:
|
|
|
|
signer = provider.getSigner(account)
|
|
|
|
|
2022-04-25 13:12:37 +00:00
|
|
|
var deploy: Deployment
|
|
|
|
try:
|
|
|
|
if deploymentFile == string.default:
|
|
|
|
deploy = deployment()
|
|
|
|
else:
|
|
|
|
deploy = deployment(deploymentFile)
|
|
|
|
except IOError as e:
|
|
|
|
error "Unable to read deployment json", msg = e.msg
|
|
|
|
return none ContractInteractions
|
|
|
|
|
|
|
|
ContractInteractions.new(signer, deploy)
|
|
|
|
|
|
|
|
proc new*(_: type ContractInteractions): ?ContractInteractions =
|
2022-04-14 08:34:42 +00:00
|
|
|
ContractInteractions.new("ws://localhost:8545")
|
2022-04-14 10:48:10 +00:00
|
|
|
|
|
|
|
proc start*(interactions: ContractInteractions) {.async.} =
|
2022-05-17 14:42:03 +00:00
|
|
|
await interactions.clock.start()
|
2022-05-17 14:40:57 +00:00
|
|
|
await interactions.sales.start()
|
2022-05-17 14:40:21 +00:00
|
|
|
await interactions.proving.start()
|
2022-04-14 10:48:10 +00:00
|
|
|
|
|
|
|
proc stop*(interactions: ContractInteractions) {.async.} =
|
2022-05-17 14:40:57 +00:00
|
|
|
await interactions.sales.stop()
|
2022-05-17 14:40:21 +00:00
|
|
|
await interactions.proving.stop()
|
2022-05-17 14:42:03 +00:00
|
|
|
await interactions.clock.stop()
|