eth-faucet

Gas token gifter with controls from time intervals, amounts and access
Log | Files | Refs | README

commit 621077aa0f966f662ca570247c0535e8fe44c500
parent cefdc5c00b83e4018d0b0ccc7c78087d319925ee
Author: lash <dev@holbrook.no>
Date:   Sat,  4 Feb 2023 06:13:20 +0000

Add balance threshold in checker

Diffstat:
Mpython/eth_faucet/data/PeriodSimple.bin | 4++--
Mpython/eth_faucet/data/PeriodSimple.json | 2+-
Mpython/tests/test_basic.py | 12+++++++++++-
Mpython/tests/test_period.py | 90++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
Msolidity/PeriodSimple.sol | 7+++++++
5 files changed, 101 insertions(+), 14 deletions(-)

diff --git a/python/eth_faucet/data/PeriodSimple.bin b/python/eth_faucet/data/PeriodSimple.bin @@ -1 +1 @@ -608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060008054906101000a900473ffffffffffffffffffffffffffffffffffffffff16600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506108dc806100c16000396000f3fe608060405234801561001057600080fd5b50600436106100a5576000357c010000000000000000000000000000000000000000000000000000000090048063b1a997ac11610078578063b1a997ac14610132578063c23697a81461014e578063ef78d4fd1461017e578063f4430dd81461019c576100a5565b80630f3a9f65146100aa5780634b6bdf1d146100c65780634ee577b4146100e45780638da5cb5b14610114575b600080fd5b6100c460048036038101906100bf91906105ac565b6101b8565b005b6100ce610250565b6040516100db919061061a565b60405180910390f35b6100fe60048036038101906100f99190610661565b610276565b60405161010b919061069d565b60405180910390f35b61011c61028e565b604051610129919061061a565b60405180910390f35b61014c60048036038101906101479190610661565b6102b2565b005b61016860048036038101906101639190610661565b610427565b60405161017591906106d3565b60405180910390f35b6101866104cf565b604051610193919061069d565b60405180910390f35b6101b660048036038101906101b19190610661565b6104d5565b005b3373ffffffffffffffffffffffffffffffffffffffff1660008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1614610246576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161023d9061074b565b60405180910390fd5b8060028190555050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60036020528060005260406000206000915090505481565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806103595750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b610398576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161038f906107b7565b60405180910390fd5b6103a181610427565b6103e0576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016103d790610823565b60405180910390fd5b42600360008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555050565b600080600360008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020540361047857600190506104ca565b600254600360008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020546104c59190610872565b421190505b919050565b60025481565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161461052d57600080fd5b80600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b600080fd5b6000819050919050565b61058981610576565b811461059457600080fd5b50565b6000813590506105a681610580565b92915050565b6000602082840312156105c2576105c1610571565b5b60006105d084828501610597565b91505092915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610604826105d9565b9050919050565b610614816105f9565b82525050565b600060208201905061062f600083018461060b565b92915050565b61063e816105f9565b811461064957600080fd5b50565b60008135905061065b81610635565b92915050565b60006020828403121561067757610676610571565b5b60006106858482850161064c565b91505092915050565b61069781610576565b82525050565b60006020820190506106b2600083018461068e565b92915050565b60008115159050919050565b6106cd816106b8565b82525050565b60006020820190506106e860008301846106c4565b92915050565b600082825260208201905092915050565b7f4552525f4e4f545f4f574e455200000000000000000000000000000000000000600082015250565b6000610735600d836106ee565b9150610740826106ff565b602082019050919050565b6000602082019050818103600083015261076481610728565b9050919050565b7f4552525f41434345535300000000000000000000000000000000000000000000600082015250565b60006107a1600a836106ee565b91506107ac8261076b565b602082019050919050565b600060208201905081810360008301526107d081610794565b9050919050565b7f4552525f5052454d415455524500000000000000000000000000000000000000600082015250565b600061080d600d836106ee565b9150610818826107d7565b602082019050919050565b6000602082019050818103600083015261083c81610800565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600061087d82610576565b915061088883610576565b92508282019050808211156108a05761089f610843565b5b9291505056fea26469706673582212203ed5c0c86394159ef741670b0b28eb5726fd98c9f413fc65ae7b7e36eb7cbd7264736f6c63430008110033 -\ No newline at end of file +608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060008054906101000a900473ffffffffffffffffffffffffffffffffffffffff16600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506109bb806100c16000396000f3fe608060405234801561001057600080fd5b50600436106100bb576000357c010000000000000000000000000000000000000000000000000000000090048063b1a997ac11610083578063b1a997ac14610164578063c23697a814610180578063c3173774146101b0578063ef78d4fd146101ce578063f4430dd8146101ec576100bb565b80630f3a9f65146100c05780634b6bdf1d146100dc5780634ee577b4146100fa57806387020d6b1461012a5780638da5cb5b14610146575b600080fd5b6100da60048036038101906100d5919061068b565b610208565b005b6100e46102a0565b6040516100f191906106f9565b60405180910390f35b610114600480360381019061010f9190610740565b6102c6565b604051610121919061077c565b60405180910390f35b610144600480360381019061013f919061068b565b6102de565b005b61014e610340565b60405161015b91906106f9565b60405180910390f35b61017e60048036038101906101799190610740565b610364565b005b61019a60048036038101906101959190610740565b6104d9565b6040516101a791906107b2565b60405180910390f35b6101b86105a8565b6040516101c5919061077c565b60405180910390f35b6101d66105ae565b6040516101e3919061077c565b60405180910390f35b61020660048036038101906102019190610740565b6105b4565b005b3373ffffffffffffffffffffffffffffffffffffffff1660008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1614610296576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161028d9061082a565b60405180910390fd5b8060028190555050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60046020528060005260406000206000915090505481565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161461033657600080fd5b8060038190555050565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16148061040b5750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b61044a576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161044190610896565b60405180910390fd5b610453816104d9565b610492576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161048990610902565b60405180910390fd5b42600460008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555050565b60006003548273ffffffffffffffffffffffffffffffffffffffff1631101561050157600080fd5b6000600460008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020540361055157600190506105a3565b600254600460008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205461059e9190610951565b421190505b919050565b60035481565b60025481565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161461060c57600080fd5b80600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b600080fd5b6000819050919050565b61066881610655565b811461067357600080fd5b50565b6000813590506106858161065f565b92915050565b6000602082840312156106a1576106a0610650565b5b60006106af84828501610676565b91505092915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006106e3826106b8565b9050919050565b6106f3816106d8565b82525050565b600060208201905061070e60008301846106ea565b92915050565b61071d816106d8565b811461072857600080fd5b50565b60008135905061073a81610714565b92915050565b60006020828403121561075657610755610650565b5b60006107648482850161072b565b91505092915050565b61077681610655565b82525050565b6000602082019050610791600083018461076d565b92915050565b60008115159050919050565b6107ac81610797565b82525050565b60006020820190506107c760008301846107a3565b92915050565b600082825260208201905092915050565b7f4552525f4e4f545f4f574e455200000000000000000000000000000000000000600082015250565b6000610814600d836107cd565b915061081f826107de565b602082019050919050565b6000602082019050818103600083015261084381610807565b9050919050565b7f4552525f41434345535300000000000000000000000000000000000000000000600082015250565b6000610880600a836107cd565b915061088b8261084a565b602082019050919050565b600060208201905081810360008301526108af81610873565b9050919050565b7f4552525f5052454d415455524500000000000000000000000000000000000000600082015250565b60006108ec600d836107cd565b91506108f7826108b6565b602082019050919050565b6000602082019050818103600083015261091b816108df565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600061095c82610655565b915061096783610655565b925082820190508082111561097f5761097e610922565b5b9291505056fea2646970667358221220cea5f56dcc1ced45f330e44c5e38309512ae6f03042f084994943724ec31aaab64736f6c63430008110033 +\ No newline at end of file diff --git a/python/eth_faucet/data/PeriodSimple.json b/python/eth_faucet/data/PeriodSimple.json @@ -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"}] diff --git a/python/tests/test_basic.py b/python/tests/test_basic.py @@ -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() diff --git a/python/tests/test_period.py b/python/tests/test_period.py @@ -77,14 +77,12 @@ class TestFaucetPeriod(EthTesterCase): r = self.conn.do(o) self.assertEqual(r['status'], 1) - - def test_period(self): nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) c = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) enc = ABIContractEncoder() - enc.method('setPeriod') - enc.typ(ABIContractType.UINT256) - enc.uint256(100) + 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) @@ -94,12 +92,32 @@ class TestFaucetPeriod(EthTesterCase): 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) 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('setPeriod') + enc.typ(ABIContractType.UINT256) + enc.uint256(100) data = enc.get() tx = c.template(self.accounts[0], self.period_store_address, use_nonce=True) tx = c.set_code(tx, data) @@ -117,7 +135,7 @@ class TestFaucetPeriod(EthTesterCase): 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]) + (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) @@ -125,7 +143,59 @@ class TestFaucetPeriod(EthTesterCase): 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.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('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) + (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) diff --git a/solidity/PeriodSimple.sol b/solidity/PeriodSimple.sol @@ -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; }