示例#1
0
    def send_transaction(
        self, txn: Transaction, *signers: Account, opts: types.TxOpts = types.TxOpts()
    ) -> types.RPCResponse:
        """Send a transaction.

        :param txn: Transaction object.
        :param signers: Signers to sign the transaction.
        :param opts: (optional) Transaction options.

        >>> from solana.account import Account
        >>> from solana.system_program import TransferParams, transfer
        >>> from solana.transaction import Transaction
        >>> sender, reciever = Account(1), Account(2)
        >>> txn = Transaction().add(transfer(TransferParams(
        ...     from_pubkey=sender.public_key(), to_pubkey=reciever.public_key(), lamports=1000)))
        >>> solana_client = Client("http://localhost:8899")
        >>> solana_client.send_transaction(txn, sender) # doctest: +SKIP
        {'jsonrpc': '2.0',
         'result': '236zSA5w4NaVuLXXHK1mqiBuBxkNBu84X6cfLBh1v6zjPrLfyECz4zdedofBaZFhs4gdwzSmij9VkaSo2tR5LTgG',
         'id': 12}
        """
        try:
            # TODO: Cache recent blockhash
            blockhash_resp = self.get_recent_blockhash()
            if not blockhash_resp["result"]:
                raise RuntimeError("failed to get recent blockhash")
            txn.recent_blockhash = Blockhash(blockhash_resp["result"]["value"]["blockhash"])
        except Exception as err:
            raise RuntimeError("failed to get recent blockhash") from err

        txn.sign(*signers)
        return self.send_raw_transaction(txn.serialize(), opts=opts)
示例#2
0
    def send_transaction(self, txn: Transaction,
                         *signers: Account) -> RPCResponse:
        """Send a transaction.

        :param txn: Transaction object.
        :param signers: Signers to sign the transaction

        >>> from solana.account import Account
        >>> from solana.system_program import TransferParams, transfer
        >>> sender, reciever = Account(1), Account(2)
        >>> tx = transfer(TransferParams(
        ...     from_pubkey=sender.public_key(), to_pubkey=reciever.public_key(), lamports=1000))
        >>> solana_client = Client("http://localhost:8899")
        >>> solana_client.send_transaction(tx, sender) # doctest: +SKIP
        {'jsonrpc': '2.0',
         'result': '236zSA5w4NaVuLXXHK1mqiBuBxkNBu84X6cfLBh1v6zjPrLfyECz4zdedofBaZFhs4gdwzSmij9VkaSo2tR5LTgG',
         'id': 12}
        """
        try:
            # TODO: Cache recent blockhash
            blockhash_resp = self.get_recent_blockhash()
            if not blockhash_resp["result"]:
                raise RuntimeError("failed to get recent blockhash")
            txn.recent_blockhash = Blockhash(
                blockhash_resp["result"]["value"]["blockhash"])
        except Exception as err:
            raise RuntimeError("failed to get recent blockhash") from err

        txn.sign(*signers)
        wire_format = b58encode(txn.serialize()).decode("utf-8")
        return self._provider.make_request(RPCMethod("sendTransaction"),
                                           wire_format)