2022-01-25 14:05:54 +00:00
|
|
|
import std/json
|
2022-01-20 11:56:18 +00:00
|
|
|
import pkg/asynctest
|
2022-05-23 14:20:51 +00:00
|
|
|
import pkg/questionable
|
2022-01-20 11:56:18 +00:00
|
|
|
import pkg/stint
|
|
|
|
import pkg/ethers
|
2023-03-29 11:41:44 +00:00
|
|
|
import pkg/ethers/erc20
|
2022-01-20 11:56:18 +00:00
|
|
|
import ./hardhat
|
feat: Allow contract transactions to be waited on
Allow waiting for a specified number of confirmations for contract transactions.
This change only requires an optional TransactionResponse return type to be added to the contract function. This allows the transaction hash to be passed to `.wait`.
For example, previously the `mint` method looked like this without a return value:
```
method mint(token: TestToken, holder: Address, amount: UInt256) {.base, contract.}
```
it still works without a return value, but if we want to wait for a 3 confirmations, we can now define it like this:
```
method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.}
```
and use like this:
```
let receipt = await token.connect(signer0)
.mint(accounts[1], 100.u256)
.wait(3) # wait for 3 confirmations
```
2022-05-17 04:57:18 +00:00
|
|
|
import ./miner
|
2022-07-11 13:15:01 +00:00
|
|
|
import ./mocks
|
2022-01-20 11:56:18 +00:00
|
|
|
|
|
|
|
type
|
2022-02-02 15:56:37 +00:00
|
|
|
|
2023-03-29 11:41:44 +00:00
|
|
|
TestToken = ref object of Erc20Token
|
2022-01-20 11:56:18 +00:00
|
|
|
|
2022-05-23 14:20:51 +00:00
|
|
|
method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.}
|
2023-06-13 14:24:59 +00:00
|
|
|
method myBalance(token: TestToken): UInt256 {.contract, view.}
|
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:
|
2022-02-02 15:56:37 +00:00
|
|
|
provider = JsonRpcProvider.new("ws://localhost:8545")
|
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-06-08 09:17:16 +00:00
|
|
|
check (await token.name()) == "TestToken"
|
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())
|
feat: Allow contract transactions to be waited on
Allow waiting for a specified number of confirmations for contract transactions.
This change only requires an optional TransactionResponse return type to be added to the contract function. This allows the transaction hash to be passed to `.wait`.
For example, previously the `mint` method looked like this without a return value:
```
method mint(token: TestToken, holder: Address, amount: UInt256) {.base, contract.}
```
it still works without a return value, but if we want to wait for a 3 confirmations, we can now define it like this:
```
method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.}
```
and use like this:
```
let receipt = await token.connect(signer0)
.mint(accounts[1], 100.u256)
.wait(3) # wait for 3 confirmations
```
2022-05-17 04:57:18 +00:00
|
|
|
discard await token.mint(accounts[1], 100.u256)
|
2022-01-25 16:17:43 +00:00
|
|
|
check (await token.totalSupply()) == 100.u256
|
|
|
|
check (await token.balanceOf(accounts[1])) == 100.u256
|
|
|
|
|
2023-06-13 14:24:59 +00:00
|
|
|
test "can call constant functions with a signer and the account is used for the call":
|
|
|
|
let signer0 = provider.getSigner(accounts[0])
|
|
|
|
let signer1 = provider.getSigner(accounts[1])
|
|
|
|
discard await token.connect(signer0).mint(accounts[1], 100.u256)
|
|
|
|
check (await token.connect(signer0).myBalance()) == 0.u256
|
|
|
|
check (await token.connect(signer1).myBalance()) == 100.u256
|
|
|
|
|
2022-01-25 16:17:43 +00:00
|
|
|
test "can call non-constant functions without a signer":
|
feat: Allow contract transactions to be waited on
Allow waiting for a specified number of confirmations for contract transactions.
This change only requires an optional TransactionResponse return type to be added to the contract function. This allows the transaction hash to be passed to `.wait`.
For example, previously the `mint` method looked like this without a return value:
```
method mint(token: TestToken, holder: Address, amount: UInt256) {.base, contract.}
```
it still works without a return value, but if we want to wait for a 3 confirmations, we can now define it like this:
```
method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.}
```
and use like this:
```
let receipt = await token.connect(signer0)
.mint(accounts[1], 100.u256)
.wait(3) # wait for 3 confirmations
```
2022-05-17 04:57:18 +00:00
|
|
|
discard await token.mint(accounts[1], 100.u256)
|
2022-01-25 16:17:43 +00:00
|
|
|
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
|
|
|
|
|
2022-05-24 05:13:25 +00:00
|
|
|
test "can call non-constant functions without a return type":
|
|
|
|
token = TestToken.new(token.address, provider.getSigner())
|
|
|
|
proc mint(token: TestToken, holder: Address, amount: UInt256) {.contract.}
|
|
|
|
await token.mint(accounts[1], 100.u256)
|
|
|
|
check (await balanceOf(token, accounts[1])) == 100.u256
|
|
|
|
|
|
|
|
test "can call non-constant functions with a ?TransactionResponse return type":
|
|
|
|
token = TestToken.new(token.address, provider.getSigner())
|
|
|
|
proc mint(token: TestToken,
|
|
|
|
holder: Address,
|
|
|
|
amount: UInt256): ?TransactionResponse {.contract.}
|
|
|
|
let txResp = await token.mint(accounts[1], 100.u256)
|
2022-06-29 11:54:31 +00:00
|
|
|
check txResp is (?TransactionResponse)
|
2022-05-24 05:13:25 +00:00
|
|
|
check txResp.isSome
|
|
|
|
|
|
|
|
test "can call non-constant functions with a Confirmable return type":
|
|
|
|
|
|
|
|
token = TestToken.new(token.address, provider.getSigner())
|
|
|
|
proc mint(token: TestToken,
|
|
|
|
holder: Address,
|
|
|
|
amount: UInt256): Confirmable {.contract.}
|
|
|
|
let txResp = await token.mint(accounts[1], 100.u256)
|
|
|
|
check txResp is Confirmable
|
|
|
|
check txResp.isSome
|
|
|
|
|
2022-01-26 09:38:17 +00:00
|
|
|
test "fails to compile when function has an implementation":
|
|
|
|
let works = compiles:
|
|
|
|
proc foo(token: TestToken, bar: Address) {.contract.} = discard
|
|
|
|
check not works
|
|
|
|
|
|
|
|
test "fails to compile when function has no parameters":
|
|
|
|
let works = compiles:
|
|
|
|
proc foo() {.contract.}
|
|
|
|
check not works
|
|
|
|
|
2022-01-25 16:17:43 +00:00
|
|
|
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
|
2022-01-26 11:23:30 +00:00
|
|
|
|
|
|
|
test "can connect to different providers and signers":
|
|
|
|
let signer0 = provider.getSigner(accounts[0])
|
|
|
|
let signer1 = provider.getSigner(accounts[1])
|
feat: Allow contract transactions to be waited on
Allow waiting for a specified number of confirmations for contract transactions.
This change only requires an optional TransactionResponse return type to be added to the contract function. This allows the transaction hash to be passed to `.wait`.
For example, previously the `mint` method looked like this without a return value:
```
method mint(token: TestToken, holder: Address, amount: UInt256) {.base, contract.}
```
it still works without a return value, but if we want to wait for a 3 confirmations, we can now define it like this:
```
method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.}
```
and use like this:
```
let receipt = await token.connect(signer0)
.mint(accounts[1], 100.u256)
.wait(3) # wait for 3 confirmations
```
2022-05-17 04:57:18 +00:00
|
|
|
discard await token.connect(signer0).mint(accounts[0], 100.u256)
|
2022-01-26 11:23:30 +00:00
|
|
|
await token.connect(signer0).transfer(accounts[1], 50.u256)
|
|
|
|
await token.connect(signer1).transfer(accounts[2], 25.u256)
|
|
|
|
check (await token.connect(provider).balanceOf(accounts[0])) == 50.u256
|
|
|
|
check (await token.connect(provider).balanceOf(accounts[1])) == 25.u256
|
|
|
|
check (await token.connect(provider).balanceOf(accounts[2])) == 25.u256
|
2022-02-02 15:56:37 +00:00
|
|
|
|
2022-07-11 13:15:01 +00:00
|
|
|
test "takes custom values for nonce, gasprice and gaslimit":
|
2022-07-12 10:43:56 +00:00
|
|
|
let overrides = TransactionOverrides(
|
2022-07-11 13:15:01 +00:00
|
|
|
nonce: some 100.u256,
|
|
|
|
gasPrice: some 200.u256,
|
|
|
|
gasLimit: some 300.u256
|
|
|
|
)
|
|
|
|
let signer = MockSigner.new(provider)
|
|
|
|
discard await token.connect(signer).mint(accounts[0], 42.u256, overrides)
|
|
|
|
check signer.transactions.len == 1
|
|
|
|
check signer.transactions[0].nonce == overrides.nonce
|
|
|
|
check signer.transactions[0].gasPrice == overrides.gasPrice
|
|
|
|
check signer.transactions[0].gasLimit == overrides.gasLimit
|
|
|
|
|
2023-04-18 15:28:28 +00:00
|
|
|
test "can call functions for different block heights":
|
|
|
|
let block1 = await provider.getBlockNumber()
|
|
|
|
let signer = provider.getSigner(accounts[0])
|
|
|
|
discard await token.connect(signer).mint(accounts[0], 100.u256)
|
|
|
|
let block2 = await provider.getBlockNumber()
|
|
|
|
|
|
|
|
let beforeMint = CallOverrides(blockTag: some BlockTag.init(block1))
|
|
|
|
let afterMint = CallOverrides(blockTag: some BlockTag.init(block2))
|
|
|
|
|
|
|
|
check (await token.balanceOf(accounts[0], beforeMint)) == 0
|
|
|
|
check (await token.balanceOf(accounts[0], afterMint)) == 100
|
|
|
|
|
2022-02-02 15:56:37 +00:00
|
|
|
test "receives events when subscribed":
|
|
|
|
var transfers: seq[Transfer]
|
|
|
|
|
|
|
|
proc handleTransfer(transfer: Transfer) =
|
|
|
|
transfers.add(transfer)
|
|
|
|
|
|
|
|
let signer0 = provider.getSigner(accounts[0])
|
|
|
|
let signer1 = provider.getSigner(accounts[1])
|
|
|
|
|
|
|
|
let subscription = await token.subscribe(Transfer, handleTransfer)
|
feat: Allow contract transactions to be waited on
Allow waiting for a specified number of confirmations for contract transactions.
This change only requires an optional TransactionResponse return type to be added to the contract function. This allows the transaction hash to be passed to `.wait`.
For example, previously the `mint` method looked like this without a return value:
```
method mint(token: TestToken, holder: Address, amount: UInt256) {.base, contract.}
```
it still works without a return value, but if we want to wait for a 3 confirmations, we can now define it like this:
```
method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.}
```
and use like this:
```
let receipt = await token.connect(signer0)
.mint(accounts[1], 100.u256)
.wait(3) # wait for 3 confirmations
```
2022-05-17 04:57:18 +00:00
|
|
|
discard await token.connect(signer0).mint(accounts[0], 100.u256)
|
2022-02-02 15:56:37 +00:00
|
|
|
await token.connect(signer0).transfer(accounts[1], 50.u256)
|
|
|
|
await token.connect(signer1).transfer(accounts[2], 25.u256)
|
|
|
|
await subscription.unsubscribe()
|
|
|
|
|
|
|
|
check transfers == @[
|
|
|
|
Transfer(receiver: accounts[0], value: 100.u256),
|
|
|
|
Transfer(sender: accounts[0], receiver: accounts[1], value: 50.u256),
|
|
|
|
Transfer(sender: accounts[1], receiver: accounts[2], value: 25.u256)
|
|
|
|
]
|
|
|
|
|
|
|
|
test "stops receiving events when unsubscribed":
|
|
|
|
var transfers: seq[Transfer]
|
|
|
|
|
|
|
|
proc handleTransfer(transfer: Transfer) =
|
|
|
|
transfers.add(transfer)
|
|
|
|
|
|
|
|
let signer0 = provider.getSigner(accounts[0])
|
|
|
|
|
|
|
|
let subscription = await token.subscribe(Transfer, handleTransfer)
|
feat: Allow contract transactions to be waited on
Allow waiting for a specified number of confirmations for contract transactions.
This change only requires an optional TransactionResponse return type to be added to the contract function. This allows the transaction hash to be passed to `.wait`.
For example, previously the `mint` method looked like this without a return value:
```
method mint(token: TestToken, holder: Address, amount: UInt256) {.base, contract.}
```
it still works without a return value, but if we want to wait for a 3 confirmations, we can now define it like this:
```
method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.}
```
and use like this:
```
let receipt = await token.connect(signer0)
.mint(accounts[1], 100.u256)
.wait(3) # wait for 3 confirmations
```
2022-05-17 04:57:18 +00:00
|
|
|
discard await token.connect(signer0).mint(accounts[0], 100.u256)
|
2022-02-02 15:56:37 +00:00
|
|
|
await subscription.unsubscribe()
|
|
|
|
|
|
|
|
await token.connect(signer0).transfer(accounts[1], 50.u256)
|
|
|
|
|
|
|
|
check transfers == @[Transfer(receiver: accounts[0], value: 100.u256)]
|
feat: Allow contract transactions to be waited on
Allow waiting for a specified number of confirmations for contract transactions.
This change only requires an optional TransactionResponse return type to be added to the contract function. This allows the transaction hash to be passed to `.wait`.
For example, previously the `mint` method looked like this without a return value:
```
method mint(token: TestToken, holder: Address, amount: UInt256) {.base, contract.}
```
it still works without a return value, but if we want to wait for a 3 confirmations, we can now define it like this:
```
method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.}
```
and use like this:
```
let receipt = await token.connect(signer0)
.mint(accounts[1], 100.u256)
.wait(3) # wait for 3 confirmations
```
2022-05-17 04:57:18 +00:00
|
|
|
|
|
|
|
test "can wait for contract interaction tx to be mined":
|
2022-05-18 13:14:39 +00:00
|
|
|
# must not be awaited so we can get newHeads inside of .wait
|
|
|
|
let futMined = provider.mineBlocks(10)
|
feat: Allow contract transactions to be waited on
Allow waiting for a specified number of confirmations for contract transactions.
This change only requires an optional TransactionResponse return type to be added to the contract function. This allows the transaction hash to be passed to `.wait`.
For example, previously the `mint` method looked like this without a return value:
```
method mint(token: TestToken, holder: Address, amount: UInt256) {.base, contract.}
```
it still works without a return value, but if we want to wait for a 3 confirmations, we can now define it like this:
```
method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.}
```
and use like this:
```
let receipt = await token.connect(signer0)
.mint(accounts[1], 100.u256)
.wait(3) # wait for 3 confirmations
```
2022-05-17 04:57:18 +00:00
|
|
|
|
|
|
|
let signer0 = provider.getSigner(accounts[0])
|
|
|
|
let receipt = await token.connect(signer0)
|
|
|
|
.mint(accounts[1], 100.u256)
|
2022-05-18 13:14:39 +00:00
|
|
|
.confirm(3) # wait for 3 confirmations
|
feat: Allow contract transactions to be waited on
Allow waiting for a specified number of confirmations for contract transactions.
This change only requires an optional TransactionResponse return type to be added to the contract function. This allows the transaction hash to be passed to `.wait`.
For example, previously the `mint` method looked like this without a return value:
```
method mint(token: TestToken, holder: Address, amount: UInt256) {.base, contract.}
```
it still works without a return value, but if we want to wait for a 3 confirmations, we can now define it like this:
```
method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.}
```
and use like this:
```
let receipt = await token.connect(signer0)
.mint(accounts[1], 100.u256)
.wait(3) # wait for 3 confirmations
```
2022-05-17 04:57:18 +00:00
|
|
|
let endBlock = await provider.getBlockNumber()
|
|
|
|
|
|
|
|
check receipt.blockNumber.isSome # was eventually mined
|
2022-05-18 13:14:39 +00:00
|
|
|
|
|
|
|
# >= 3 because more blocks may have been mined by the time the
|
|
|
|
# check in `.wait` was done.
|
|
|
|
# +1 for the block the tx was mined in
|
|
|
|
check (endBlock - !receipt.blockNumber) + 1 >= 3
|
|
|
|
|
|
|
|
await futMined
|