2022-01-25 14:05:54 +00:00
|
|
|
import std/json
|
2022-01-20 11:56:18 +00:00
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/stint
|
|
|
|
import pkg/ethers
|
|
|
|
import ./hardhat
|
|
|
|
|
|
|
|
type
|
2022-01-20 13:00:28 +00:00
|
|
|
Erc20* = ref object of Contract
|
2022-01-20 11:56:18 +00:00
|
|
|
TestToken = ref object of Erc20
|
|
|
|
|
2022-01-25 16:17:43 +00:00
|
|
|
method totalSupply*(erc20: Erc20): UInt256 {.base, contract, view.}
|
|
|
|
method balanceOf*(erc20: Erc20, account: Address): UInt256 {.base, contract, view.}
|
|
|
|
method allowance*(erc20: Erc20, owner, spender: Address): UInt256 {.base, contract, view.}
|
|
|
|
|
|
|
|
method mint(token: TestToken, holder: Address, amount: UInt256) {.base, contract.}
|
2022-01-20 11:56:18 +00:00
|
|
|
|
|
|
|
suite "Contracts":
|
|
|
|
|
|
|
|
var token: TestToken
|
|
|
|
var provider: JsonRpcProvider
|
2022-01-25 14:05:54 +00:00
|
|
|
var snapshot: JsonNode
|
2022-01-25 16:17:43 +00:00
|
|
|
var accounts: seq[Address]
|
2022-01-20 11:56:18 +00:00
|
|
|
|
|
|
|
setup:
|
|
|
|
provider = JsonRpcProvider.new()
|
2022-01-25 14:05:54 +00:00
|
|
|
snapshot = await provider.send("evm_snapshot")
|
2022-01-25 16:17:43 +00:00
|
|
|
accounts = await provider.listAccounts()
|
2022-01-20 11:56:18 +00:00
|
|
|
let deployment = readDeployment()
|
|
|
|
token = TestToken.new(!deployment.address(TestToken), provider)
|
|
|
|
|
2022-01-25 14:05:54 +00:00
|
|
|
teardown:
|
|
|
|
discard await provider.send("evm_revert", @[snapshot])
|
|
|
|
|
2022-01-25 16:17:43 +00:00
|
|
|
test "can call constant functions":
|
2022-01-20 11:56:18 +00:00
|
|
|
check (await token.totalSupply()) == 0.u256
|
|
|
|
check (await token.balanceOf(accounts[0])) == 0.u256
|
|
|
|
check (await token.allowance(accounts[0], accounts[1])) == 0.u256
|
2022-01-25 16:17:43 +00:00
|
|
|
|
|
|
|
test "can call non-constant functions":
|
|
|
|
token = TestToken.new(token.address, provider.getSigner())
|
|
|
|
await token.mint(accounts[1], 100.u256)
|
|
|
|
check (await token.totalSupply()) == 100.u256
|
|
|
|
check (await token.balanceOf(accounts[1])) == 100.u256
|
|
|
|
|
|
|
|
test "can call non-constant functions without a signer":
|
|
|
|
await token.mint(accounts[1], 100.u256)
|
|
|
|
check (await token.balanceOf(accounts[1])) == 0.u256
|
|
|
|
|
|
|
|
test "can call constant functions without a return type":
|
|
|
|
token = TestToken.new(token.address, provider.getSigner())
|
|
|
|
proc mint(token: TestToken, holder: Address, amount: UInt256) {.contract, view.}
|
|
|
|
await mint(token, accounts[1], 100.u256)
|
|
|
|
check (await balanceOf(token, accounts[1])) == 0.u256
|
|
|
|
|
|
|
|
test "fails to compile when non-constant function has a return type":
|
|
|
|
let works = compiles:
|
|
|
|
proc foo(token: TestToken, bar: Address): UInt256 {.contract.}
|
|
|
|
check not works
|