Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Mainnet/STOContract/README.md

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions Mainnet/STOContract/STOContract.Tests/AddressExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Stratis.SmartContracts;
using System;
using System.Collections.Generic;
using System.Text;

namespace STOContractTests
{
public static class AddressExtensions
{
private static byte[] HexStringToBytes(string val)
{
if (val.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
val = val.Substring(2);

byte[] ret = new byte[val.Length / 2];
for (int i = 0; i < val.Length; i = i + 2)
{
string hexChars = val.Substring(i, 2);
ret[i / 2] = byte.Parse(hexChars, System.Globalization.NumberStyles.HexNumber);
}
return ret;
}

public static Address HexToAddress(this string hexString)
{
// uint160 only parses a big-endian hex string
var result = HexStringToBytes(hexString);
return CreateAddress(result);
}

private static Address CreateAddress(byte[] bytes)
{
uint pn0 = BitConverter.ToUInt32(bytes, 0);
uint pn1 = BitConverter.ToUInt32(bytes, 4);
uint pn2 = BitConverter.ToUInt32(bytes, 8);
uint pn3 = BitConverter.ToUInt32(bytes, 12);
uint pn4 = BitConverter.ToUInt32(bytes, 16);

return new Address(pn0, pn1, pn2, pn3, pn4);
}
}
}
16 changes: 16 additions & 0 deletions Mainnet/STOContract/STOContract.Tests/CreateResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Stratis.SmartContracts;

namespace STOContractTests
{
public class CreateResult : ICreateResult
{
public Address NewContractAddress { get; private set; }

public bool Success { get; private set; }

public static ICreateResult Failed() => new CreateResult { Success = false };


public static ICreateResult Succeed(Address address) => new CreateResult { Success = true, NewContractAddress = address };
}
}
83 changes: 83 additions & 0 deletions Mainnet/STOContract/STOContract.Tests/InMemoryState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Stratis.SmartContracts;
using System;
using System.Collections.Generic;

namespace STOContractTests
{
public class InMemoryState : IPersistentState
{
private readonly Dictionary<string, object> storage = new Dictionary<string, object>();
public bool IsContractResult { get; set; }
public void Clear(string key) => storage.Remove(key);

public T GetValue<T>(string key) => (T)storage.GetValueOrDefault(key, default(T));

public void AddOrReplace(string key, object value)
{
if (!storage.TryAdd(key,value))
storage[key] = value;
}
public Address GetAddress(string key) => GetValue<Address>(key);

public T[] GetArray<T>(string key) => GetValue<T[]>(key);

public bool GetBool(string key) => GetValue<bool>(key);

public byte[] GetBytes(byte[] key) => throw new NotImplementedException();

public byte[] GetBytes(string key) => GetValue<byte[]>(key);

public char GetChar(string key) => GetValue<char>(key);

public int GetInt32(string key) => GetValue<int>(key);

public long GetInt64(string key) => GetValue<long>(key);

public string GetString(string key) => GetValue<string>(key);

public T GetStruct<T>(string key)
where T : struct => GetValue<T>(key);

public uint GetUInt32(string key) => GetValue<uint>(key);

public ulong GetUInt64(string key) => GetValue<ulong>(key);

public UInt128 GetUInt128(string key) => GetValue<UInt128>(key);

public UInt256 GetUInt256(string key) => GetValue<UInt256>(key);

public bool IsContract(Address address) => IsContractResult;

public void SetAddress(string key, Address value) => AddOrReplace(key, value);

public void SetArray(string key, Array a) => AddOrReplace(key, a);

public void SetBool(string key, bool value) => AddOrReplace(key, value);

public void SetBytes(byte[] key, byte[] value)
{
throw new NotImplementedException();
}

public void SetBytes(string key, byte[] value) => AddOrReplace(key, value);

public void SetChar(string key, char value) => AddOrReplace(key, value);

public void SetInt32(string key, int value) => AddOrReplace(key, value);

public void SetInt64(string key, long value) => AddOrReplace(key, value);

public void SetString(string key, string value) => AddOrReplace(key, value);

public void SetStruct<T>(string key, T value)
where T : struct => AddOrReplace(key, value);

public void SetUInt32(string key, uint value) => AddOrReplace(key, value);

public void SetUInt64(string key, ulong value) => AddOrReplace(key, value);

public void SetUInt128(string key, UInt128 value) => AddOrReplace(key, value);

public void SetUInt256(string key, UInt256 value) => AddOrReplace(key, value);
}
}
17 changes: 17 additions & 0 deletions Mainnet/STOContract/STOContract.Tests/STOContract.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>STOContractTests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Moq" Version="4.12.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\STOContract\STOContract.csproj" />
</ItemGroup>
</Project>
Loading