def Query(self): """ Query the smart contract for its token information (name, symbol, decimals). Args: wallet (neo.Wallets.Wallet): a wallet instance. Returns: None: if the NEP5Token instance `Name` is already set. True: if all information was retrieved. False: if information retrieval failed. """ if self.name is not None: # don't query twice return sb = ScriptBuilder() sb.EmitAppCallWithOperation(self.ScriptHash, 'name') sb.EmitAppCallWithOperation(self.ScriptHash, 'symbol') sb.EmitAppCallWithOperation(self.ScriptHash, 'decimals') engine = ApplicationEngine.Run(sb.ToArray()) results = engine.EvaluationStack.Items try: self.name = results[0].GetString() self.symbol = results[1].GetString() self.decimals = results[2].GetBigInteger() if len(self.name) > 1 and self.name != 'Stack Item' \ and len(self.symbol) > 1 and self.symbol != 'Stack Item'\ and self.decimals < 10: return True except Exception as e: logger.info("could not query token %s " % e) return False
def get_invoke_result(self, script, container=None): appengine = ApplicationEngine.Run(script=script, container=container) return { "script": script.decode('utf-8'), "state": VMStateStr(appengine.State), "gas_consumed": appengine.GasConsumed().ToString(), "stack": [ContractParameter.ToParameter(item).ToJson() for item in appengine.EvaluationStack.Items] }
def get_invoke_result(self, script): appengine = ApplicationEngine.Run(script=script) return { "script": script.hex(), "state": appengine.State, "gas_consumed": appengine.GasConsumed().ToString(), "stack": [ContractParameter.ToParameter(item).ToJson() for item in appengine.EvaluationStack.Items] }
def get_invoke_result(self, script): snapshot = GetBlockchain()._db.createSnapshot() appengine = ApplicationEngine.Run(snapshot, script=script) return { "script": script.decode('utf-8'), "state": VMStateStr(appengine.State), "gas_consumed": appengine.GasConsumed().ToString(), "stack": [ContractParameter.ToParameter(item).ToJson() for item in appengine.ResultStack.Items] }
def Query(self): """ Query the smart contract for its token information (name, symbol, decimals). Args: wallet (neo.Wallets.Wallet): a wallet instance. Returns: None: if the NEP5Token instance `Name` is already set. True: if all information was retrieved. False: if information retrieval failed. """ if self.name is not None: # don't query twice return sb = ScriptBuilder() sb.EmitAppCallWithOperation(self.ScriptHash, 'name') sb.EmitAppCallWithOperation(self.ScriptHash, 'symbol') sb.EmitAppCallWithOperation(self.ScriptHash, 'decimals') snapshot = GetBlockchain().Default()._db.createSnapshot().Clone() engine = None try: engine = ApplicationEngine.Run(snapshot, sb.ToArray(), exit_on_error=True, gas=Fixed8.FromDecimal(10.0), test_mode=False) except Exception as e: traceback.print_exc() pass if engine and len(engine.ResultStack.Items) == 3: results = engine.ResultStack.Items try: self.name = results[0].GetString() self.symbol = results[1].GetString() self.decimals = results[2].GetBigInteger() if len(self.name) > 1 and self.name != 'Stack Item' \ and len(self.symbol) > 1 and self.symbol != 'Stack Item' \ and self.decimals < 10: return True except Exception as e: pass return False
def get_invoke_result_balance(self, script): appengine = ApplicationEngine.Run(script=script) val = appengine.EvaluationStack.Items[0].GetBigInteger() balance = Decimal(val) return { "script": script.decode('utf-8'), "state": VMStateStr(appengine.State), "gas_consumed": appengine.GasConsumed().ToString(), "stack": [ ContractParameter.ToParameter(item).ToJson() for item in appengine.EvaluationStack.Items ], "balance": str(balance) }
def MonkeyPatchPersist(self, block, snapshot=None): if snapshot is None: snapshot = self._db.createSnapshot() snapshot.PersistingBlock = block amount_sysfee = self.GetSysFeeAmount( block.PrevHash) + (block.TotalFees().value / Fixed8.D) amount_sysfee_bytes = struct.pack("<d", amount_sysfee) with self._db.getBatch() as wb: for tx in block.Transactions: unspentcoinstate = UnspentCoinState.FromTXOutputsConfirmed( tx.outputs) snapshot.UnspentCoins.Add(tx.Hash.ToBytes(), unspentcoinstate) # go through all the accounts in the tx outputs for output in tx.outputs: account = snapshot.Accounts.GetAndChange( output.AddressBytes, lambda: AccountState(output.ScriptHash)) if account.HasBalance(output.AssetId): account.AddToBalance(output.AssetId, output.Value) else: account.SetBalanceFor(output.AssetId, output.Value) # go through all tx inputs unique_tx_input_hashes = [] for input in tx.inputs: if input.PrevHash not in unique_tx_input_hashes: unique_tx_input_hashes.append(input.PrevHash) for txhash in unique_tx_input_hashes: prevTx, height = self.GetTransaction(txhash.ToBytes()) coin_refs_by_hash = [ coinref for coinref in tx.inputs if coinref.PrevHash.ToBytes() == txhash.ToBytes() ] for input in coin_refs_by_hash: snapshot.UnspentCoins.GetAndChange( input.PrevHash.ToBytes()).Items[ input.PrevIndex] |= CoinState.Spent if prevTx.outputs[input.PrevIndex].AssetId.ToBytes( ) == Blockchain.SystemShare().Hash.ToBytes(): sc = snapshot.SpentCoins.GetAndChange( input.PrevHash.ToBytes(), lambda: SpentCoinState(input.PrevHash, height, [])) sc.Items.append( SpentCoinItem(input.PrevIndex, block.Index)) output = prevTx.outputs[input.PrevIndex] acct = snapshot.Accounts.GetAndChange( prevTx.outputs[input.PrevIndex].AddressBytes, lambda: AccountState(output.ScriptHash)) assetid = prevTx.outputs[input.PrevIndex].AssetId acct.SubtractFromBalance( assetid, prevTx.outputs[input.PrevIndex].Value) # do a whole lotta stuff with tx here... if tx.Type == TransactionType.RegisterTransaction: asset = AssetState(tx.Hash, tx.AssetType, tx.Name, tx.Amount, Fixed8(0), tx.Precision, Fixed8(0), Fixed8(0), UInt160(data=bytearray(20)), tx.Owner, tx.Admin, tx.Admin, block.Index + 2 * 2000000, False) snapshot.Assets.Add(tx.Hash.ToBytes(), asset) elif tx.Type == TransactionType.IssueTransaction: txresults = [ result for result in tx.GetTransactionResults() if result.Amount.value < 0 ] for result in txresults: asset = snapshot.Assets.GetAndChange( result.AssetId.ToBytes()) asset.Available = asset.Available - result.Amount elif tx.Type == TransactionType.ClaimTransaction: for input in tx.Claims: sc = snapshot.SpentCoins.TryGet(input.PrevHash.ToBytes()) if sc and sc.HasIndex(input.PrevIndex): sc.DeleteIndex(input.PrevIndex) snapshot.SpentCoins.GetAndChange( input.PrevHash.ToBytes()) elif tx.Type == TransactionType.EnrollmentTransaction: snapshot.Validators.GetAndChange( tx.PublicKey.ToBytes(), lambda: ValidatorState(pub_key=tx.PublicKey)) # logger.info("VALIDATOR %s " % validator.ToJson()) elif tx.Type == TransactionType.StateTransaction: # @TODO Implement persistence for State Descriptors pass elif tx.Type == TransactionType.PublishTransaction: def create_contract_state(): return ContractState(tx.Code, tx.NeedStorage, tx.Name, tx.CodeVersion, tx.Author, tx.Email, tx.Description) snapshot.Contracts.GetAndChange(tx.Code.ScriptHash().ToBytes(), create_contract_state) elif tx.Type == TransactionType.InvocationTransaction: return ApplicationEngine.Run(TriggerType.Application, tx, snapshot.Clone(), tx.Gas, True, wb)