The following question has been asked on Telegram:
Does anybody have an example SmartPy script to create a new token and Mint it to a wallet address?
The following question has been asked on Telegram:
Does anybody have an example SmartPy script to create a new token and Mint it to a wallet address?
Here is an example of an fungible token contract that uses the FA2 library.
import smartpy as sp
from templates import fa2_lib as fa2
main = fa2.main
@sp.module
def m():
# Order of inheritance: [Admin], [<policy>], <base class>, [<mixins>]
class MyFungible(
main.Admin,
main.Fungible,
main.MintFungible,
):
def __init__(self, administrator, metadata, ledger, token_metadata):
main.MintFungible.__init__(self)
main.Fungible.__init__(self, metadata, ledger, token_metadata)
main.Admin.__init__(self, administrator)
@sp.add_test(name="NFT", is_default=True)
def test():
administrator = sp.test_account("Administrator")
alice = sp.test_account("Alice")
tok0_md = fa2.make_metadata(name="Token Zero", decimals=1, symbol="Tok0")
metadata = sp.utils.metadata_of_url("ipfs://example")
sc = sp.test_scenario([fa2.t, fa2.main, m])
c1 = m.MyFungible(
administrator=administrator.address,
metadata=metadata,
ledger={},
token_metadata=[],
)
sc += c1
c1.mint([sp.record(to_=alice.address, amount=5, token=sp.variant("new", tok0_md))]).run(sender=administrator)
c1.mint([sp.record(to_=alice.address, amount=10, token=sp.variant("existing", 0))]).run(sender=administrator)
Here is an example with more features:
import smartpy as sp
from templates import fa2_lib as fa2
main = fa2.main
@sp.module
def m():
# Order of inheritance: [Admin], [<policy>], <base class>, [<mixins>]
class MyFungible(
main.Admin,
main.Fungible,
main.ChangeMetadata,
main.WithdrawMutez,
main.MintFungible,
main.BurnFungible,
main.OnchainviewBalanceOf,
):
def __init__(self, administrator, metadata, ledger, token_metadata):
main.OnchainviewBalanceOf.__init__(self)
main.BurnFungible.__init__(self)
main.MintFungible.__init__(self)
main.WithdrawMutez.__init__(self)
main.ChangeMetadata.__init__(self)
main.Fungible.__init__(self, metadata, ledger, token_metadata)
main.Admin.__init__(self, administrator)
@sp.add_test(name="NFT", is_default=True)
def test():
administrator = sp.test_account("Administrator")
alice = sp.test_account("Alice")
tok0_md = fa2.make_metadata(name="Token Zero", decimals=1, symbol="Tok0")
metadata = sp.utils.metadata_of_url("ipfs://example")
sc = sp.test_scenario([fa2.t, fa2.main, m])
c1 = m.MyFungible(
administrator=administrator.address,
metadata=metadata,
ledger={},
token_metadata=[],
)
sc += c1
c1.mint([sp.record(to_=alice.address, amount=5, token=sp.variant("new", tok0_md))]).run(sender=administrator)
c1.mint([sp.record(to_=alice.address, amount=10, token=sp.variant("existing", 0))]).run(sender=administrator)
For more information you can have a look at the FA2 library guide and in particular the token metadata page.
You can find much more examples by reading the tests for fungible and the testing library.
If you are sure your contract will only create one token you can use the SingleAsset class instead of Fungible. See Base classes | SmartPy. This will be optimized for that type of contracts.