Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

High Volume GAS Claim Improvement #419

Merged
merged 5 commits into from
May 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to this project are documented in this file.
- fix ``StateMachine.Contract_Migrate`` and add tests
- add ability to attach tx attrs to build command and testinvoke. altered tx attr parsing
- updated the install instructions present on ``docs``
- added support for optionally chunking through GAS claims in prompt `#419 <https://github.com/CityOfZion/neo-python/issues/419>`_
- support RPC and REST endpoints in parallel `#420 <https://github.com/CityOfZion/neo-python/issues/420>`_
- Added new command ``tkn_history`` to the prompt. It shows the recorded history of transfers of a given NEP5 token, that are related to the open wallet.
- fix current block lookup during smart contract event processing `#426 <https://github.com/CityOfZion/neo-python/issues/426>`_
Expand Down
41 changes: 31 additions & 10 deletions neo/Prompt/Commands/Wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
from neo.Core.TX.TransactionAttribute import TransactionAttribute, TransactionAttributeUsage
from neo.SmartContract.ContractParameterContext import ContractParametersContext
from neo.Network.NodeLeader import NodeLeader
from neo.Prompt.Utils import get_asset_id, get_from_addr
from neo.Prompt.Utils import get_asset_id, get_from_addr, get_arg
from neocore.Fixed8 import Fixed8
from neocore.UInt160 import UInt160
from prompt_toolkit import prompt
import binascii
import json
import math


def DeleteAddress(prompter, wallet, addr):
Expand Down Expand Up @@ -107,13 +108,33 @@ def AddAlias(wallet, addr, title):

def ClaimGas(wallet, require_password=True, args=None):

if args:
params, from_addr_str = get_from_addr(args)
else:
params = None
from_addr_str = None

unclaimed_coins = wallet.GetUnclaimedCoins()
unclaimed_coin_refs = [coin.Reference for coin in unclaimed_coins]

if len(unclaimed_coin_refs) == 0:
unclaimed_count = len(unclaimed_coins)
if unclaimed_count == 0:
print("no claims to process")
return False

max_coins_per_claim = None
if params:
max_coins_per_claim = get_arg(params, 0, convert_to_int=True)
if not max_coins_per_claim:
print("max_coins_to_claim must be an integer")
return False
if max_coins_per_claim <= 0:
print("max_coins_to_claim must be greater than zero")
return False
if max_coins_per_claim and unclaimed_count > max_coins_per_claim:
unclaimed_coins = unclaimed_coins[:max_coins_per_claim]

unclaimed_coin_refs = [coin.Reference for coin in unclaimed_coins]

available_bonus = Blockchain.Default().CalculateBonusIgnoreClaimed(unclaimed_coin_refs)

if available_bonus == Fixed8.Zero():
Expand All @@ -129,13 +150,11 @@ def ClaimGas(wallet, require_password=True, args=None):

# the following can be used to claim gas that is in an imported contract_addr
# example, wallet claim --from-addr={smart contract addr}
if args:
params, from_addr_str = get_from_addr(args)
if from_addr_str:
script_hash = wallet.ToScriptHash(from_addr_str)
standard_contract = wallet.GetStandardAddress()
claim_tx.Attributes = [TransactionAttribute(usage=TransactionAttributeUsage.Script,
data=standard_contract.Data)]
if from_addr_str:
script_hash = wallet.ToScriptHash(from_addr_str)
standard_contract = wallet.GetStandardAddress()
claim_tx.Attributes = [TransactionAttribute(usage=TransactionAttributeUsage.Script,
data=standard_contract.Data)]

claim_tx.outputs = [
TransactionOutput(AssetId=Blockchain.SystemCoin().Hash, Value=available_bonus, script_hash=script_hash)
Expand All @@ -146,6 +165,8 @@ def ClaimGas(wallet, require_password=True, args=None):

print("\n---------------------------------------------------------------")
print("Will make claim for %s GAS" % available_bonus.ToString())
if max_coins_per_claim and unclaimed_count > max_coins_per_claim:
print("NOTE: You are claiming GAS on %s unclaimed coins. %s additional claim transactions will be required to claim all available GAS." % (max_coins_per_claim, math.floor(unclaimed_count / max_coins_per_claim)))
print("------------------------------------------------------------------\n")

if require_password:
Expand Down
9 changes: 9 additions & 0 deletions neo/Prompt/Commands/tests/test_claim_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,19 @@ def test_4_wallet_claim_ok(self):

wallet = self.GetWallet3()

claim = ClaimGas(wallet, require_password=False, args=['1'])

self.assertTrue(claim)

def test_5_wallet_claim_ok(self):

wallet = self.GetWallet3()

claim = ClaimGas(wallet, require_password=False)

self.assertTrue(claim)


def test_block_150000_sysfee(self):

fee = Blockchain.Default().GetSysFeeAmountByHeight(150000)
Expand Down
2 changes: 1 addition & 1 deletion neo/bin/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class PromptInterface:
'open wallet {path}',
'create wallet {path}',
'wallet {verbose}',
'wallet claim',
'wallet claim (max_coins_to_claim)',
'wallet migrate',
'wallet rebuild {start block}',
'wallet delete_addr {addr}',
Expand Down