Skip to content

Commit

Permalink
refactor(tests): use semantic value format
Browse files Browse the repository at this point in the history
  • Loading branch information
darwintree committed Dec 23, 2024
1 parent 9c2f05f commit 54b7d7f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
19 changes: 10 additions & 9 deletions tests/admin_control_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
from cfx_utils import CFX, Drip
from conflux.transactions import CONTRACT_DEFAULT_GAS, charged_of_huge_gas
from test_framework.test_framework import ConfluxTestFramework
from test_framework.util import assert_equal
Expand All @@ -19,14 +20,14 @@ def run_test(self):
acct1 = self.cfx.account.create()
self.log.info("addr=%s priv_key=%s", acct1.address, acct1.key.hex())
self.cfx_transfer(acct1.hex_address, value = 5)
assert_equal(self.cfx.get_balance(acct1.address).to("CFX").value, 5)
assert_equal(self.cfx.get_balance(acct1.address).to("CFX"), CFX(5))
self.w3.wallet.add_account(acct1)


acct2 = self.cfx.account.create()
self.log.info("addr2=%s priv_key2=%s", acct2.address, acct2.key.hex())
self.cfx_transfer(acct2.hex_address, value = 5)
assert_equal(self.cfx.get_balance(acct2.address).to("CFX").value, 5)
assert_equal(self.cfx.get_balance(acct2.address).to("CFX"), CFX(5))
self.w3.wallet.add_account(acct2)

# deploy pay contract
Expand All @@ -46,12 +47,12 @@ def run_test(self):
b0 = self.cfx.get_balance(acct1.address)
pay_contract.functions.recharge().transact({
"from": acct1.address,
"value": 10 ** 18,
"value": CFX(1),
"gas": gas,
"gasPrice": 1,
}).executed()
assert_equal(self.cfx.get_balance(contract_addr).value, 10 ** 18)
assert_equal(self.cfx.get_balance(acct1.address).value, b0.value - 10 ** 18 - charged_of_huge_gas(gas))
assert_equal(self.cfx.get_balance(contract_addr), CFX(1))
assert_equal(self.cfx.get_balance(acct1.address), b0 - CFX(1) - Drip(charged_of_huge_gas(gas)))
assert_equal(self.cfx.get_admin(contract_addr), acct1.address.lower())


Expand All @@ -62,7 +63,7 @@ def run_test(self):
"gasPrice": 1
}).executed()
assert_equal(self.cfx.get_admin(contract_addr), acct1.address.lower())
assert_equal(self.cfx.get_balance(acct2.address).value, 5 * 10 ** 18 - charged_of_huge_gas(gas))
assert_equal(self.cfx.get_balance(acct2.address), CFX(5) - Drip(charged_of_huge_gas(gas)))

# transfer admin (success)
admin_control_contract.functions.setAdmin(contract_addr, acct2.address).transact({
Expand All @@ -79,10 +80,10 @@ def run_test(self):
"gas": gas,
"gasPrice": 1,
}).executed()
assert_equal(self.cfx.get_balance(contract_addr).value, 0)
assert_equal(self.cfx.get_balance(acct2.address).value, 6 * 10 ** 18 - charged_of_huge_gas(gas) * 2)
assert_equal(self.cfx.get_balance(contract_addr), 0)
assert_equal(self.cfx.get_balance(acct2.address), CFX(6) - Drip(charged_of_huge_gas(gas) * 2))
assert_equal(self.cfx.get_collateral_for_storage(acct1.address), 0)
assert_equal(self.cfx.get_balance(acct1.address).value, b0.value + 512 * 976562500000000)
assert_equal(self.cfx.get_balance(acct1.address), b0 + Drip(512 * 976562500000000))

self.log.info("Pass")

Expand Down
7 changes: 4 additions & 3 deletions tests/cip107_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from conflux.rpc import RpcClient
from decimal import Decimal
from cfx_utils import CFX
from conflux.utils import *
from test_framework.util import *
from test_framework.mininode import *
Expand Down Expand Up @@ -104,7 +105,7 @@ def set_sponsor(self, value):
self.storage.address, [ZERO_ADDRESS]).transact().executed()
sponsorContract.setSponsorForCollateral(
self.storage.address).transact({
"value": int(value*10**18)
"value": CFX(Decimal(value))
}).executed()

def set_entry(self, index):
Expand Down Expand Up @@ -172,7 +173,7 @@ def test_change_sponsor(self):
self.w3.wallet.add_account(acct)

self.sponsorControl.functions.setSponsorForCollateral(storage.address()).transact({
"value": int(0.5*10**18),
"value": CFX(Decimal(0.5)),
"from": acct.address
}).executed()
check.checked_tick(
Expand Down
5 changes: 3 additions & 2 deletions tests/overlay_account_storage_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from cfx_utils import CFX
from conflux_web3.contract import ConfluxContract
from conflux_web3.contract.function import ConfluxContractFunction
from conflux.utils import *
Expand Down Expand Up @@ -134,7 +135,7 @@ def run_task_group(self, customized_enactor):
assert_equal(len(receipt["storageReleased"]), 1)

self.internal_contract("SponsorWhitelistControl").functions.setSponsorForCollateral(storage_contract.address).transact({
"value": 1000 * 10 ** 18
"value": CFX(1000)
}).executed()
storage_contract.functions.setSponsored(self.genesis_addr3).transact().executed()

Expand All @@ -157,7 +158,7 @@ def run_task_group(self, customized_enactor):
assert_equal(len(receipt["storageReleased"]), 0)

self.internal_contract("SponsorWhitelistControl").functions.setSponsorForCollateral(another_contract.address).transact({
"value": 1000 * 10 ** 18
"value": CFX(1000)
}).executed()
another_contract.functions.setSponsored(self.genesis_addr4).transact().executed()

Expand Down
3 changes: 2 additions & 1 deletion tests/sponsored_tx_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
from cfx_utils import CFX
from conflux.transactions import CONTRACT_DEFAULT_GAS, COLLATERAL_UNIT_IN_DRIP, charged_of_huge_gas
from test_framework.test_framework import ConfluxTestFramework
from test_framework.mininode import *
Expand Down Expand Up @@ -47,7 +48,7 @@ def run_test(self):
# sponsor the contract succeed
b0 = client.get_balance(genesis_addr)
control_contract.functions.setSponsorForGas(contract_addr, upper_bound).transact({
"value": 10 ** 18,
"value": CFX(1),
"gas": gas,
}).executed()
assert_equal(client.get_sponsor_balance_for_gas(contract_addr), 10 ** 18)
Expand Down

0 comments on commit 54b7d7f

Please sign in to comment.