Drop Function

One of the first tasks with a new token is implementing a drop mechanism, allowing the contract owner to easily distribute tokens to selected wallets—often used for airdrops to promote a project. We’ll use the msg structure to verify the caller is the token owner. This function requires managing both the owner’s and recipient’s wallets simultaneously.

 Drop: function (to, amount) {
    
      // If the sender address is the contract owner and it is a positive amount
      if (msg.From == this._owner && amount > 0) {
          
          // Creates a new wallet instance 
          var Owner = Object.create(CRC_Wallet); 
        
          // Loads the owner's  wallet
          Owner.OpenWallet(this._owner);
        
          // check if the owner has enough tokens for the drop
          if(Owner.Balance < amount)
          {
                // Low balance
                print("Low Balance\n");
                return false;
          }
          // Create a new wallet instance 
          var To = Object.create(CRC_Wallet); 
        
          // Opens the recipient's wallet
          if(To.OpenWallet(to)){

                // If the wallet is available transfers the tokens to the recipient's Wallet
                To.Balance   += Number(amount);
                
                // Removes the tokens out of the owner's wallet
                Owner.Balance-= Number(amount);
            
                // Removes the same amount  of tokens out of the contract's balance
                this._Balance -= Number(amount);

                // Print the message in JSON format
                println('Dropped : ' + amount + ', To : ' + To.Address);

                // close the wallets
                Owner.CloseWallet();
                To.CloseWallet();
            
                return true;
            
          } else {
            
                // Invalid Recipient Address
                print("Invalid Recipient\n");
            
                return false;            
          }
      }
    
      // unauthorized Drop
      print("Invalid Drop\n");
    
      return false;
  }

Observations (OBS):

1. Before opening any wallets, we first verify the caller’s identity as the token owner and ensure the token amount is positive. If these conditions aren’t met, we skip wallet access to avoid slowing down execution.

2. We also confirm the owner has enough tokens before proceeding. If not, we skip accessing the recipient’s wallet, saving both time and unnecessary wallet access since no updates are needed for the owner’s wallet.

3. We check if the recipient’s wallet opens but not the owner’s, as we know the owner’s wallet exists (it initiated the transaction). The recipient’s address, however, might be incorrect. While this is typically checked by the front-end, it’s good practice to include it in the code.

Once all conditions are met, we access the wallets and update them, ensuring optimal performance by carefully ordering the operations.

Last updated