Пример #1
0
    def transfer(
        self,
        crypto: Crypto,
        destination_address: Address,
        amount: int,
        tx_fee: int,
        tx_nonce: str,
        chain_id: int = 1,
        **kwargs,
    ) -> Optional[str]:
        """
        Submit a transfer transaction to the ledger.

        :param crypto: the crypto object associated to the payer.
        :param destination_address: the destination address of the payee.
        :param amount: the amount of wealth to be transferred.
        :param tx_fee: the transaction fee.
        :param tx_nonce: verifies the authenticity of the tx
        :param chain_id: the Chain ID of the Ethereum transaction. Default is 1 (i.e. mainnet).
        :return: tx digest if present, otherwise None
        """
        tx_digest = None
        nonce = self._try_get_transaction_count(crypto.address)
        if nonce is None:
            return tx_digest

        transaction = {
            "nonce": nonce,
            "chainId": chain_id,
            "to": destination_address,
            "value": amount,
            "gas": tx_fee,
            "gasPrice": self._api.toWei(self._gas_price, GAS_ID),
            "data": tx_nonce,
        }

        gas_estimate = self._try_get_gas_estimate(transaction)
        if gas_estimate is None or tx_fee <= gas_estimate:  # pragma: no cover
            logger.warning(
                "Need to increase tx_fee in the configs to cover the gas consumption of the transaction. Estimated gas consumption is: {}.".format(
                    gas_estimate
                )
            )
            return tx_digest

        signed_transaction = crypto.sign_transaction(transaction)

        tx_digest = self.send_signed_transaction(tx_signed=signed_transaction,)

        return tx_digest
Пример #2
0
    def transfer(
        self,
        crypto: Crypto,
        destination_address: Address,
        amount: int,
        tx_fee: int,
        tx_nonce: str = "",
        denom: str = "testfet",
        account_number: int = 0,
        sequence: int = 0,
        gas: int = 80000,
        memo: str = "",
        sync_mode: str = "sync",
        chain_id: str = "aea-testnet",
        **kwargs,
    ) -> Optional[str]:
        """
        Submit a transfer transaction to the ledger.

        :param crypto: the crypto object associated to the payer.
        :param destination_address: the destination address of the payee.
        :param amount: the amount of wealth to be transferred.
        :param tx_fee: the transaction fee.
        :param tx_nonce: verifies the authenticity of the tx
        :param chain_id: the Chain ID of the Ethereum transaction. Default is 1 (i.e. mainnet).
        :return: tx digest if present, otherwise None
        """
        result = self._try_get_account_number_and_sequence(crypto.address)
        if result is not None:
            account_number, sequence = result
        transfer = {
            "type": "cosmos-sdk/MsgSend",
            "value": {
                "from_address": crypto.address,
                "to_address": destination_address,
                "amount": [{
                    "denom": denom,
                    "amount": str(amount)
                }],
            },
        }
        tx = {
            "account_number": str(account_number),
            "sequence": str(sequence),
            "chain_id": chain_id,
            "fee": {
                "gas": str(gas),
                "amount": [{
                    "denom": denom,
                    "amount": str(tx_fee)
                }],
            },
            "memo": memo,
            "msgs": [transfer],
        }
        signature = crypto.sign_transaction(tx)
        base64_pbk = base64.b64encode(bytes.fromhex(
            crypto.public_key)).decode("utf-8")
        pushable_tx = {
            "tx": {
                "msg": [transfer],
                "fee": {
                    "gas": str(gas),
                    "amount": [{
                        "denom": denom,
                        "amount": str(tx_fee)
                    }],
                },
                "memo":
                memo,
                "signatures": [{
                    "signature": signature,
                    "pub_key": {
                        "type": "tendermint/PubKeySecp256k1",
                        "value": base64_pbk,
                    },
                    "account_number": str(account_number),
                    "sequence": str(sequence),
                }],
            },
            "mode": sync_mode,
        }
        # TODO retrieve, gas dynamically
        tx_digest = self.send_signed_transaction(tx_signed=pushable_tx)
        return tx_digest