Test contracts with polling

This commit is contained in:
Mark Spanbroek 2023-06-27 15:59:31 +02:00 committed by markspanbroek
parent 2481bda6e4
commit 7e346914c0
2 changed files with 207 additions and 199 deletions

View File

@ -15,7 +15,9 @@ type
method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.} method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.}
method myBalance(token: TestToken): UInt256 {.contract, view.} method myBalance(token: TestToken): UInt256 {.contract, view.}
suite "Contracts": for url in ["ws://localhost:8545", "http://localhost:8545"]:
suite "Contracts (" & url & ")":
var token: TestToken var token: TestToken
var provider: JsonRpcProvider var provider: JsonRpcProvider
@ -23,7 +25,7 @@ suite "Contracts":
var accounts: seq[Address] var accounts: seq[Address]
setup: setup:
provider = JsonRpcProvider.new("ws://localhost:8545") provider = JsonRpcProvider.new(url, pollingInterval = 100.millis)
snapshot = await provider.send("evm_snapshot") snapshot = await provider.send("evm_snapshot")
accounts = await provider.listAccounts() accounts = await provider.listAccounts()
let deployment = readDeployment() let deployment = readDeployment()
@ -149,14 +151,15 @@ suite "Contracts":
discard await token.connect(signer0).mint(accounts[0], 100.u256) discard await token.connect(signer0).mint(accounts[0], 100.u256)
await token.connect(signer0).transfer(accounts[1], 50.u256) await token.connect(signer0).transfer(accounts[1], 50.u256)
await token.connect(signer1).transfer(accounts[2], 25.u256) await token.connect(signer1).transfer(accounts[2], 25.u256)
await subscription.unsubscribe()
check transfers == @[ check eventually transfers == @[
Transfer(receiver: accounts[0], value: 100.u256), Transfer(receiver: accounts[0], value: 100.u256),
Transfer(sender: accounts[0], receiver: accounts[1], value: 50.u256), Transfer(sender: accounts[0], receiver: accounts[1], value: 50.u256),
Transfer(sender: accounts[1], receiver: accounts[2], value: 25.u256) Transfer(sender: accounts[1], receiver: accounts[2], value: 25.u256)
] ]
await subscription.unsubscribe()
test "stops receiving events when unsubscribed": test "stops receiving events when unsubscribed":
var transfers: seq[Transfer] var transfers: seq[Transfer]
@ -167,11 +170,14 @@ suite "Contracts":
let subscription = await token.subscribe(Transfer, handleTransfer) let subscription = await token.subscribe(Transfer, handleTransfer)
discard await token.connect(signer0).mint(accounts[0], 100.u256) discard await token.connect(signer0).mint(accounts[0], 100.u256)
check eventually transfers.len == 1
await subscription.unsubscribe() await subscription.unsubscribe()
await token.connect(signer0).transfer(accounts[1], 50.u256) await token.connect(signer0).transfer(accounts[1], 50.u256)
await sleepAsync(100.millis)
check transfers == @[Transfer(receiver: accounts[0], value: 100.u256)] check transfers.len == 1
test "can wait for contract interaction tx to be mined": test "can wait for contract interaction tx to be mined":
# must not be awaited so we can get newHeads inside of .wait # must not be awaited so we can get newHeads inside of .wait

View File

@ -13,7 +13,9 @@ type
method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.} method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.}
suite "ERC20": for url in ["ws://localhost:8545", "http://localhost:8545"]:
suite "ERC20 (" & url & ")":
var token, token1: Erc20Token var token, token1: Erc20Token
var testToken: TestToken var testToken: TestToken
@ -22,7 +24,7 @@ suite "ERC20":
var accounts: seq[Address] var accounts: seq[Address]
setup: setup:
provider = JsonRpcProvider.new("ws://localhost:8545") provider = JsonRpcProvider.new(url, pollingInterval = 100.millis)
snapshot = await provider.send("evm_snapshot") snapshot = await provider.send("evm_snapshot")
accounts = await provider.listAccounts() accounts = await provider.listAccounts()
let deployment = readDeployment() let deployment = readDeployment()