예제 #1
0
    def send_money(self, from_priv, amount_by_address):
        from_addr = self.get_address_of_private_key(from_priv)
        gas_price_wei = self.get_gas_price_wei()

        total_gas_cost_eth = GAS_LIMIT * eth_of_wei(gas_price_wei) * len(
            amount_by_address)
        total_required_eth = sum(
            amount_by_address.values()) + total_gas_cost_eth
        if self.get_balance(from_addr) < total_required_eth:
            raise NotEnoughMoney(from_addr)

        nonce = self.get_nonce(from_addr)

        tx_ids = []
        for i, (to_addr, amount) in enumerate(amount_by_address.items()):
            tx = {
                'from': from_addr,
                'to': to_addr,
                'value': wei_of_eth(amount),
                'nonce': hex(nonce + i),
                'gasPrice': hex(gas_price_wei),
                'gas': hex(GAS_LIMIT),
            }
            signed_tx = Account.sign_transaction(
                tx, from_priv).rawTransaction.hex()
            tx_id = self.proxy.make_request('eth_sendRawTransaction',
                                            signed_tx)
            tx_ids.append(tx_id)

        return tx_ids
예제 #2
0
    def sendRawTransaction(self, to_address, contract_abi, fn_name, args=None,
                           bin_data=None, gasPrice=30000000,
                           packet_type=ChannelPack.TYPE_RPC):
        cmd = "sendRawTransaction"
        if to_address != "":
            common.check_and_format_address(to_address)
        # 第三个参数是方法的abi,可以传入None,encode_transaction_data做了修改,支持通过方法+参数在整个abi里找到对应的方法abi来编码

        if bin_data is None:
            functiondata = encode_transaction_data(fn_name, contract_abi, None, args)
        # the args is None
        elif args is None:
            functiondata = bin_data
        # deploy with params
        else:
            fn_data = get_aligned_function_data(contract_abi, None, args)
            functiondata = bin_data + fn_data[2:]

        if to_address is not None and len(to_address) > 0:
            from eth_utils import to_checksum_address
            to_address = to_checksum_address(to_address)

        # load default account if not set .notice: account only use for
        # sign transaction for sendRawTransaction
        if self.client_account is None:
            self.load_default_account()
        # 填写一个bcos transaction 的 mapping
        import random
        txmap = dict()
        txmap["randomid"] = random.randint(0, 1000000000)  # 测试用 todo:改为随机数
        txmap["gasPrice"] = gasPrice
        txmap["gasLimit"] = gasPrice
        txmap["blockLimit"] = self.getBlockLimit()  # 501  # 测试用,todo:从链上查一下

        txmap["to"] = to_address
        txmap["value"] = 0
        txmap["data"] = functiondata
        txmap["fiscoChainId"] = self.fiscoChainId
        txmap["groupId"] = self.groupid
        txmap["extraData"] = ""
        '''
        from datatypes.bcostransactions import (
            serializable_unsigned_transaction_from_dict,
        )
        # 将mapping构建一个transaction对象,非必要,用来对照的
        transaction = serializable_unsigned_transaction_from_dict(txmap)
        # 感受下transaction encode的原始数据
        print(encode_hex(rlp.encode(transaction)))
        '''

        # 实际上只需要用sign_transaction就可以获得rawTransaction的编码数据了,input :txmap,私钥
        signedTxResult = Account.sign_transaction(txmap, self.client_account.privateKey)
        # signedTxResult.rawTransaction是二进制的,要放到rpc接口里要encode下
        params = [self.groupid, encode_hex(signedTxResult.rawTransaction)]
        result = self.common_request(cmd, params, packet_type)
        return result
예제 #3
0
txmap["blockLimit"] = 501  # 测试用,todo:从链上查一下
txmap["to"] = contractAddress
txmap["value"] = 0
txmap["data"] = functiondata
txmap["fiscoChainId"] = 1
txmap["groupId"] = 1
txmap["extraData"] = ""
# txmap["chainId"]=None #chainId没用了,fiscoChainId有用
print(txmap)
# 将mapping构建一个transaction对象,非必要,用来对照的
transaction = serializable_unsigned_transaction_from_dict(txmap)
# 感受下transaction encode的原始数据
print(encode_hex(rlp.encode(transaction)))

# 实际上只需要用sign_transaction就可以获得rawTransaction的编码数据了,input :txmap,私钥
signedTxResult = Account.sign_transaction(txmap, ac2.privateKey)
print(signedTxResult)
# signedTxResult.rawTransaction是二进制的,要放到rpc接口里要encode下
print(encode_hex(signedTxResult.rawTransaction))

url = "http://119.23.46.126:8545"
rpc = utils.rpc.HTTPProvider(url)
if True:
    param = [1, encode_hex(signedTxResult.rawTransaction)]
    # 发送
    response = rpc.make_request("sendRawTransaction", param)
    print("deploy", response)
    txid = response['result']
    param = [1, txid]
    i = 0
    for i in range(0, 15):
예제 #4
0
def publish_evm_code(transaction, eos_pub_key=None):
    global g_chain_id
    global g_current_account
    global g_last_trx_ret
    global g_public_key

    #    transaction['chainId'] = chain_id #Ethereum mainnet
    #     print(transaction)

    sender = transaction['from']
    if sender[:2] == '0x':
        sender = sender[2:]
    sender = sender.lower()
    logger.info(sender)
    a = EthAccount('helloworld11', sender)
    logger.info(('+++++++++sender:', sender))
    nonce = a.get_nonce()
    assert nonce >= 0

    transaction['nonce'] = nonce
    transaction['gasPrice'] = 0
    transaction['gas'] = 20000000

    if sender in keys:
        priv_key = key_maps[sender]
        encoded_transaction = Account.sign_transaction(transaction, priv_key)
        encoded_transaction = encoded_transaction.rawTransaction.hex()[2:]
    elif g_public_key:
        transaction = dissoc(transaction, 'from')
        encoded_transaction = sign_transaction_dict_with_eos_key(
            transaction, g_chain_id, g_public_key)
        #        logger.info(encoded_transaction)
        encoded_transaction = encoded_transaction.hex()
    else:
        transaction = dissoc(transaction, 'from')
        unsigned_transaction = serializable_unsigned_transaction_from_dict(
            transaction)
        encoded_transaction = encode_transaction(unsigned_transaction,
                                                 vrs=(g_chain_id, 0, 0))
        encoded_transaction = encoded_transaction.hex()

    if g_current_account:
        account_name = g_current_account
    else:
        account_name = 'helloworld11'

    if g_contract_name:
        contract_name = g_contract_name
    else:
        contract_name = 'helloworld11'

    args = {'trx': encoded_transaction, 'sender': sender}
    ret = eosapi.push_action(contract_name, 'raw', args,
                             {account_name: 'active'})
    g_last_trx_ret = ret
    logs = ret['processed']['action_traces'][0]['console']
    # logger.info(logs)
    logger.info(('++++elapsed:', ret['processed']['elapsed']))
    try:
        logs = bytes.fromhex(logs)
        logs = rlp.decode(logs)
        # logger.info(logs)
    except Exception as e:
        logger.error(logs)
        raise e
    return logs