Add balance threshold in checker

This commit is contained in:
lash 2023-02-04 06:13:20 +00:00
parent cefdc5c00b
commit 621077aa0f
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 109 additions and 22 deletions

File diff suppressed because one or more lines are too long

View File

@ -1 +1 @@
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"PeriodChange","type":"event"},{"inputs":[{"internalType":"address","name":"_subject","type":"address"}],"name":"check","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"lastUsed","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"period","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_subject","type":"address"}],"name":"poke","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"poker","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_period","type":"uint256"}],"name":"setPeriod","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_poker","type":"address"}],"name":"setPoker","outputs":[],"stateMutability":"nonpayable","type":"function"}]
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"PeriodChange","type":"event"},{"inputs":[],"name":"balanceThreshold","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_subject","type":"address"}],"name":"check","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"lastUsed","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"period","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_subject","type":"address"}],"name":"poke","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"poker","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_threshold","type":"uint256"}],"name":"setBalanceThreshold","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_period","type":"uint256"}],"name":"setPeriod","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_poker","type":"address"}],"name":"setPoker","outputs":[],"stateMutability":"nonpayable","type":"function"}]

View File

@ -92,7 +92,7 @@ class TestFaucet(EthTesterCase):
self.assertEqual(int(r, 16), prebalance + 1000)
def test_basic(self):
def test_basic_fund(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
@ -130,5 +130,15 @@ class TestFaucet(EthTesterCase):
self.assertEqual(int(r, 16), prebalance - cost + 1000)
def test_basic_self(self):
nonce_oracle = RPCNonceOracle(self.accounts[2], self.conn)
c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash_hex, o) = c.gimme(self.address, self.accounts[2])
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
if __name__ == '__main__':
unittest.main()

View File

@ -77,6 +77,39 @@ class TestFaucetPeriod(EthTesterCase):
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
c = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
enc = ABIContractEncoder()
enc.method('setPoker')
enc.typ(ABIContractType.ADDRESS)
enc.address(self.address)
data = enc.get()
tx = c.template(self.accounts[0], self.period_store_address, use_nonce=True)
tx = c.set_code(tx, data)
(tx_hash_hex, o) = c.finalize(tx)
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
def test_period_basic(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[1])
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
nonce_oracle = RPCNonceOracle(self.accounts[2], self.conn)
c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash_hex, o) = c.gimme(self.address, self.accounts[2])
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
def test_period(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
@ -94,12 +127,63 @@ class TestFaucetPeriod(EthTesterCase):
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2])
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
(tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2])
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 0)
self.backend.time_travel(self.start_time + 6000)
(tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2])
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
def test_balance(self):
o = balance(self.accounts[2])
r = self.conn.do(o)
prebalance = int(r, 16)
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
c = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
enc = ABIContractEncoder()
enc.method('setPoker')
enc.typ(ABIContractType.ADDRESS)
enc.address(self.address)
enc.method('setBalanceThreshold')
enc.typ(ABIContractType.UINT256)
enc.uint256(prebalance + 1)
data = enc.get()
tx = c.template(self.accounts[0], self.period_store_address, use_nonce=True)
tx = c.set_code(tx, data)
(tx_hash_hex, o) = c.finalize(tx)
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
nonce_oracle = RPCNonceOracle(self.accounts[2], self.conn)
c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash_hex, o) = c.gimme(self.address, self.accounts[2])
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 0)
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
c = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
enc = ABIContractEncoder()
enc.method('setBalanceThreshold')
enc.typ(ABIContractType.UINT256)
enc.uint256(1)
data = enc.get()
tx = c.template(self.accounts[0], self.period_store_address, use_nonce=True)
tx = c.set_code(tx, data)
@ -109,23 +193,9 @@ class TestFaucetPeriod(EthTesterCase):
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
nonce_oracle = RPCNonceOracle(self.accounts[2], self.conn)
c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2])
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
r = (tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2])
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 0)
self.backend.time_travel(self.start_time + 6000)
r = (tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2])
(tx_hash_hex, o) = c.gimme(self.address, self.accounts[2])
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)

View File

@ -7,6 +7,7 @@ contract PeriodSimple {
address public owner;
address public poker;
uint256 public period;
uint256 public balanceThreshold;
mapping (address => uint256) public lastUsed;
event PeriodChange(uint256 _value);
@ -26,7 +27,13 @@ contract PeriodSimple {
poker = _poker;
}
function setBalanceThreshold(uint256 _threshold) public {
require(msg.sender == owner);
balanceThreshold = _threshold;
}
function check(address _subject) public view returns(bool) {
require(_subject.balance >= balanceThreshold);
if (lastUsed[_subject] == 0) {
return true;
}