Published 15 October 2018

Пример токена на платформе Credits

Пример токена на платформе Credits

Для создания токена необходим интерфейс BasicStandard (согласно стандарта, должен быть реализован обязательно, приведен ниже)

public interface BasicStandard {

   String getName();

   String getSymbol();

   int getDecimal();

   boolean setFrozen(boolean frozen);

   String totalSupply();

   String balanceOf(String owner);

   String allowance(String owner, String spender);

   boolean transfer(String to, String amount);

   boolean transferFrom(String from, String to, String amount);

   void approve(String spender, String amount);

   boolean burn(String amount);

}

и  ExtensionStandard (согласно стандарта, реализация рекомендуема, но не обязательно, приведен ниже)

public interface ExtensionStandard extends BasicStandard {

  void register();

  boolean buyTokens(String amount);

}

Далее приводиться пример реализации токена, согласно стандарта Credits

В примере заданы в конструкторе класса:

наименование токена (name)

символ токена (symbol)

количество знаков (decimal)

публичный ключ владельца токена (owner)

общее количество токенов (totalCoins)

далее приведен пример реализация методов интерфейсов BasicStandard  и ExtensionStandard

 

package com.Credits.cst;
import com.ExtensionStandard;
import com.SmartContract;
import java.math.BigDecimal;
public class SampleContract extends SmartContract implements ExtensionStandard {
   static final String checkRegisterError = "This user is not registered, please call register() method";
   static final String checkFrozenError =
      "This smartContract is frozen, only approve()";
   final String owner;
   private String name;
   private String symbol;
   private java.math.BigDecimal totalCoins;
   private java.util.HashMap<String, java.util.Map<String, java.math.BigDecimal>> allowed;
   java.util.HashMap<String, java.math.BigDecimal> balances;
   private final java.math.BigDecimal tokenCost;
   private boolean frozen;
   private final int decimal;
   public Contract() {
      name = "SampleToken";
      symbol = "CT";
      decimal = 3;
      tokenCost = new java.math.BigDecimal(10).setScale(decimal, java.math.BigDecimal.ROUND_FLOOR);
      totalCoins = new java.math.BigDecimal(10_000_000).setScale(decimal, java.math.BigDecimal.ROUND_FLOOR);
      owner = "owner’s public key";
      allowed = new java.util.HashMap<>();
      balances = new java.util.HashMap<>();
      balances.put(owner, new java.math.BigDecimal(1_000_000L).setScale(decimal, java.math.BigDecimal.ROUND_FLOOR));
   }
   

   @Override
   public int getDecimal() {
      return decimal;
   }


   @Override
   public void register() {
      balances.putIfAbsent(initiator, java.math.BigDecimal.ZERO.setScale(decimal, java.math.BigDecimal.ROUND_FLOOR));
   }


   @Override
   public boolean setFrozen(boolean frozen) {
      if (initiator.equals(owner)) {
         this.frozen = frozen;
         return true;
      }
      return false;
   }


   @Override
   public String getName() {
      return name;
   }


   @Override
   public String getSymbol() {
      return symbol;
   }


   @Override
   public String totalSupply() {
      return totalCoins.toString();
   }


   @Override
   public String balanceOf(String owner) {
      checkRegistered(initiator);
      java.math.BigDecimal balance = balances.get(owner);
      return balance != null ? balance.toString() : "0";
   }


   @Override
   public String allowance(String owner, String spender) {
      checkRegistered(initiator);
      if (allowed.get(owner) == null) {
         return "0";
      }
      java.math.BigDecimal amount = allowed.get(owner).get(spender);
      return amount != null ? amount.toString() : "0";
   }


   @Override
   public boolean transfer(String to, String amount) {
      checkRegistered(initiator);
      checkFrozen();
      java.math.BigDecimal decimalAmount = new java.math.BigDecimal(amount).setScale(decimal, java.math.BigDecimal.ROUND_FLOOR);
      java.math.BigDecimal senderBalance = balances.get(initiator);
      java.math.BigDecimal targetBalance = balances.get(to);
      if (senderBalance == null || targetBalance == null || senderBalance.compareTo(decimalAmount) < 0) {
         return false;
      }
      balances.put(initiator, senderBalance.subtract(decimalAmount));
      balances.put(to, targetBalance.add(decimalAmount));
      return true;
   }


   @Override
   public boolean transferFrom(String from, String to, String amount) {
      checkRegistered(initiator);
      checkFrozen();
      java.math.BigDecimal sourceBalance = balances.get(from);
      java.math.BigDecimal targetBalance = balances.get(to);
      java.math.BigDecimal decimalAmount = new java.math.BigDecimal(amount).setScale(decimal, java.math.BigDecimal.ROUND_FLOOR);
      if (sourceBalance == null || targetBalance == null || sourceBalance.compareTo(decimalAmount) < 0) {
         return false;
      }
      java.util.Map<String, java.math.BigDecimal> spender = allowed.get(from);
      if (spender == null) {
         return false;
      }
      java.math.BigDecimal allowTokens = spender.get(initiator);
      if (allowTokens == null || allowTokens.compareTo(decimalAmount) < 0) {
         return false;
      }
      spender.put(initiator, allowTokens.subtract(decimalAmount));
      balances.put(from, sourceBalance.subtract(decimalAmount));
      balances.put(to, targetBalance.add(decimalAmount));
      return true;
   }


   @Override
   public void approve(String spender, String amount) {
      checkRegistered(initiator);
      java.math.BigDecimal decimalAmount = new java.math.BigDecimal(amount).setScale(decimal, java.math.BigDecimal.ROUND_FLOOR);
      java.util.Map<String, java.math.BigDecimal> initiatorSpenders = allowed.get(initiator);
      if (initiatorSpenders == null) {
         java.util.Map<String, java.math.BigDecimal> newSpender = new java.util.HashMap<>();
         newSpender.put(spender, decimalAmount);
         allowed.put(initiator, newSpender);
      } else {
         java.math.BigDecimal spenderAmount = initiatorSpenders.get(spender);
         initiatorSpenders.put(spender, spenderAmount == null ? decimalAmount : spenderAmount.add(decimalAmount));
      }
   }


   @Override
   public boolean burn(String amount) {
      checkRegistered(initiator);
      checkFrozen();
      java.math.BigDecimal decimalAmount = new java.math.BigDecimal(amount).setScale(decimal, java.math.BigDecimal.ROUND_FLOOR);
      if (!initiator.equals(owner) || totalCoins.compareTo(decimalAmount) < 0) {
         return false;
      }
      totalCoins = totalCoins.subtract(decimalAmount);
      return true;
   }


   @Override
   public boolean buyTokens(String amount) {
      checkRegistered(initiator);
      checkFrozen();
      java.math.BigDecimal decimalAmount = new java.math.BigDecimal(amount).setScale(decimal, java.math.BigDecimal.ROUND_FLOOR);
      sendTransaction(initiator, owner, tokenCost.multiply(decimalAmount).doubleValue(), "", 0.1);
      if (balances.containsKey(initiator)) {
         balances.put(initiator, balances.get(initiator).add(decimalAmount));
      } else {
         balances.put(initiator, decimalAmount);
      }
      totalCoins = totalCoins.subtract(decimalAmount);
      return true;
   }
   public boolean checkRegistered(String owner) {
      if (balances.get(owner) == null) {
         throw new RuntimeException(checkRegisterError);
      }
         return true;
   }
   public boolean checkFrozen() {
      if (this.frozen) {
         throw new RuntimeException(checkFrozenError);
      }
      return true;
   }
}

29 Useful article

Contents