Example #1
0
        public void BurnTokens(string symbol, Address target, BigInteger amount)
        {
            var Runtime = this;

            Runtime.Expect(amount > 0, "amount must be positive and greater than zero");
            Runtime.Expect(IsWitness(target), "invalid witness");

            Runtime.Expect(Runtime.TokenExists(symbol), "invalid token");
            var token = Runtime.GetToken(symbol);

            Runtime.Expect(token.Flags.HasFlag(TokenFlags.Fungible), "token must be fungible");
            Runtime.Expect(token.IsBurnable(), "token must be burnable");
            Runtime.Expect(!token.Flags.HasFlag(TokenFlags.Fiat), "token can't be fiat");

            Nexus.BurnTokens(this, token, target, target, Chain.Name, amount);
        }
Example #2
0
        public bool TransferTokens(string symbol, Address source, Address destination, BigInteger amount)
        {
            var Runtime = this;

            if (IsPlatformAddress(source))
            {
                return(Nexus.MintTokens(this, symbol, this.EntryAddress, destination, amount, true));
            }

            if (IsPlatformAddress(destination))
            {
                return(Nexus.BurnTokens(this, symbol, source, amount, true));
            }

            Runtime.Expect(amount > 0, "amount must be positive and greater than zero");
            Runtime.Expect(source != destination, "source and destination must be different");
            Runtime.Expect(IsWitness(source), "invalid witness");
            Runtime.Expect(!Runtime.IsTrigger, "not allowed inside a trigger");

            if (destination.IsInterop)
            {
                Runtime.Expect(Runtime.Chain.IsRoot, "interop transfers only allowed in main chain");
                Runtime.CallContext("interop", "WithdrawTokens", source, destination, symbol, amount);
                return(true);
            }

            Runtime.Expect(Runtime.Nexus.TokenExists(symbol), "invalid token");
            var tokenInfo = Runtime.Nexus.GetTokenInfo(symbol);

            Runtime.Expect(tokenInfo.Flags.HasFlag(TokenFlags.Fungible), "token must be fungible");
            Runtime.Expect(tokenInfo.Flags.HasFlag(TokenFlags.Transferable), "token must be transferable");

            Runtime.Expect(Runtime.Nexus.TransferTokens(Runtime, symbol, source, destination, amount), "transfer failed");

            Runtime.Notify(EventKind.TokenSend, source, new TokenEventData()
            {
                chainAddress = Runtime.Chain.Address, value = amount, symbol = symbol
            });
            Runtime.Notify(EventKind.TokenReceive, destination, new TokenEventData()
            {
                chainAddress = Runtime.Chain.Address, value = amount, symbol = symbol
            });

            return(true);
        }
Example #3
0
        public void SwapTokens(string sourceChain, Address from, string targetChain, Address to, string symbol, BigInteger value, byte[] rom, byte[] ram)
        {
            var Runtime = this;

            Runtime.Expect(sourceChain != targetChain, "source chain and target chain must be different");
            Runtime.Expect(Runtime.TokenExists(symbol), "invalid token");

            var token = Runtime.GetToken(symbol);

            Runtime.Expect(token.Flags.HasFlag(TokenFlags.Transferable), "must be transferable token");

            if (token.IsFungible())
            {
                Runtime.Expect(rom == null, "fungible tokens cant have rom");
                Runtime.Expect(ram == null, "fungible tokens cant have ram");
            }
            else
            {
                Runtime.Expect(rom != null & rom.Length > 0, "nft rom is missing");
                Runtime.Expect(ram != null, "nft ram is missing");
            }

            if (PlatformExists(sourceChain))
            {
                Runtime.Expect(sourceChain != DomainSettings.PlatformName, "invalid platform as source chain");

                if (token.IsFungible())
                {
                    Nexus.MintTokens(this, token, from, to, sourceChain, value);
                }
                else
                {
                    Nexus.MintToken(this, token, from, to, sourceChain, value, rom, ram);
                }
            }
            else
            if (PlatformExists(targetChain))
            {
                Runtime.Expect(targetChain != DomainSettings.PlatformName, "invalid platform as target chain");
                Nexus.BurnTokens(this, token, from, to, targetChain, value);

                var swap = new ChainSwap(DomainSettings.PlatformName, sourceChain, Transaction.Hash, targetChain, targetChain, Hash.Null);
                this.Chain.RegisterSwap(this.Storage, to, swap);
            }
            else
            if (sourceChain == this.Chain.Name)
            {
                Runtime.Expect(IsNameOfParentChain(targetChain) || IsNameOfChildChain(targetChain), "target must be parent or child chain");
                Runtime.Expect(to.IsUser, "destination must be user address");
                Runtime.Expect(IsWitness(from), "invalid witness");

                /*if (tokenInfo.IsCapped())
                 * {
                 *  var sourceSupplies = new SupplySheet(symbol, this.Runtime.Chain, Runtime.Nexus);
                 *  var targetSupplies = new SupplySheet(symbol, targetChain, Runtime.Nexus);
                 *
                 *  if (IsAddressOfParentChain(targetChainAddress))
                 *  {
                 *      Runtime.Expect(sourceSupplies.MoveToParent(this.Storage, amount), "source supply check failed");
                 *  }
                 *  else // child chain
                 *  {
                 *      Runtime.Expect(sourceSupplies.MoveToChild(this.Storage, targetChain.Name, amount), "source supply check failed");
                 *  }
                 * }*/

                if (token.IsFungible())
                {
                    Nexus.BurnTokens(this, token, from, to, targetChain, value);
                }
                else
                {
                    Nexus.BurnToken(this, token, from, to, targetChain, value);
                }

                var swap = new ChainSwap(DomainSettings.PlatformName, sourceChain, Transaction.Hash, DomainSettings.PlatformName, targetChain, Hash.Null);
                this.Chain.RegisterSwap(this.Storage, to, swap);
            }
            else
            if (targetChain == this.Chain.Name)
            {
                Runtime.Expect(IsNameOfParentChain(sourceChain) || IsNameOfChildChain(sourceChain), "source must be parent or child chain");
                Runtime.Expect(!to.IsInterop, "destination cannot be interop address");
                Runtime.Expect(IsWitness(to), "invalid witness");

                if (token.IsFungible())
                {
                    Nexus.MintTokens(this, token, from, to, sourceChain, value);
                }
                else
                {
                    Nexus.MintToken(this, token, from, to, sourceChain, value, rom, ram);
                }
            }
            else
            {
                throw new ChainException("invalid swap chain source and destinations");
            }
        }