The following question has been asked on Telegram:
Can u send tokens to other people’s wallets in smartpy for example keep a map of everyone that has purchased tokens the. send a percentage of tokens to them on another entry point?
The following question has been asked on Telegram:
Can u send tokens to other people’s wallets in smartpy for example keep a map of everyone that has purchased tokens the. send a percentage of tokens to them on another entry point?
You can use sp.send(<receiver>, <amount>)
to send tokens to wallets but pay attention to the sending to multiple addresses flaw described here: https://opentezos.com/smart-contracts/avoiding-flaws/#2-transferring-tez-in-a-call-that-should-benefits-others.
The best practice is to create a withdraw
entrypoint so anyone can withdraw its tokens.
For example:
import smartpy as sp
@sp.module
def main():
class MyContract(sp.Contract):
def __init__(self, receivers):
sp.cast(receivers, sp.big_map[sp.address, sp.mutez])
self.data.receivers = receivers
@sp.entrypoint
def withdraw(self):
amount = self.data.receivers.get(sp.sender, error="NotInReceivers")
sp.send(sp.sender, amount)
del self.data.receivers[sp.sender]
if "templates" not in __name__:
@sp.add_test(name="MyContract")
def test():
alice = sp.test_account("alice")
sc = sp.test_scenario(main)
receivers = sp.big_map({alice.address: sp.tez(5)})
c1 = main.MyContract(receivers)
c1.set_initial_balance(sp.tez(20))
sc += c1
c1.withdraw().run(sender=alice)