2021-02-08 14:19:34 +00:00
|
|
|
# Tests of Swap contracts via external module
|
|
|
|
#
|
|
|
|
import
|
2021-02-17 05:50:46 +00:00
|
|
|
std/[unittest, options, tables, sets, osproc, strutils, strformat, json],
|
2021-02-23 05:15:06 +00:00
|
|
|
chronicles,
|
|
|
|
../test_helpers, ./utils,
|
|
|
|
../../waku/v2/protocol/waku_swap/waku_swap_contracts
|
2021-02-08 14:19:34 +00:00
|
|
|
|
|
|
|
procSuite "Basic balance test":
|
2021-02-17 05:50:46 +00:00
|
|
|
var aliceSwapAddress = ""
|
|
|
|
var signature = ""
|
|
|
|
var erc20address = ""
|
2021-02-08 14:19:34 +00:00
|
|
|
test "Get pwd of swap module":
|
|
|
|
let (output, errC) = osproc.execCmdEx("(cd ../swap-contracts-module && pwd)")
|
2021-02-23 05:15:06 +00:00
|
|
|
debug "output", output
|
2021-02-08 14:19:34 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
contains(output, "swap-contracts-module")
|
|
|
|
|
|
|
|
test "Get balance from running node":
|
|
|
|
# NOTE: This corresponds to the first default account in Hardhat
|
2021-02-25 02:47:48 +00:00
|
|
|
let balRes = waku_swap_contracts.getBalance("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")
|
|
|
|
var balance: float
|
|
|
|
if balRes.isOk():
|
|
|
|
let json = balRes[]
|
|
|
|
let balanceStr = json["balance"].getStr()
|
|
|
|
balance = parseFloat(balanceStr)
|
2021-02-08 14:19:34 +00:00
|
|
|
|
|
|
|
check:
|
2021-02-25 02:47:48 +00:00
|
|
|
balRes.isOk()
|
|
|
|
balance > 0
|
2021-02-08 14:19:34 +00:00
|
|
|
|
2021-02-17 05:50:46 +00:00
|
|
|
test "Setup Swap":
|
2021-02-25 02:47:48 +00:00
|
|
|
let res = waku_swap_contracts.setupSwap()
|
|
|
|
let json = res[]
|
2021-02-17 05:50:46 +00:00
|
|
|
|
|
|
|
var aliceAddress = json["aliceAddress"].getStr()
|
|
|
|
aliceSwapAddress = json["aliceSwapAddress"].getStr()
|
|
|
|
erc20address = json["erc20address"].getStr()
|
2021-02-23 05:15:06 +00:00
|
|
|
debug "erc20address", erc20address
|
|
|
|
debug "json", json
|
2021-02-17 05:50:46 +00:00
|
|
|
|
|
|
|
# Contains default Alice account
|
|
|
|
check:
|
|
|
|
contains(aliceAddress, "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")
|
|
|
|
|
|
|
|
test "Sign Cheque":
|
2021-02-25 02:47:48 +00:00
|
|
|
var sigRes = waku_swap_contracts.signCheque(aliceSwapAddress)
|
|
|
|
if sigRes.isOk():
|
|
|
|
let json = sigRes[]
|
|
|
|
signature = json["signature"].getStr()
|
2021-02-17 05:50:46 +00:00
|
|
|
|
|
|
|
check:
|
2021-02-25 02:47:48 +00:00
|
|
|
sigRes.isOk()
|
2021-02-17 05:50:46 +00:00
|
|
|
contains(signature, "0x")
|
|
|
|
|
2021-02-23 05:15:06 +00:00
|
|
|
test "Get ERC20 Balances":
|
2021-02-25 02:47:48 +00:00
|
|
|
let res = waku_swap_contracts.getERC20Balances(erc20address)
|
2021-02-17 05:50:46 +00:00
|
|
|
|
|
|
|
check:
|
2021-02-25 02:47:48 +00:00
|
|
|
res.isOk()
|
|
|
|
res[]["bobBalance"].getInt() == 10000
|
2021-02-17 05:50:46 +00:00
|
|
|
|
|
|
|
test "Redeem cheque and check balance":
|
2021-02-25 02:47:48 +00:00
|
|
|
let redeemRes = waku_swap_contracts.redeemCheque(aliceSwapAddress, signature)
|
|
|
|
var resp = redeemRes[]["resp"].getStr()
|
|
|
|
debug "Redeem resp", resp
|
2021-02-17 05:50:46 +00:00
|
|
|
|
2021-02-25 02:47:48 +00:00
|
|
|
let balRes = getERC20Balances(erc20address)
|
2021-02-17 05:50:46 +00:00
|
|
|
|
|
|
|
# Balance for Bob has now increased
|
|
|
|
check:
|
2021-02-25 02:47:48 +00:00
|
|
|
redeemRes.isOk()
|
|
|
|
balRes.isOk()
|
|
|
|
balRes[]["bobBalance"].getInt() == 10500
|