Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add from param #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions smartpy_fa2_fungible/part_4_complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
def my_module():

conversion_type: type = sp.record(
from_ = sp.address, # Account to convert tokens from
source_token_id = sp.nat, # The ID of the source token
target_token_id = sp.nat, # The ID of the target token
amount = sp.nat, # The number of source tokens to convert
Expand Down Expand Up @@ -46,16 +47,28 @@ def convert(self, batch):

for conversion in batch:

record(source_token_id, target_token_id, amount).match = conversion
record(
from_,
source_token_id,
target_token_id,
amount
).match = conversion

# Verify that transfers are allowed
assert self.private.policy.supports_transfer, "FA2_TX_DENIED"
# Verify that the source and target tokens exist
assert self.is_defined_(source_token_id), "FA2_TOKEN_UNDEFINED"
assert self.is_defined_(target_token_id), "FA2_TOKEN_UNDEFINED"

# Verify that the sender is either the owner or the operator of the source tokens
assert (sp.sender == from_) or self.data.operators.contains(
sp.record(
owner=from_, operator=sp.sender, token_id=source_token_id
)
), "FA2_NOT_OPERATOR"

# Get a pair to represent the key for the ledger for the source tokens
from_source = (sp.sender, source_token_id)
from_source = (from_, source_token_id)

# Burn the source tokens
self.data.ledger[from_source] = sp.as_nat(
Expand All @@ -72,7 +85,7 @@ def convert(self, batch):
self.data.supply[source_token_id] = 0

# Get a pair to represent the key for the ledger for the target tokens
from_target = (sp.sender, target_token_id)
from_target = (from_, target_token_id)

# Mint the target tokens
target_amount = self.data.ledger.get(from_target, default=0)
Expand Down Expand Up @@ -299,7 +312,7 @@ def test():

# Verify that you can convert your own tokens
conversions = [
sp.record(source_token_id = 0, target_token_id = 1, amount = 2),
sp.record(from_ = alice.address, source_token_id = 0, target_token_id = 1, amount = 2),
]
contract.convert(
conversions,
Expand All @@ -313,3 +326,33 @@ def test():
)
scenario.verify(_total_supply(contract, sp.record(token_id=0)) == 12)
scenario.verify(_total_supply(contract, sp.record(token_id=1)) == 16)

# Verify that you can't convert someone else's tokens
contract.convert(
conversions,
_sender=bob,
_valid=False,
_exception="FA2_NOT_OPERATOR"
)

# Make Bob an operator of Alice's token 0
operator_update = sp.record(owner=alice.address, operator=bob.address, token_id=0)
contract.update_operators(
[
sp.variant.add_operator(operator_update)
],
_sender=alice
)
# Have Bob convert Alice's tokens
contract.convert(
conversions,
_sender=bob
)
scenario.verify(
_get_balance(contract, sp.record(owner=alice.address, token_id=0)) == 6
)
scenario.verify(
_get_balance(contract, sp.record(owner=alice.address, token_id=1)) == 7
)
scenario.verify(_total_supply(contract, sp.record(token_id=0)) == 10)
scenario.verify(_total_supply(contract, sp.record(token_id=1)) == 18)