def test_transfer_ERC20_USDT(): #rinkeby 合约地址: 0x0f38e3426de0f7afdf7e641633b287f462f346f2 erc20_data = make_data( to='0xC4d2e23807d176441221248fCbC03170e40B37d1', #代币接收地址 value=Decimal('1000001.123456') * (10**6)) print("erc20_data:{}".format(erc20_data)) tx = Transaction( nonce=420, gasprice=20 * (10**9), startgas=210000, to='0xeca059f3d6de135e520e789cdfeecbf5ceca3770', #合约地址 value=0, # data=b'' data=unhexlify(erc20_data)) tx.sender = '0x954d1a58c7abd4ac8ebe05f59191Cf718eb0cB89' #源地址 signed_tx = tx.sign( key= 'DBBAD2A5682517E4FF095F948F721563231282CA4179AE0DFEA1C76143BA9607', #源地址的私钥 network_id=None) # 4:Rinkeby rlp_data = rlp.encode(rlp.infer_sedes(signed_tx).serialize(signed_tx)) print(hexlify(rlp_data)) #交易数据 pass
def test_call(block, sender, to, data='', gasprice=0, value=0): state_root_before = block.state_root assert block.has_parent() # rebuild block state before finalization parent = block.get_parent() test_block = block.init_from_parent(parent, block.coinbase, timestamp=block.timestamp) for _tx in block.get_transactions(): success, output = processblock.apply_transaction(test_block, _tx) assert success # apply transaction startgas = block.gas_limit - block.gas_used gasprice = 0 nonce = test_block.get_nonce(sender) tx = Transaction(nonce, gasprice, startgas, to, value, data) tx.sender = sender try: success, output = processblock.apply_transaction(test_block, tx) except processblock.InvalidTransaction as e: success = False assert block.state_root == state_root_before if success: return output else: log.debug('test_call failed', error=e) return None
def call(this, to, value=0, data='', sender=None, startgas=25000, gasprice=10*denoms.szabo): sender = address20(sender or this.coinbase) to = address20(to) block = this.head_candidate state_root_before = block.state_root assert block.has_parent() # rebuild block state before finalization parent = block.get_parent() test_block = block.init_from_parent(parent, block.coinbase, timestamp=block.timestamp) for tx in block.get_transactions(): success, output = processblock.apply_transaction(test_block, tx) assert success # apply transaction nonce = test_block.get_nonce(sender) tx = Transaction(nonce, gasprice, startgas, to, value, data) tx.sender = sender try: success, output = processblock.apply_transaction(test_block, tx) except processblock.InvalidTransaction as e: success = False assert block.state_root == state_root_before if success: return output else: return False
def call(this, to, value=0, data='', sender=None, startgas=25000, gasprice=60 * denoms.shannon): sender = normalize_address(sender or this.coinbase) to = normalize_address(to, allow_blank=True) block = this.head_candidate state_root_before = block.state_root assert block.has_parent() # rebuild block state before finalization parent = block.get_parent() test_block = block.init_from_parent(parent, block.coinbase, timestamp=block.timestamp) for tx in block.get_transactions(): success, output = processblock.apply_transaction(test_block, tx) assert success # apply transaction nonce = test_block.get_nonce(sender) tx = Transaction(nonce, gasprice, startgas, to, value, data) tx.sender = sender try: success, output = processblock.apply_transaction(test_block, tx) except InvalidTransaction: success = False assert block.state_root == state_root_before if success: return output else: return False
def main3(): erc20_data = make_data(to='0x401bf316182c792048c75e52ce18cb12ec3c4273', value=Decimal('65.20627410') * (10**18)) print("erc20_data:{}".format(erc20_data)) tx = Transaction( nonce=58, gasprice=10 * (10**9), startgas=210000, to='0xee8e2882e07f89685430c27e2f77636b08df3c81', value=1, # data=b'' data=erc20_data.decode('hex')) tx.sender = '0x56e5782c908f69bd46b7e77338349f961fbe55b1' # print(tx.hash) signed_tx = tx.sign( key='63C08FABC252B53B9471E520CE8199971DB8884B5B569CBBBD17BC714E6BB39F', network_id=Rinkeby) # 4:Rinkeby print(signed_tx.hash.encode('hex')) rlp_data = rlp.encode(rlp.infer_sedes(tx).serialize(tx)) print(rlp_data.encode('hex')) # print(signed_tx) pass
def main2(): erc20_data = make_data(to='0xdf88522B56B85d4F0Bb08a7494b97E017BC6CB31', value=Decimal('656340.20627410') * (10**18)) print("erc20_data:{}".format(erc20_data)) tx = Transaction( nonce=51, gasprice=10 * (10**9), startgas=210000, to='0x130fc2749d35fe026f32427f60dd3f2ecb6c2f33', value=1 * (10**16), # data=b'' data=erc20_data.decode('hex')) tx.sender = '0x56e5782c908f69bd46b7e77338349f961fbe55b1' # print(tx.hash) signed_tx = tx.sign( key='63C08FABC252B53B9471E520CE8199971DB8884B5B569CBBBD17BC714E6BB39F', network_id=Rinkeby) # 4:Rinkeby print(signed_tx.hash.encode('hex')) rlp_data = rlp.encode(rlp.infer_sedes(tx).serialize(tx)) print(rlp_data.encode('hex')) # print(signed_tx) pass
def main(): erc20_data = make_data(to='0x56e5782c908f69bd46b7e77338349f961fbe55b1', value=Decimal('2019.08281809') * (10**18)) print("erc20_data:{}".format(erc20_data)) tx = Transaction( nonce=120, gasprice=10 * (10**9), startgas=210000, to='0x130fc2749d35fe026f32427f60dd3f2ecb6c2f33', value=0, # data=b'' data=erc20_data.decode('hex')) tx.sender = '0x954d1a58c7abd4ac8ebe05f59191cf718eb0cb89' # print(tx.hash) signed_tx = tx.sign( key='DBBAD2A5682517E4FF095F948F721563231282CA4179AE0DFEA1C76143BA9607', network_id=Rinkeby) # 4:Rinkeby print(signed_tx.hash.encode('hex')) rlp_data = rlp.encode(rlp.infer_sedes(tx).serialize(tx)) print(rlp_data.encode('hex')) # print(signed_tx) pass
def create_transaction(self, func_name, args, nonce_offset=0, value=0): data = self.create_transaction_data(func_name, args) nonce = self.web3.eth.getTransactionCount(self.caller_address, 'pending') + nonce_offset tx = Transaction(nonce, self.gas_price, self.gas_limit, self.address, value, data) # v = CHAIN_ID according to EIP 155. tx.v = self.web3.version.network tx.sender = decode_hex(self.caller_address) return tx
def call(self, data, block_id=None): block = self.json_rpc_server.get_block(block_id) state_root_before = block.state_root # rebuild block state before finalization if block.has_parent(): parent = block.get_parent() test_block = block.init_from_parent(parent, block.coinbase, timestamp=block.timestamp) for tx in block.get_transactions(): success, output = processblock.apply_transaction(test_block, tx) assert success else: original = block.snapshot() original['journal'] = deepcopy(original['journal']) # do not alter original journal test_block = ethereum.blocks.genesis(block.db) test_block.revert(original) # validate transaction if not isinstance(data, dict): raise BadRequestError('Transaction must be an object') to = address_decoder(data['to']) try: startgas = quantity_decoder(data['gas']) except KeyError: startgas = block.gas_limit - block.gas_used try: gasprice = quantity_decoder(data['gasPrice']) except KeyError: gasprice = 0 try: value = quantity_decoder(data['value']) except KeyError: value = 0 try: data_ = data_decoder(data['data']) except KeyError: data_ = b'' try: sender = address_decoder(data['from']) except KeyError: sender = '\x00' * 20 # apply transaction nonce = test_block.get_nonce(sender) tx = Transaction(nonce, gasprice, startgas, to, value, data_) tx.sender = sender try: success, output = processblock.apply_transaction(test_block, tx) except processblock.InvalidTransaction as e: success = False assert block.state_root == state_root_before if success: return output else: return False
def call(self, data, block_id=None): block = self.json_rpc_server.get_block(block_id) state_root_before = block.state_root # rebuild block state before finalization if block.has_parent(): parent = block.get_parent() test_block = block.init_from_parent(parent, block.coinbase, timestamp=block.timestamp) for tx in block.get_transactions(): success, output = processblock.apply_transaction(test_block, tx) assert success else: original = block.snapshot() original['journal'] = deepcopy(original['journal']) # do not alter original journal test_block = ethereum.blocks.genesis(block.db) test_block.revert(original) # validate transaction if not isinstance(data, dict): raise BadRequestError('Transaction must be an object') to = address_decoder(data['to']) try: startgas = quantity_decoder(data['gas']) except KeyError: startgas = block.gas_limit - block.gas_used try: gasprice = quantity_decoder(data['gasPrice']) except KeyError: gasprice = 0 try: value = quantity_decoder(data['value']) except KeyError: value = 0 try: data_ = data_decoder(data['data']) except KeyError: data_ = b'' try: sender = address_decoder(data['from']) except KeyError: sender = '\x00' * 20 # apply transaction nonce = test_block.get_nonce(sender) tx = Transaction(nonce, gasprice, startgas, to, value, data_) tx.sender = sender try: success, output = processblock.apply_transaction(test_block, tx) except processblock.InvalidTransaction: success = False assert block.state_root == state_root_before if success: return output else: return False
def create_transaction(web3: Web3, from_: str, to: str, data: bytes = b'', nonce_offset: int = 0, value: int = 0, gas_price: int = GAS_PRICE, gas_limit: int = POT_GAS_LIMIT) -> Transaction: nonce = web3.eth.getTransactionCount(from_, 'pending') + nonce_offset tx = Transaction(nonce, gas_price, gas_limit, to, value, data) tx.sender = decode_hex(from_) return tx
def create_transaction( web3: Web3, from_: str, to: str, data: bytes = b'', nonce_offset: int = 0, value: int = 0, gas_price: Union[int, None] = None, gas_limit: int = NETWORK_CFG.POT_GAS_LIMIT) -> Transaction: if gas_price is None: gas_price = NETWORK_CFG.GAS_PRICE nonce = web3.eth.getTransactionCount(from_, 'pending') + nonce_offset tx = Transaction(nonce, gas_price, gas_limit, to, value, data) tx.sender = decode_hex(from_) return tx
def create_transaction( web3: Web3, from_: str, to: str, data: bytes = b'', nonce_offset: int = 0, value: int = 0, gas_price: Union[int, None] = None, gas_limit: int = NETWORK_CFG.POT_GAS_LIMIT ) -> Transaction: if gas_price is None: gas_price = NETWORK_CFG.GAS_PRICE nonce = web3.eth.getTransactionCount(from_, 'pending') + nonce_offset tx = Transaction(nonce, gas_price, gas_limit, to, value, data) tx.sender = decode_hex(from_) return tx
def call(self, data, block_id=None): block = self.json_rpc_server.get_block(block_id) # rebuild block state before finalization parent = block.get_parent() test_block = block.init_from_parent(parent, block.coinbase, timestamp=block.timestamp) for tx in block.get_transactions(): success, output = processblock.apply_transaction(test_block, tx) assert success # validate transaction if not isinstance(data, dict): raise BadRequestError('Transaction must be an object') to = address_decoder(data['to']) try: startgas = quantity_decoder(data['gas']) except KeyError: startgas = block.gas_limit - block.gas_used try: gasprice = quantity_decoder(data['gasPrice']) except KeyError: gasprice = 0 try: value = quantity_decoder(data['value']) except KeyError: value = 0 try: data_ = data_decoder(data['data']) except KeyError: data_ = b'' try: sender = address_decoder(data['from']) except KeyError: sender = '\x00' * 20 # initialize transaction nonce = block.get_nonce(sender) tx = Transaction(nonce, gasprice, startgas, to, value, data_) tx.sender = sender # apply transaction try: success, output = processblock.apply_transaction(test_block, tx) except processblock.InvalidTransaction: success = False if success: return output else: return False