2023-03-29 11:41:44 +00:00
|
|
|
import std/json
|
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/stint
|
|
|
|
import pkg/ethers
|
|
|
|
import pkg/ethers/erc20
|
|
|
|
import ./hardhat
|
|
|
|
|
|
|
|
type
|
|
|
|
TestToken = ref object of Erc20Token
|
|
|
|
|
|
|
|
method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.}
|
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
for url in ["ws://localhost:8545", "http://localhost:8545"]:
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
suite "ERC20 (" & url & ")":
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-06-29 08:23:14 +00:00
|
|
|
var token: Erc20Token
|
2023-06-27 13:59:31 +00:00
|
|
|
var testToken: TestToken
|
|
|
|
var provider: JsonRpcProvider
|
|
|
|
var snapshot: JsonNode
|
|
|
|
var accounts: seq[Address]
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
setup:
|
|
|
|
provider = JsonRpcProvider.new(url, pollingInterval = 100.millis)
|
|
|
|
snapshot = await provider.send("evm_snapshot")
|
|
|
|
accounts = await provider.listAccounts()
|
|
|
|
let deployment = readDeployment()
|
|
|
|
testToken = TestToken.new(!deployment.address(TestToken), provider.getSigner())
|
|
|
|
token = Erc20Token.new(!deployment.address(TestToken), provider.getSigner())
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
teardown:
|
|
|
|
discard await provider.send("evm_revert", @[snapshot])
|
2023-06-27 14:40:29 +00:00
|
|
|
await provider.close()
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
test "retrieves basic information":
|
|
|
|
check (await token.name()) == "TestToken"
|
|
|
|
check (await token.symbol()) == "TST"
|
|
|
|
check (await token.decimals()) == 12
|
|
|
|
check (await token.totalSupply()) == 0.u256
|
|
|
|
check (await token.balanceOf(accounts[0])) == 0.u256
|
|
|
|
check (await token.allowance(accounts[0], accounts[1])) == 0.u256
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
test "transfer tokens":
|
|
|
|
check (await token.balanceOf(accounts[0])) == 0.u256
|
|
|
|
check (await token.allowance(accounts[0], accounts[1])) == 0.u256
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
discard await testToken.mint(accounts[0], 100.u256)
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
check (await token.totalSupply()) == 100.u256
|
|
|
|
check (await token.balanceOf(accounts[0])) == 100.u256
|
|
|
|
check (await token.balanceOf(accounts[1])) == 0.u256
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-07-04 13:53:46 +00:00
|
|
|
discard await token.transfer(accounts[1], 50.u256)
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
check (await token.balanceOf(accounts[0])) == 50.u256
|
|
|
|
check (await token.balanceOf(accounts[1])) == 50.u256
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
test "approve tokens":
|
|
|
|
discard await testToken.mint(accounts[0], 100.u256)
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
check (await token.allowance(accounts[0], accounts[1])) == 0.u256
|
|
|
|
check (await token.balanceOf(accounts[0])) == 100.u256
|
|
|
|
check (await token.balanceOf(accounts[1])) == 0.u256
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-07-04 13:53:46 +00:00
|
|
|
discard await token.approve(accounts[1], 50.u256)
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
check (await token.allowance(accounts[0], accounts[1])) == 50.u256
|
|
|
|
check (await token.balanceOf(accounts[0])) == 100.u256
|
|
|
|
check (await token.balanceOf(accounts[1])) == 0.u256
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-10-16 08:23:58 +00:00
|
|
|
test "increase/decrease allowance":
|
|
|
|
discard await testToken.mint(accounts[0], 100.u256)
|
|
|
|
|
|
|
|
check (await token.allowance(accounts[0], accounts[1])) == 0.u256
|
|
|
|
check (await token.balanceOf(accounts[0])) == 100.u256
|
|
|
|
check (await token.balanceOf(accounts[1])) == 0.u256
|
|
|
|
|
|
|
|
discard await token.increaseAllowance(accounts[1], 50.u256)
|
|
|
|
|
|
|
|
check (await token.allowance(accounts[0], accounts[1])) == 50.u256
|
|
|
|
check (await token.balanceOf(accounts[0])) == 100.u256
|
|
|
|
check (await token.balanceOf(accounts[1])) == 0.u256
|
|
|
|
|
|
|
|
discard await token.increaseAllowance(accounts[1], 50.u256)
|
|
|
|
|
|
|
|
check (await token.allowance(accounts[0], accounts[1])) == 100.u256
|
|
|
|
check (await token.balanceOf(accounts[0])) == 100.u256
|
|
|
|
check (await token.balanceOf(accounts[1])) == 0.u256
|
|
|
|
|
|
|
|
discard await token.decreaseAllowance(accounts[1], 50.u256)
|
|
|
|
|
|
|
|
check (await token.allowance(accounts[0], accounts[1])) == 50.u256
|
|
|
|
check (await token.balanceOf(accounts[0])) == 100.u256
|
|
|
|
check (await token.balanceOf(accounts[1])) == 0.u256
|
|
|
|
|
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
test "transferFrom tokens":
|
|
|
|
let senderAccount = accounts[0]
|
|
|
|
let receiverAccount = accounts[1]
|
|
|
|
let receiverAccountSigner = provider.getSigner(receiverAccount)
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
check (await token.balanceOf(senderAccount)) == 0.u256
|
|
|
|
check (await token.allowance(senderAccount, receiverAccount)) == 0.u256
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
discard await testToken.mint(senderAccount, 100.u256)
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
check (await token.totalSupply()) == 100.u256
|
|
|
|
check (await token.balanceOf(senderAccount)) == 100.u256
|
|
|
|
check (await token.balanceOf(receiverAccount)) == 0.u256
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-07-04 13:53:46 +00:00
|
|
|
discard await token.approve(receiverAccount, 50.u256)
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-06-27 13:59:31 +00:00
|
|
|
check (await token.allowance(senderAccount, receiverAccount)) == 50.u256
|
|
|
|
check (await token.balanceOf(senderAccount)) == 100.u256
|
|
|
|
check (await token.balanceOf(receiverAccount)) == 0.u256
|
2023-03-29 11:41:44 +00:00
|
|
|
|
2023-07-04 13:53:46 +00:00
|
|
|
discard await token.connect(receiverAccountSigner).transferFrom(senderAccount, receiverAccount, 50.u256)
|
2023-06-27 13:59:31 +00:00
|
|
|
|
|
|
|
check (await token.balanceOf(senderAccount)) == 50.u256
|
|
|
|
check (await token.balanceOf(receiverAccount)) == 50.u256
|
|
|
|
check (await token.allowance(senderAccount, receiverAccount)) == 0.u256
|
2023-03-29 11:41:44 +00:00
|
|
|
|