Lição 3

Understanding a FA1.2 Contract

Welcome to the third lesson of our tokenization course. Having deployed the full token contract, let's break down the contract code and understand the different elements. This lesson aims to give you a deep understanding of how a FA1.2 token contract is constructed.

1. Admin Contract

The Admin contract class in our token contract is responsible for defining administrative privileges. It includes a single entry point: setAdministrator. This entry point allows the current administrator to assign a new administrator.

Python
class Admin(sp.Contract):
    def __init__(self, administrator):
        self.init(administrator=administrator)

    @sp.entrypointdef setAdministrator(self, params):
        sp.verify(sp.sender == self.data.administrator)
        self.data.administrator = params

The setAdministrator function verifies that only the current administrator can execute this function. If the verification fails, the operation is rejected. If it passes, the function proceeds to assign the new administrator.

2. Pause Contract

The Pause contract class provides a mechanism to pause and unpause the contract operations. It includes an entry point setPause that can change the paused status of the contract.

Python
class Pause(sp.Contract):
    def __init__(self):
        self.init(paused=False)

    @sp.entrypointdef setPause(self, params):
        sp.verify(sp.sender == self.data.administrator)
        self.data.paused = params

The function setPause first checks that the operation is performed by the administrator. If the check passes, it updates the paused status of the contract.

3. Mint Contract

The Mint contract class enables increasing the token supply. It comes with a mint entry point that increases the total supply and updates the balance of a particular address.

Python
class Mint(sp.Contract):
    @sp.entrypointdef mint(self, params):
        sp.verify(sp.sender == self.data.administrator)
        self.data.total_supply += params.value
        self.data.balances[params.address].balance += params.value

The mint function first verifies that the sender is the administrator. Then, it increases the total supply and the balance of the address specified.

4. Burn Contract

The Burn contract class is for decreasing the token supply. It has a burn entry point which reduces the total supply and the balance of a specific address.

Python
class Burn(sp.Contract):
    @sp.entrypointdef burn(self, params):
        sp.verify(sp.sender == self.data.administrator)
        self.data.total_supply -= params.value
        self.data.balances[params.address].balance -= params.value

The burn function operates similarly to the mint function but decreases the total supply and the balance of the specified address instead.

With the following code, the Admin burns Bob token

Python
  sc.h2("Admin burns Bob token")
        c1.burn(address=bob.address, value=1).run(sender=admin)

5. ChangeMetadata Contract

The ChangeMetadata contract class is for updating the metadata of the contract. It includes a function update_metadata which updates a key-value pair in the metadata.

Python
class ChangeMetadata(sp.Contract):
    @sp.entrypointdef update_metadata(self, key, value):
        sp.verify(sp.sender == self.data.administrator)
        self.data.metadata[key] = value

The update_metadata function, similar to the previous functions, verifies that the sender is the administrator. Then, it updates the specified key-value pair in the metadata.

In the next lesson, we will delve into the final contract that includes all these functionalities, Fa1_2TestFull. We will learn about how this contract inherits from all the classes we discussed today and how they all contribute to the token’s functionality. Stay tuned!

Isenção de responsabilidade
* O investimento em criptomoedas envolve grandes riscos. Prossiga com cautela. O curso não se destina a servir de orientação para investimentos.
* O curso foi criado pelo autor que entrou para o Gate Learn. As opiniões compartilhadas pelo autor não representam o Gate Learn.
Catálogo
Lição 3

Understanding a FA1.2 Contract

Welcome to the third lesson of our tokenization course. Having deployed the full token contract, let's break down the contract code and understand the different elements. This lesson aims to give you a deep understanding of how a FA1.2 token contract is constructed.

1. Admin Contract

The Admin contract class in our token contract is responsible for defining administrative privileges. It includes a single entry point: setAdministrator. This entry point allows the current administrator to assign a new administrator.

Python
class Admin(sp.Contract):
    def __init__(self, administrator):
        self.init(administrator=administrator)

    @sp.entrypointdef setAdministrator(self, params):
        sp.verify(sp.sender == self.data.administrator)
        self.data.administrator = params

The setAdministrator function verifies that only the current administrator can execute this function. If the verification fails, the operation is rejected. If it passes, the function proceeds to assign the new administrator.

2. Pause Contract

The Pause contract class provides a mechanism to pause and unpause the contract operations. It includes an entry point setPause that can change the paused status of the contract.

Python
class Pause(sp.Contract):
    def __init__(self):
        self.init(paused=False)

    @sp.entrypointdef setPause(self, params):
        sp.verify(sp.sender == self.data.administrator)
        self.data.paused = params

The function setPause first checks that the operation is performed by the administrator. If the check passes, it updates the paused status of the contract.

3. Mint Contract

The Mint contract class enables increasing the token supply. It comes with a mint entry point that increases the total supply and updates the balance of a particular address.

Python
class Mint(sp.Contract):
    @sp.entrypointdef mint(self, params):
        sp.verify(sp.sender == self.data.administrator)
        self.data.total_supply += params.value
        self.data.balances[params.address].balance += params.value

The mint function first verifies that the sender is the administrator. Then, it increases the total supply and the balance of the address specified.

4. Burn Contract

The Burn contract class is for decreasing the token supply. It has a burn entry point which reduces the total supply and the balance of a specific address.

Python
class Burn(sp.Contract):
    @sp.entrypointdef burn(self, params):
        sp.verify(sp.sender == self.data.administrator)
        self.data.total_supply -= params.value
        self.data.balances[params.address].balance -= params.value

The burn function operates similarly to the mint function but decreases the total supply and the balance of the specified address instead.

With the following code, the Admin burns Bob token

Python
  sc.h2("Admin burns Bob token")
        c1.burn(address=bob.address, value=1).run(sender=admin)

5. ChangeMetadata Contract

The ChangeMetadata contract class is for updating the metadata of the contract. It includes a function update_metadata which updates a key-value pair in the metadata.

Python
class ChangeMetadata(sp.Contract):
    @sp.entrypointdef update_metadata(self, key, value):
        sp.verify(sp.sender == self.data.administrator)
        self.data.metadata[key] = value

The update_metadata function, similar to the previous functions, verifies that the sender is the administrator. Then, it updates the specified key-value pair in the metadata.

In the next lesson, we will delve into the final contract that includes all these functionalities, Fa1_2TestFull. We will learn about how this contract inherits from all the classes we discussed today and how they all contribute to the token’s functionality. Stay tuned!

Isenção de responsabilidade
* O investimento em criptomoedas envolve grandes riscos. Prossiga com cautela. O curso não se destina a servir de orientação para investimentos.
* O curso foi criado pelo autor que entrou para o Gate Learn. As opiniões compartilhadas pelo autor não representam o Gate Learn.