def _transfer_args( self, source: PublicKey, dest: PublicKey, owner: Union[Keypair, PublicKey], amount: int, multi_signers: Optional[List[Keypair]], opts: TxOpts, ) -> Tuple[Transaction, List[Keypair], TxOpts]: if isinstance(owner, Keypair): owner_pubkey = owner.public_key signers = [owner] else: owner_pubkey = owner signers = multi_signers if multi_signers else [] txn = Transaction().add( spl_token.transfer( spl_token.TransferParams( program_id=self.program_id, source=source, dest=dest, owner=owner_pubkey, amount=amount, signers=[signer.public_key for signer in signers], ) ) ) return txn, signers, opts
def test_transfer(stubbed_reciever, stubbed_sender): """Test transfer.""" params = spl_token.TransferParams( program_id=TOKEN_PROGRAM_ID, source=stubbed_sender.public_key(), dest=stubbed_reciever, owner=stubbed_sender.public_key(), amount=123, ) instruction = spl_token.transfer(params) assert spl_token.decode_transfer(instruction) == params multisig_params = spl_token.TransferParams( program_id=TOKEN_PROGRAM_ID, source=stubbed_sender.public_key(), dest=stubbed_reciever, owner=stubbed_sender.public_key(), signers=[PublicKey(i + 1) for i in range(3)], amount=123, ) instruction = spl_token.transfer(multisig_params) assert spl_token.decode_transfer(instruction) == multisig_params
def transfer( self, source: PublicKey, dest: PublicKey, owner: Union[Account, PublicKey], amount: int, multi_signers: Optional[List[Account]] = None, opts: TxOpts = TxOpts(), ) -> RPCResponse: """Transfer tokens to another account. :param source: Public key of account to transfer tokens from. :param dest: Public key of account to transfer tokens to. :param owner: Owner of the source account. :param amount: Number of tokens to transfer. :param multi_signers: (optional) Signing accounts if `owner` is a multiSig. :param opts: (optional) Transaction options. """ if isinstance(owner, Account): owner_pubkey = owner.public_key() signers = [owner] else: owner_pubkey = owner signers = multi_signers if multi_signers else [] txn = Transaction().add( spl_token.transfer( spl_token.TransferParams( program_id=self.program_id, source=source, dest=dest, owner=owner_pubkey, amount=amount, signers=[signer.public_key() for signer in signers], ) ) ) return self._conn.send_transaction(txn, *signers, opts=opts)