public override void Encode(ref AbiEncodeBuffer buff)
        {
            var uintEncoder = UInt256Encoder.UncheckedEncoders.Get();

            try
            {
                // write data offset position into header
                int offset = buff.HeadLength + buff.DataAreaCursorPosition;
                uintEncoder.Encode(buff.HeadCursor, offset);
                buff.IncrementHeadCursor(UInt256.SIZE);

                // write array item count into data buffer
                int len = _val.Count();
                uintEncoder.Encode(buff.DataAreaCursor, len);
                buff.IncrementDataCursor(UInt256.SIZE);

                // write payload into data buffer
                var payloadBuffer = new AbiEncodeBuffer(buff.DataAreaCursor, Enumerable.Repeat(_info.ArrayItemInfo, len).ToArray());
                foreach (var item in _val)
                {
                    _itemEncoder.SetValue(item);
                    _itemEncoder.Encode(ref payloadBuffer);
                    buff.IncrementDataCursor(_itemEncoder.GetEncodedSize());
                }
            }
            finally
            {
                UInt256Encoder.UncheckedEncoders.Put(uintEncoder);
            }
        }
示例#2
0
 public override void Encode(ref AbiEncodeBuffer buffer)
 {
     ValidateArrayLength();
     foreach (var item in _val)
     {
         _itemEncoder.SetValue(item);
         _itemEncoder.Encode(ref buffer);
     }
 }
        public void Encode(ref AbiEncodeBuffer buffer)
        {
            ValidateArrayLength();

            // If we have no elements, no work needs to be done.
            if (TypeInfo.ArrayDimensionSizes.Length == 0)
            {
                return;
            }

            // Create a variable to track our position.
            int[] encodingPosition = new int[TypeInfo.ArrayDimensionSizes.Length];

            // Get value as array (convert to array if its underlying type is something else)
            IList parentArray = _val is IList arr ? arr : _val.ToArray();

            // Loop for each element to index.
            bool reachedEnd = false;

            while (!reachedEnd)
            {
                // Define the parent array to resolve for this element.
                IList innerMostArray = parentArray;

                // Increment our decoding position.
                bool incrementing = true;
                for (int x = 0; x < encodingPosition.Length; x++)
                {
                    // If this isn't the final index (inner most array index), then it's an index to another array.
                    if (x < encodingPosition.Length - 1)
                    {
                        // Get value as array (convert to array if its underlying type is something else)
                        var inner = innerMostArray[encodingPosition[x]];
                        innerMostArray = inner is IList ia ? ia : ((inner as IEnumerable).Cast <object>()).ToArray();
                    }
                    else
                    {
                        // We've resolved the element to index.
                        _itemEncoder.SetValue(innerMostArray[encodingPosition[x]]);
                        _itemEncoder.Encode(ref buffer);
                    }

                    // Increment the index for this dimension
                    if (incrementing)
                    {
                        // Increment our position.
                        encodingPosition[x]++;

                        // Determine if we need to carry a digit.
                        if (encodingPosition[x] >= TypeInfo.ArrayDimensionSizes[x])
                        {
                            // Reset the digit, we will carry over to the next.
                            encodingPosition[x] = 0;
                        }
                        else
                        {
                            incrementing = false;
                        }
                    }
                }

                // If we incremented all digits and still have increment flag set, we overflowed our last element, so we reached the end
                reachedEnd = incrementing;
            }
        }
示例#4
0
 public void Encode(ref AbiEncodeBuffer buffer)
 {
     _encoder.Encode(ref buffer);
 }