예제 #1
0
        public override DecodedContainer DecodeIntoContainer(Web3 web, string address, BigInteger index, int offset)
        {
            string val = getStorageAt(web, address, index);
            string decode;

            if (offset > 0 || size < 32)
            {
                BigInteger num = SolidityUtils.getAtOffset(val, offset, size);
                val = num.ToString(); //TODO: test ouf small size strings
            }

            decode = new Bytes32TypeDecoder().Decode <string>(val);
            //testString test string \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\u0018
            //clean this up
            string[] substrings = decode.Split('\0', 2);
            if (substrings.Length > 0) //if it is not null terminated for some reason leave it as is
            {
                decode = substrings[0];
            }

            return(new DecodedContainer
            {
                decodedValue = decode,
                rawValue = val,
                solidityVar = this
            });
        }
        public async Task ShouldBeSupportedForSomeOrAllProperties()
        {
            // the result of a known query
            var jsonEncododeQueryBytes =
                "{'rawBytes':'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUZvcmQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARmllc3RhAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARQSBuaWNlIGxpdHRsZSBjYXIAAAAAAAAAAAAAAAAAAAA='}";

            var deserializedWrapper = JObject.Parse(jsonEncododeQueryBytes);
            var rawOutputFromQuery  = (byte[])deserializedWrapper["rawBytes"].ToObject(typeof(byte[]));

            //// vanilla code generation - no properties overriden or changed with "new" operator
            //// bytes32 properties are returned as byte[]
            var codeGeneratedFunctionDto = new GetVehicleOutputDTO();
            var vanillaProperties        = PropertiesExtractor
                                           .GetPropertiesWithParameterAttribute(codeGeneratedFunctionDto.GetType()).ToArray();

            new FunctionCallDecoder().DecodeAttributes(rawOutputFromQuery, codeGeneratedFunctionDto, vanillaProperties);

            var bytes32Decoder = new Bytes32TypeDecoder();

            Assert.Equal(BigInteger.One, codeGeneratedFunctionDto.Vehicle.Id);
            Assert.Equal("Ford", bytes32Decoder.Decode <string>(codeGeneratedFunctionDto.Vehicle.Manufacturer));
            Assert.Equal("Fiesta", bytes32Decoder.Decode <string>(codeGeneratedFunctionDto.Vehicle.Model));
            Assert.Equal("A nice little car", codeGeneratedFunctionDto.Vehicle.Description);

            //// repeating each property on dto with "new" keyword in order to return a different .net type
            var dtoWhereEveryPropertyIsNew = new GetVehicleOutputDTO_WithAllNewPropertyTypes();
            var propertiesForAllNew        = PropertiesExtractor
                                             .GetPropertiesWithParameterAttribute(dtoWhereEveryPropertyIsNew.GetType()).ToArray();

            new FunctionCallDecoder().DecodeAttributes(rawOutputFromQuery, dtoWhereEveryPropertyIsNew,
                                                       propertiesForAllNew);

            Assert.Equal(BigInteger.One, dtoWhereEveryPropertyIsNew.Vehicle.Id);
            Assert.Equal("Ford", dtoWhereEveryPropertyIsNew.Vehicle.Manufacturer);
            Assert.Equal("Fiesta", dtoWhereEveryPropertyIsNew.Vehicle.Model);
            Assert.Equal("A nice little car", dtoWhereEveryPropertyIsNew.Vehicle.Description);

            // custom "vehicle_custom" class which inherits the code generated "vehicle" class
            // bytes32 properties for Manufacturer and Model created as "new" properties
            var modifiedDtoWithSomeNewProperties = new GetVehicleOutputDTO_WithSomeNewPropertyTypes();
            var propertiesForModifiedDto         = PropertiesExtractor
                                                   .GetPropertiesWithParameterAttribute(modifiedDtoWithSomeNewProperties.GetType()).ToArray();

            new FunctionCallDecoder().DecodeAttributes(rawOutputFromQuery, modifiedDtoWithSomeNewProperties,
                                                       propertiesForModifiedDto);


            Assert.Equal(BigInteger.One, modifiedDtoWithSomeNewProperties.Vehicle.Id);
            Assert.Equal("Ford", modifiedDtoWithSomeNewProperties.Vehicle.Manufacturer);
            Assert.Equal("Fiesta", modifiedDtoWithSomeNewProperties.Vehicle.Model);
            Assert.Equal("A nice little car", modifiedDtoWithSomeNewProperties.Vehicle.Description);
        }
예제 #3
0
        public override DecodedContainer DecodeIntoContainer(Web3 web, string address, BigInteger index, int offset)
        {
            if (offset > 0)
            {
                throw new NotSupportedException("Error offset not supported in Enum since it is a int (256bit, 32byte)");
            }
            string val    = getStorageAt(web, address, index);
            string decode = new Bytes32TypeDecoder().Decode <uint>(val).ToString();

            return(new DecodedContainer
            {
                decodedValue = decode,
                rawValue = val,
                solidityVar = this
            });
        }
예제 #4
0
        public override DecodedContainer DecodeIntoContainer(Web3 web, string address, BigInteger index, int offset)
        {
            string     val = getStorageAt(web, address, index);
            BigInteger num;

            if (offset == 0 && size == 32)
            {
                num = new Bytes32TypeDecoder().Decode <BigInteger>(val);
            }
            else
            {
                num = SolidityUtils.getAtOffset(val, offset, size);
            }
            string decode = num.ToString();

            return(new DecodedContainer
            {
                decodedValue = decode,
                rawValue = val,
                solidityVar = this
            });
        }
예제 #5
0
 public Bytes32Type(string name) : base(name)
 {
     Decoder = new Bytes32TypeDecoder();
     Encoder = new Bytes32TypeEncoder();
 }