Exemple #1
0
        public void SetAppTitle(string name, string title)
        {
            var index = FindAppIndex(name);

            Runtime.Expect(index >= 0, "app not found");

            var app = _apps.Get <AppInfo>(index);

            app.title = title;
            _apps.Replace(index, app);
        }
        private bool UpdatePendingSettle(StorageList list, int index)
        {
            var swap       = list.Get <PendingSettle>(index);
            var prevStatus = swap.status;

            switch (swap.status)
            {
            case SwapStatus.Settle:
            {
                var diff = Timestamp.Now - swap.time;
                if (diff >= 60)
                {
                    swap.settleHash = SettleTransaction(DomainSettings.PlatformName, DomainSettings.RootChainName, swap.sourceHash);
                    if (swap.settleHash != Hash.Null)
                    {
                        swap.status = SwapStatus.Confirm;
                    }
                }
                break;
            }

            case SwapStatus.Confirm:
            {
                var result = this.NexusAPI.GetTransaction(swap.settleHash.ToString());
                if (result is TransactionResult)
                {
                    var tx = (TransactionResult)result;
                    swap.status = SwapStatus.Finished;
                }
                else
                if (result is ErrorResult)
                {
                    var error = ((ErrorResult)result).error;
                    if (error != "pending")
                    {
                        swap.settleHash = Hash.Null;
                        swap.time       = Timestamp.Now;
                        swap.status     = SwapStatus.Settle;
                    }
                }
                break;
            }

            default: return(false);
            }

            if (swap.status == SwapStatus.Finished)
            {
                var settlements = new StorageMap(SettlementTag, this.Storage);
                settlements.Set <Hash, Hash>(swap.sourceHash, swap.destinationHash);
                return(true);
            }

            if (swap.status != prevStatus)
            {
                list.Replace <PendingSettle>(index, swap);
            }

            return(false);
        }
        public void SetBroker(Address from, Hash hash)
        {
            Runtime.Expect(Runtime.IsWitness(from), "invalid witness");
            Runtime.Expect(Runtime.IsKnownValidator(from), "invalid validator");

            var count = _withdraws.Count();
            var index = -1;

            for (int i = 0; i < count; i++)
            {
                var entry = _withdraws.Get <InteropWithdraw>(i);
                if (entry.hash == hash)
                {
                    index = i;
                    break;
                }
            }

            Runtime.Expect(index >= 0, "invalid hash");

            var withdraw = _withdraws.Get <InteropWithdraw>(index);

            Runtime.Expect(withdraw.broker.IsNull, "broker already set");

            Runtime.Expect(Runtime.TransferTokens(DomainSettings.FuelTokenSymbol, from, this.Address, withdraw.collateralAmount), "collateral payment failed");
            Runtime.Notify(EventKind.TokenEscrow, from, new TokenEventData()
            {
                chainAddress = this.Runtime.Chain.Address, value = withdraw.feeAmount, symbol = withdraw.feeSymbol
            });

            withdraw.broker    = from;
            withdraw.timestamp = Runtime.Time;
            _withdraws.Replace <InteropWithdraw>(index, withdraw);

            var expireDate = new Timestamp(Runtime.Time.Value + 86400); // 24 hours from now

            Runtime.Notify(EventKind.RolePromote, from, new RoleEventData()
            {
                role = "broker", date = expireDate
            });
        }
        public void TestStorageList()
        {
            var context = new MemoryStorageContext();
            var list    = new StorageList("test".AsByteArray(), context);

            Assert.IsTrue(list.Count() == 0);

            list.Add("hello");
            list.Add("world");
            Assert.IsTrue(list.Count() == 2);

            list.RemoveAt(0);
            Assert.IsTrue(list.Count() == 1);

            var temp = list.Get <string>(0);

            Assert.IsTrue(temp == "world");

            list.Replace <string>(0, "hello");

            temp = list.Get <string>(0);
            Assert.IsTrue(temp == "hello");
        }
Exemple #5
0
        public void Migrate(Address from, Address to)
        {
            Runtime.Expect(IsWitness(from), "witness failed");

            Runtime.Expect(to.IsUser, "destination must be user address");

            var index = GetIndexOfValidator(from);

            Runtime.Expect(index >= 0, "not a validator");

            var transferResult = (bool)Runtime.CallContext("energy", "Migrate", from, to);

            Runtime.Expect(transferResult, "stake transfer failed");

            _validatorList.Replace <Address>(index, to);

            var entry = _validatorMap.Get <Address, ValidatorEntry>(from);

            _validatorMap.Remove <Address>(from);

            entry.address = to;
            _validatorMap.Set <Address, ValidatorEntry>(to, entry);
        }
        public void MasterClaim(Address from)
        {
            Runtime.Expect(_masterClaimCount < 12 * 4, "no more claims available"); // 4 years

            Runtime.Expect(Runtime.IsWitness(from), "invalid witness");
            Runtime.Expect(IsMaster(from), "invalid master");

            var thisClaimDate = GetMaster(from).claimDate;

            Runtime.Expect(Runtime.Time >= thisClaimDate, "not enough time waited");

            var symbol = DomainSettings.StakingTokenSymbol;
            var token  = Runtime.GetToken(symbol);

            var totalAmount = MasterClaimGlobalAmount;

            Runtime.Expect(Runtime.MintTokens(token.Symbol, this.Address, this.Address, totalAmount), "mint failed");

            var listSize = _mastersList.Count();

            var validMasterCount = GetClaimMasterCount(thisClaimDate);

            var individualAmount = totalAmount / validMasterCount;
            var leftovers        = totalAmount % validMasterCount;

            for (int i = 0; i < listSize; i++)
            {
                var targetMaster = _mastersList.Get <EnergyMaster>(i);

                if (targetMaster.claimDate != thisClaimDate)
                {
                    continue;
                }

                var transferAmount = individualAmount;
                if (targetMaster.address == from)
                {
                    transferAmount += leftovers;
                }

                Runtime.Expect(Runtime.TransferTokens(token.Symbol, this.Address, targetMaster.address, transferAmount), "transfer failed");

                totalAmount -= transferAmount;

                Runtime.Notify(EventKind.TokenMint, targetMaster.address, new TokenEventData()
                {
                    symbol = token.Symbol, value = transferAmount, chainAddress = this.Address
                });

                var nextClaim = GetMasterClaimDateFromReference(1, thisClaimDate);

                _mastersList.Replace(i, new EnergyMaster()
                {
                    address = from, claimDate = nextClaim
                });
            }
            Runtime.Expect(totalAmount == 0, "something failed");

            _lastMasterClaim = Runtime.Time;
            _masterClaimCount++;
        }