Пример #1
0
        public async Task StoreAndDeleteContract_ShouldDelete()
        {
            const string CONTRACT1_ID    = "Contract1-ID";
            var          contractStorage = new ContractStorage(new LocalStorageServiceMock());

            await contractStorage.StoreContract(CONTRACT1_ID, "Contract1", "Contract1-Content");

            var contractLinks = await contractStorage.GetAllContractLinks();

            var contract1 = await contractStorage.GetContractXml(CONTRACT1_ID);

            Assert.Equal(1, contractLinks.Count);

            var contractLink1 = contractLinks.Single(c => c.ContractId == CONTRACT1_ID);

            Assert.Equal("Contract1-Content", contract1);

            await contractStorage.RemoveContract(CONTRACT1_ID);

            contractLinks = await contractStorage.GetAllContractLinks();

            contract1 = await contractStorage.GetContractXml(CONTRACT1_ID);

            Assert.Empty(contractLinks);
            Assert.Null(contract1);
        }
Пример #2
0
        public async Task StoreContract_ShouldStore()
        {
            const string CONTRACT1_ID    = "Contract1-ID";
            const string CONTRACT2_ID    = "Contract2-ID";
            var          contractStorage = new ContractStorage(new LocalStorageServiceMock());

            await contractStorage.StoreContract(CONTRACT1_ID, "Contract1", "Contract1-Content");

            await contractStorage.StoreContract(CONTRACT2_ID, "Contract2", "Contract2-Content");

            var contractLinks = await contractStorage.GetAllContractLinks();

            var contract1 = await contractStorage.GetContractXml(CONTRACT1_ID);

            var contract2 = await contractStorage.GetContractXml(CONTRACT2_ID);

            Assert.Equal(2, contractLinks.Count);

            var contractLink1 = contractLinks.Single(c => c.ContractId == CONTRACT1_ID);
            var contractLink2 = contractLinks.Single(c => c.ContractId == CONTRACT2_ID);

            Assert.Equal("Contract1", contractLink1.ContractName);
            Assert.Equal("Contract2", contractLink2.ContractName);
            Assert.Equal("Contract1-Content", contract1);
            Assert.Equal("Contract2-Content", contract2);
        }
Пример #3
0
        protected async Task OnOpenRecentClicked(string id)
        {
            var contractXml = await ContractStorage.GetContractXml(id);

            ContractManager.RestoreContract(contractXml);
            NavigationManager.NavigateTo("/process");
        }
Пример #4
0
        protected override async Task OnParametersSetAsync()
        {
            ContractLinks = await ContractStorage.GetAllContractLinks();

            ExampleContracts = await ExampleLoader.ReadManifest();

            await base.OnParametersSetAsync();
        }
Пример #5
0
        protected async void RemoveStoredContract(string contractId)
        {
            if (await SaveGuardJsCommunicator.DisplayAndCollectConfirmation("This will permanently remove the contract. Are you sure?"))
            {
                await ContractStorage.RemoveContract(contractId);

                ContractLinks = await ContractStorage.GetAllContractLinks();

                StateHasChanged();
            }
        }
Пример #6
0
        async Task OriginateContract(Block block, string address)
        {
            var rawContract = await Proto.Rpc.GetContractAsync(block.Level, address);

            #region contract
            var contract = new Contract
            {
                Id              = Cache.AppState.NextAccountId(),
                FirstBlock      = block,
                Address         = address,
                Balance         = rawContract.RequiredInt64("balance"),
                Creator         = await Cache.Accounts.GetAsync(NullAddress.Address),
                Type            = AccountType.Contract,
                Kind            = ContractKind.SmartContract,
                MigrationsCount = 1,
            };

            Db.TryAttach(contract.Creator);
            contract.Creator.ContractsCount++;

            Db.Accounts.Add(contract);
            #endregion

            #region script
            var code           = Micheline.FromJson(rawContract.Required("script").Required("code")) as MichelineArray;
            var micheParameter = code.First(x => x is MichelinePrim p && p.Prim == PrimType.parameter);
            var micheStorage   = code.First(x => x is MichelinePrim p && p.Prim == PrimType.storage);
            var micheCode      = code.First(x => x is MichelinePrim p && p.Prim == PrimType.code);
            var micheViews     = code.Where(x => x is MichelinePrim p && p.Prim == PrimType.view);
            var script         = new Script
            {
                Id              = Cache.AppState.NextScriptId(),
                Level           = block.Level,
                ContractId      = contract.Id,
                ParameterSchema = micheParameter.ToBytes(),
                StorageSchema   = micheStorage.ToBytes(),
                CodeSchema      = micheCode.ToBytes(),
                Views           = micheViews.Any()
                    ? micheViews.Select(x => x.ToBytes()).ToArray()
                    : null,
                Current = true
            };

            var viewsBytes = script.Views?
                             .OrderBy(x => x, new BytesComparer())
                             .SelectMany(x => x)
                             .ToArray()
                             ?? Array.Empty <byte>();
            var typeSchema = script.ParameterSchema.Concat(script.StorageSchema).Concat(viewsBytes);
            var fullSchema = typeSchema.Concat(script.CodeSchema);
            contract.TypeHash = script.TypeHash = Script.GetHash(typeSchema);
            contract.CodeHash = script.CodeHash = Script.GetHash(fullSchema);

            if (script.Schema.IsFA1())
            {
                if (script.Schema.IsFA12())
                {
                    contract.Tags |= ContractTags.FA12;
                }

                contract.Tags |= ContractTags.FA1;
                contract.Kind  = ContractKind.Asset;
            }
            if (script.Schema.IsFA2())
            {
                contract.Tags |= ContractTags.FA2;
                contract.Kind  = ContractKind.Asset;
            }

            Db.Scripts.Add(script);
            #endregion

            #region storage
            var storageValue = Micheline.FromJson(rawContract.Required("script").Required("storage"));
            var storage      = new Storage
            {
                Id         = Cache.AppState.NextStorageId(),
                Level      = block.Level,
                ContractId = contract.Id,
                RawValue   = script.Schema.OptimizeStorage(storageValue, false).ToBytes(),
                JsonValue  = script.Schema.HumanizeStorage(storageValue),
                Current    = true
            };

            Db.Storages.Add(storage);
            #endregion

            #region migration
            var migration = new MigrationOperation
            {
                Id            = Cache.AppState.NextOperationId(),
                Block         = block,
                Level         = block.Level,
                Timestamp     = block.Timestamp,
                Kind          = MigrationKind.Origination,
                Account       = contract,
                BalanceChange = contract.Balance,
                Script        = script,
                Storage       = storage,
            };

            script.MigrationId  = migration.Id;
            storage.MigrationId = migration.Id;

            block.Events     |= BlockEvents.SmartContracts;
            block.Operations |= Operations.Migrations;

            var state = Cache.AppState.Get();
            state.MigrationOpsCount++;

            var statistics = await Cache.Statistics.GetAsync(state.Level);

            statistics.TotalCreated += contract.Balance;

            Db.MigrationOps.Add(migration);
            #endregion

            #region bigmaps
            var storageScript = new ContractStorage(micheStorage);
            var storageTree   = storageScript.Schema.ToTreeView(storageValue);
            var bigmaps       = storageTree.Nodes()
                                .Where(x => x.Schema is BigMapSchema)
                                .Select(x => (x, x.Schema as BigMapSchema, (int)(x.Value as MichelineInt).Value));

            foreach (var(bigmap, schema, ptr) in bigmaps)
            {
                block.Events |= BlockEvents.Bigmaps;

                migration.BigMapUpdates = (migration.BigMapUpdates ?? 0) + 1;
                Db.BigMapUpdates.Add(new BigMapUpdate
                {
                    Id          = Cache.AppState.NextBigMapUpdateId(),
                    Action      = BigMapAction.Allocate,
                    BigMapPtr   = ptr,
                    Level       = block.Level,
                    MigrationId = migration.Id
                });

                var allocated = new BigMap
                {
                    Id          = Cache.AppState.NextBigMapId(),
                    Ptr         = ptr,
                    ContractId  = contract.Id,
                    StoragePath = bigmap.Path,
                    KeyType     = schema.Key.ToMicheline().ToBytes(),
                    ValueType   = schema.Value.ToMicheline().ToBytes(),
                    Active      = true,
                    FirstLevel  = block.Level,
                    LastLevel   = block.Level,
                    ActiveKeys  = 0,
                    TotalKeys   = 0,
                    Updates     = 1,
                    Tags        = BigMaps.GetTags(bigmap)
                };
                Db.BigMaps.Add(allocated);

                if (address == LiquidityToken && allocated.StoragePath == "tokens")
                {
                    var rawKey   = new MichelineString(NullAddress.Address);
                    var rawValue = new MichelineInt(100);

                    allocated.ActiveKeys++;
                    allocated.TotalKeys++;
                    allocated.Updates++;
                    var key = new BigMapKey
                    {
                        Id         = Cache.AppState.NextBigMapKeyId(),
                        Active     = true,
                        BigMapPtr  = ptr,
                        FirstLevel = block.Level,
                        LastLevel  = block.Level,
                        JsonKey    = schema.Key.Humanize(rawKey),
                        JsonValue  = schema.Value.Humanize(rawValue),
                        RawKey     = schema.Key.Optimize(rawKey).ToBytes(),
                        RawValue   = schema.Value.Optimize(rawValue).ToBytes(),
                        KeyHash    = schema.GetKeyHash(rawKey),
                        Updates    = 1
                    };
                    Db.BigMapKeys.Add(key);

                    migration.BigMapUpdates++;
                    Db.BigMapUpdates.Add(new BigMapUpdate
                    {
                        Id          = Cache.AppState.NextBigMapUpdateId(),
                        Action      = BigMapAction.AddKey,
                        BigMapKeyId = key.Id,
                        BigMapPtr   = key.BigMapPtr,
                        JsonValue   = key.JsonValue,
                        RawValue    = key.RawValue,
                        Level       = key.LastLevel,
                        MigrationId = migration.Id
                    });
                }
            }
            #endregion
        }