Transfering FA2 between two users

The following question has been asked on Telegram:

I want to create a simple token transfer between two wallets.

1 Like

You can use the transfer entrypoint.

my_fa2.transfer([
    sp.record(
        from_=alice.address,
        txs=[sp.record(token_id=0, amount=1, to_=bob.address)]
)])

A more detailed example:

import smartpy as sp
from templates import fa2_lib as fa2

# Illustratrive purpose only

t, main = fa2.t, fa2.main


@sp.module
def m():
    class Fa2_transferer(sp.Contract):
        @sp.entrypoint
        def transfer_fa2(self, fa2, from_, to_, amount, token_id):
            contract = sp.contract(t.transfer_params, fa2, entrypoint="transfer")
            param = [
                sp.record(
                    from_=from_,
                    txs=[sp.record(to_=to_, amount=amount, token_id=token_id)],
                )
            ]
            sp.transfer(param, sp.tez(0), contract.unwrap_some())


@sp.add_test(name="FA2 transfer")
def test():
    alice = sp.test_account("alice")
    bob = sp.test_account("bob")
    sc = sp.test_scenario([t, main, m])
    sc.h2("Origination")
    fa2 = main.SingleAsset(
        metadata=sp.big_map(), ledger={alice.address: 42}, token_metadata=sp.map()
    )
    sc += fa2
    c1 = m.Fa2_transferer()
    sc += c1
    sc.h2("Alice set transferer as operator")
    fa2.update_operators(
        [
            sp.variant(
                "add_operator",
                sp.record(owner=alice.address, operator=c1.address, token_id=0),
            )
        ]
    ).run(sender=alice)
    sc.h2("Someone ask transfer from alice to bob")
    c1.transfer_fa2(
        fa2=fa2.address, from_=alice.address, to_=alice.address, amount=12, token_id=0
    )

1 Like