code |
I = importlib
collection_name = Variable()
collection_owner = Variable()
collection_nfts = Hash(default_value=0)
collection_balances = Hash(default_value=0)
collection_balances_approvals = Hash(default_value=0)
@construct
def seed():
collection_name.set("Bad Bunny")
collection_owner.set(ctx.caller)
@export
def mint_nft(name: str, description: str, ipfs_image_url: str, metadata: dict, amount: int):
assert name != "", "Name cannot be empty"
assert collection_nfts[name] == 0, "Name already exists"
assert amount > 0, "You cannot transfer negative amounts"
assert collection_owner.get() == ctx.caller, "Only the collection owner can mint NFTs"
collection_nfts[name] = {"description": description, "ipfs_image_url": ipfs_image_url, "metadata": metadata, "amount": amount}
collection_balances[ctx.caller, name] = amount
@export
def transfer(name: str, amount:int, to: str):
assert amount > 0, "You cannot transfer negative amounts"
assert name != "", "Please specify the name of the NFT you want to transfer"
assert collection_balances[ctx.caller, name] >= amount, "You dont have enough NFTs to send"
collection_balances[ctx.caller, name] -= amount
collection_balances[to, name] += amount # Adds amount to receiver
@export
def approve(amount: int, name: str, to: str):
assert amount > 0, "Cannot approve negative amounts"
collection_balances_approvals[ctx.caller, to, name] += amount
@export
def transfer_from(name:str, amount:int, to: str, main_account: str):
assert amount > 0, "Cannot send negative balances!"
assert collection_balances_approvals[main_account, to, name] >= amount, "Not enough NFTs approved to send! You have {} and are trying to spend {}".format(collection_balances_approvals[main_account, to, name], amount)
assert collection_balances[main_account, name] >= amount, "Not enough NFTs to send!"
collection_balances_approvals[main_account, to, name] -= amount
collection_balances[main_account, name] -= amount
collection_balances[to, name] += amount
@export
def setup_redeem(name: str, redeem_type: str, redeem_contract: str, redeem_amount: float):
assert collection_owner.get() == ctx.caller, "You are not the collection creator"
assert collection_nfts[name] != 0, f"NFT {name} doesnt exist"
assert redeem_type.lower() in ["lp", "token"], "Only redeem types lp and token supported"
nft_data = collection_nfts[name]
assert "redeem_type" not in nft_data, "NFT is already redeemable!"
nft_data["redeem_type"] = redeem_type.lower()
nft_data["redeem_contract"] = redeem_contract
nft_data["redeem_amount"] = redeem_amount
collection_nfts[name] = nft_data
# Transfer token
if redeem_type.lower() == "token":
I.import_module(redeem_contract).transfer_from(
amount=redeem_amount,
to=ctx.this,
main_account=ctx.caller)
# Transfer LP
elif redeem_type.lower() == "lp":
I.import_module("con_rocketswap_official_v1_1").transfer_liquidity_from(
contract=redeem_contract,
amount=redeem_amount,
to=ctx.this,
main_account=ctx.caller)
@export
def redeem(name: str):
assert collection_nfts[name] != 0, f"NFT {name} doesnt exist"
nft_data = collection_nfts[name]
assert "redeem_type" in nft_data, "NFT is not redeemable!"
# Get redeem amount for one NFT
redeem_amount = nft_data["redeem_amount"]
nft_amount = nft_data["amount"]
amount = redeem_amount / nft_amount
# Transfer redeemable amount
if nft_data["redeem_type"].lower() == "token":
I.import_module(nft_data["redeem_contract"]).transfer(amount=amount, to=ctx.caller)
elif nft_data["redeem_type"].lower() == "lp":
I.import_module("con_rocketswap_official_v1_1").transfer_liquidity(contract=nft_data["redeem_contract"], amount=amount, to=ctx.caller)
# Burn NFT
approve(name=name, amount=1, to=ctx.this)
transfer_from(name=name, amount=1, to=ctx.this, main_account=ctx.caller) |