Skip to content

Commit

Permalink
[test] Add integration test for sales and purchasing
Browse files Browse the repository at this point in the history
  • Loading branch information
markspanbroek committed May 19, 2022
1 parent a87986c commit d6393de
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
15 changes: 15 additions & 0 deletions tests/integration/tokens.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import dagger/contracts
import dagger/contracts/testtoken

proc mint*(signer: Signer, amount = 1_000_000.u256) {.async.} =
## Mints a considerable amount of tokens and approves them for transfer to
## the Storage contract.
let token = TestToken.new(!deployment().address(TestToken), signer)
let storage = Storage.new(!deployment().address(Storage), signer)
await token.mint(await signer.getAddress(), amount)
await token.approve(storage.address, amount)

proc deposit*(signer: Signer) {.async.} =
## Deposits sufficient collateral into the Storage contract.
let storage = Storage.new(!deployment().address(Storage), signer)
await storage.deposit(await storage.collateralAmount())
53 changes: 49 additions & 4 deletions tests/testIntegration.nim
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import std/osproc
import std/httpclient
import std/json
import pkg/asynctest
import pkg/chronos
import ./ethertest
import ./contracts/time
import ./integration/nodes
import ./integration/tokens

suite "Integration tests":
ethersuite "Integration tests":

var node1, node2: Process
var baseurl1, baseurl2: string
var client: HttpClient

setup:
node1 = startNode ["--api-port=8080", "--udp-port=8090"]
node2 = startNode ["--api-port=8081", "--udp-port=8091"]
await provider.getSigner(accounts[0]).mint()
await provider.getSigner(accounts[1]).mint()
await provider.getSigner(accounts[1]).deposit()
node1 = startNode [
"--api-port=8080",
"--udp-port=8090",
"--eth-account=" & $accounts[0]
]
node2 = startNode [
"--api-port=8081",
"--udp-port=8091",
"--eth-account=" & $accounts[1]
]
baseurl1 = "http://localhost:8080/api/dagger/v1"
baseurl2 = "http://localhost:8081/api/dagger/v1"
client = newHttpClient()
Expand Down Expand Up @@ -62,3 +75,35 @@ suite "Integration tests":
let json = parseJson(response.body)
check json["request"]["ask"]["duration"].getStr == "0x1"
check json["request"]["ask"]["maxPrice"].getStr == "0x2"

test "nodes negotiate contracts on the marketplace":

proc sell =
let json = %*{"size": "0x1F00", "duration": "0x200", "minPrice": "0x300"}
discard client.post(baseurl2 & "/sales/availability", $json)

proc available: JsonNode =
client.get(baseurl2 & "/sales/availability").body.parseJson

proc upload: string =
client.post(baseurl1 & "/upload", "some file contents").body

proc buy(cid: string): string =
let expiry = ((waitFor provider.currentTime()) + 5).toHex
let json = %*{"duration": "0x100", "maxPrice": "0x400", "expiry": expiry}
client.post(baseurl1 & "/storage/request/" & cid, $json).body

proc finish(purchase: string): JsonNode =
while true:
let response = client.get(baseurl1 & "/storage/purchases/" & purchase)
result = parseJson(response.body)
if result["finished"].getBool:
break
waitFor sleepAsync(1.seconds)

sell()
let purchase = upload().buy().finish()

check purchase["error"].getStr == ""
check purchase["selected"].len > 0
check available().len == 0

0 comments on commit d6393de

Please sign in to comment.