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

Commit

Permalink
adding ability to claim gas from SC address
Browse files Browse the repository at this point in the history
  • Loading branch information
localhuman committed Jan 13, 2018
1 parent f42599c commit 438ec89
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion neo/Blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def GetSystemShare():

def GetStateReader():
from neo.SmartContract.StateReader import StateReader
return StateReader.Instance()
return StateReader()


def GetConsensusAddress(validators):
Expand Down
12 changes: 10 additions & 2 deletions neo/Core/Helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from neocore.Fixed8 import Fixed8
from neo.SmartContract import TriggerType
from neo.Settings import settings
from neo.EventHub import events


class Helper(object):
Expand Down Expand Up @@ -152,7 +153,6 @@ def VerifyScripts(verifiable):
if len(hashes) != len(verifiable.Scripts):
return False

state_reader = GetStateReader()
blockchain = GetBlockchain()

for i in range(0, len(hashes)):
Expand All @@ -168,6 +168,7 @@ def VerifyScripts(verifiable):
if hashes[i] != verification_hash:
return False

state_reader = GetStateReader()
engine = ApplicationEngine(TriggerType.Verification, verifiable, blockchain, state_reader, Fixed8.Zero())
engine.LoadScript(verification, False)
invoction = verifiable.Scripts[i].InvocationScript
Expand All @@ -176,15 +177,22 @@ def VerifyScripts(verifiable):
try:
success = engine.Execute()
state_reader.ExecutionCompleted(engine, success)

except Exception as e:
state_reader.ExecutionCompleted(engine, False, e)

if engine.EvaluationStack.Count != 1 or not engine.EvaluationStack.Pop().GetBoolean():
Helper.EmitServiceEvents(state_reader)
return False

Helper.EmitServiceEvents(state_reader)

return True

@staticmethod
def IToBA(value):
return [1 if digit == '1' else 0 for digit in bin(value)[2:]]

@staticmethod
def EmitServiceEvents(state_reader):
for event in state_reader.events_to_dispatch:
events.emit(event.event_type, event)
26 changes: 21 additions & 5 deletions neo/Prompt/Commands/Wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from neo.Wallets.NEP5Token import NEP5Token
from neo.Core.TX.ClaimTransaction import ClaimTransaction
from neo.Core.TX.Transaction import TransactionOutput
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 string_from_fixed8, get_asset_id
from neo.Prompt.Utils import string_from_fixed8, get_asset_id, get_from_addr
from neocore.Fixed8 import Fixed8
from neocore.UInt160 import UInt160
from prompt_toolkit import prompt
import binascii
import json
Expand Down Expand Up @@ -110,7 +112,7 @@ def AddAlias(wallet, addr, title):
print(e)


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

unclaimed_coins = wallet.GetUnclaimedCoins()
unclaimed_coin_refs = [coin.Reference for coin in unclaimed_coins]
Expand All @@ -122,16 +124,28 @@ def ClaimGas(wallet, require_password=True):
available_bonus = Blockchain.Default().CalculateBonusIgnoreClaimed(unclaimed_coin_refs)

if available_bonus == Fixed8.Zero():

print("No gas to claim")
return False

claim_tx = ClaimTransaction()
claim_tx.Claims = unclaimed_coin_refs
claim_tx.Attributes = []
claim_tx.inputs = []

script_hash = wallet.GetChangeAddress()

# 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)]

claim_tx.outputs = [
TransactionOutput(AssetId=Blockchain.SystemCoin().Hash, Value=available_bonus, script_hash=wallet.GetChangeAddress())
TransactionOutput(AssetId=Blockchain.SystemCoin().Hash, Value=available_bonus, script_hash=script_hash)
]

context = ContractParametersContext(claim_tx)
Expand All @@ -153,12 +167,14 @@ def ClaimGas(wallet, require_password=True):
if context.Completed:

claim_tx.scripts = context.GetScripts()
wallet.SaveTransaction(claim_tx)

print("claim tx: %s " % json.dumps(claim_tx.ToJson(), indent=4))

relayed = NodeLeader.Instance().Relay(claim_tx)

if relayed:
print("Relayed Tx: %s " % claim_tx.Hash.ToString())
wallet.SaveTransaction(claim_tx)
else:

print("Could not relay tx %s " % claim_tx.Hash.ToString())
Expand Down
1 change: 0 additions & 1 deletion neo/SmartContract/ContractParameterContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ def GetScripts(self):
# logger.info("SCRIPT IS %s " % item.Script)

witness = Witness(
# invocation_script='40fdb984faf0a400b6894c1ce5b317cf894ba3eb89b899cefda2ac307b278418b943534ad298884f9200dc4b7e1dc244db16c62a44a830a860060ec11d3e6e9717',
invocation_script=sb.ToArray(),
verification_script=vscript
)
Expand Down
2 changes: 1 addition & 1 deletion prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def show_wallet(self, arguments):
elif item == 'close':
self.do_close_wallet()
elif item == 'claim':
ClaimGas(self.Wallet)
ClaimGas(self.Wallet, True, arguments[1:])
elif item == 'rebuild':
self.Wallet.Rebuild()
try:
Expand Down

0 comments on commit 438ec89

Please sign in to comment.