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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

BRANCH | AZURE PIPELINES | TESTS | CODE COVERAGE
-------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------
MAIN | [![Build Status](https://dev.azure.com/portall-finance/priceContract/_apis/build/status/portall-finance.priceContract?branchName=main)](https://dev.azure.com/portall-finance/priceContract/_build/latest?definitionId=25&branchName=main) | [![Test Status](https://img.shields.io/azure-devops/tests/portall-finance/priceContract/2/main)](https://dev.azure.com/portall-finance/priceContract/_build/latest?definitionId=2&branchName=main) |
DEV | [![Build Status](https://dev.azure.com/portall-finance/priceContract/_apis/build/status/portall-finance.priceContract?branchName=dev)](https://dev.azure.com/portall-finance/priceContract/_build/latest?definitionId=25&branchName=dev) | [![Test Status](https://img.shields.io/azure-devops/tests/portall-finance/priceContract/2/dev)](https://dev.azure.com/portall-finance/priceContract/_build/latest?definitionId=2&branchName=dev) |
119 changes: 73 additions & 46 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
jobs:

# All tasks on Windows....
#- job: build_all_windows
# displayName: Build all tasks (Windows)
# pool:
# vmImage: windows-latest
# variables:
# CI_TEST: true
# steps:
# - task: UseDotNet@2
# displayName: 'Install .NET Core SDK'
# inputs:
# version: 3.1.102
#
# - task: BatchScript@1
# displayName: 'Download AElf build tools'
- job: build_all_windows
displayName: Build all tasks (Windows)
timeoutInMinutes: 120
pool:
vmImage: windows-latest
variables:
CI_TEST: true
steps:
- task: UseDotNet@2
displayName: 'Install .NET Core SDK'
inputs:
version: 6.0.x
- script: choco install protoc --version=3.19.4 -y
displayName: 'Install protobuf'
- script: choco install unzip
displayName: 'Install unzip'
- task: BatchScript@1
displayName: 'Download AElf build tools'
inputs:
filename: 'scripts/download_binary.bat'
- script: PowerShell.exe -file build.ps1 -target=Run-Unit-Tests
displayName: 'Build and Test'
- task: PublishTestResults@2
condition: always()
inputs:
testRunner: VSTest
testResultsFiles: '**/*.trx'

# - task: reportgenerator@5
# displayName: ReportGenerator
# inputs:
# filename: 'scripts/download_binary.bat'
# - script: PowerShell.exe -file scripts/install.ps1
# displayName: 'Install protobuf'
# - script: choco install unzip
# displayName: 'Install unzip'
# - powershell: Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
# - script: PowerShell.exe -file build.ps1 -target=Run-Unit-Tests
# displayName: 'Build and Test'
# reports: '$(Build.SourcesDirectory)/test/*/TestResults/*/coverage.cobertura.xml'
# targetdir: '$(Build.SourcesDirectory)/CodeCoverage'
# reporttypes: 'Cobertura'
# assemblyfilters: '-xunit*'
# - script: PowerShell.exe -file build.ps1 -target=Upload-Coverage-Azure
# displayName: 'Upload data to Codecov'
# All tasks on Linux
- job: build_all_linux
displayName: Build all tasks (Linux)
Expand All @@ -34,42 +47,56 @@ jobs:
- task: UseDotNet@2
displayName: 'Install .NET Core SDK'
inputs:
version: 3.1.102
version: 6.0.x
- script: bash scripts/install_protobuf.sh
- script: bash scripts/download_binary.sh
displayName: 'Download AElf build tools'
- script: bash scripts/install.sh
displayName: 'Install protobuf'
- script: bash build.sh --target=Test-with-Codecov
- script: bash build.sh -target=Test-with-Codecov
displayName: 'Build and Test'
- task: PublishTestResults@2
condition: always()
inputs:
testRunner: VSTest
testResultsFiles: '**/*.trx'
- task: Palmmedia.reportgenerator.reportgenerator-build-release-task.reportgenerator@4

- task: reportgenerator@5
displayName: ReportGenerator
inputs:
reports: '$(Build.SourcesDirectory)/test/*/TestResults/*/coverage.cobertura.xml'
targetdir: '$(Build.SourcesDirectory)/CodeCoverage'
reporttypes: 'Cobertura'
assemblyfilters: '-xunit*'
- script: bash build.sh --target=Upload-Coverage-Azure
- script: bash build.sh -target=Upload-Coverage-Azure
displayName: 'Upload data to Codecov'

# All tasks on macOS
#- job: build_all_darwin
# displayName: Build all tasks (macOS)
# pool:
# vmImage: macos-latest
# variables:
# CI_TEST: true
# steps:
# - task: UseDotNet@2
# displayName: 'Install .NET Core SDK'
- job: build_all_darwin
displayName: Build all tasks (macOS)
timeoutInMinutes: 120
pool:
vmImage: macos-latest
variables:
CI_TEST: true
steps:
- task: UseDotNet@2
displayName: 'Install .NET Core SDK'
inputs:
version: 6.0.x
- script: bash scripts/install_protobuf.sh
- script: bash scripts/download_binary.sh
- script: bash build.sh -target=Run-Unit-Tests
displayName: 'Build and Test'
- task: PublishTestResults@2
condition: always()
inputs:
testRunner: VSTest
testResultsFiles: '**/*.trx'

# - task: reportgenerator@5
# displayName: ReportGenerator
# inputs:
# version: 3.1.102
# - script: bash scripts/download_binary.sh
# displayName: 'Download AElf build tools'
# - script: bash scripts/install.sh
# displayName: 'Install protobuf'
# - script: bash build.sh -target=Run-Unit-Tests
# displayName: 'Build and Test'
# reports: '$(Build.SourcesDirectory)/test/*/TestResults/*/coverage.cobertura.xml'
# targetdir: '$(Build.SourcesDirectory)/CodeCoverage'
# reporttypes: 'Cobertura'
# assemblyfilters: '-xunit*'
# - script: bash build.sh -target=Upload-Coverage-Azure
# displayName: 'Upload data to Codecov'
27 changes: 24 additions & 3 deletions build.cake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#tool dotnet:?package=Codecov.Tool&version=1.13.0
#addin nuget:?package=Cake.Codecov&version=1.0.1
#tool nuget:?package=Codecov
#addin nuget:?package=Cake.Codecov&version=0.8.0

var target = Argument("target", "Default");
var configuration = Argument("configuration", "Debug");
Expand Down Expand Up @@ -71,11 +71,32 @@ Task("Test-with-Codecov")
DotNetCoreTest(testProject.FullPath, testSetting);
}
});
Task("Run-Unit-Tests")
.Description("operation test")
.IsDependentOn("Build")
.Does(() =>
{
var testSetting = new DotNetCoreTestSettings{
Configuration = configuration,
NoRestore = true,
NoBuild = true,
ArgumentCustomization = args => {
return args.Append("--logger trx");
}
};
var testProjects = GetFiles("./test/*.Tests/*.csproj");


foreach(var testProject in testProjects)
{
DotNetCoreTest(testProject.FullPath, testSetting);
}
});
Task("Upload-Coverage-Azure")
.Does(() =>
{
Codecov("./CodeCoverage/Cobertura.xml",EnvironmentVariable("CODECOV_TOKEN"));
Codecov("./CodeCoverage/Cobertura.xml","$CODECOV_TOKEN");
});

RunTarget(target);

4 changes: 2 additions & 2 deletions build.config
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash
CAKE_VERSION=1.3.0
DOTNET_VERSION=3.1.100
CAKE_VERSION=0.37.0
DOTNET_VERSION=6.0.300
5 changes: 4 additions & 1 deletion contract/AElf.Contracts.Price/AElf.Contracts.Price.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<!-- The root namespace -->
<!-- Mark this project as a contract -->
<IsContract>true</IsContract>
Expand All @@ -20,6 +20,9 @@
<ContractReference Include="..\..\protobuf\oracle_contract.proto">
<Link>Protobuf\Proto\oracle_contract.proto</Link>
</ContractReference>
<ContractReference Include="..\..\protobuf\awaken_swap_contract.proto">
<Link>Protobuf\Proto\awaken_swap_contract.proto</Link>
</ContractReference>
<CommonMessage Include="..\..\protobuf\callback_input.proto">
<Link>Protobuf\Proto\callback_input.proto</Link>
</CommonMessage>
Expand Down
12 changes: 12 additions & 0 deletions contract/AElf.Contracts.Price/PriceContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public override Empty Initialize(InitializeInput input)
? input.UnderlyingTokenSymbol
: DefaultUnderlyingTokenSymbol;
InitializeSwapUnderlyingToken(State.UnderlyingTokenSymbol.Value);
Assert(input.TokenSwapAddress != null && input.TokenSwapAddress != new Address(),
"Invalid token swap address");
State.TokenSwapAddress.Value = input.TokenSwapAddress;
return new Empty();
}

Expand Down Expand Up @@ -229,6 +232,15 @@ public override Empty SetUnderlyingToken(SetUnderlyingTokenInput input)
return new Empty();
}

public override Empty SetTokenSwapAddress(Address input)
{
CheckSenderIsController();
Assert(input != null && input != new Address(),
"Invalid token swap address");
State.TokenSwapAddress.Value = input;
return new Empty();
}

private string GetTokenKey(string token1, string token2, out bool isAdjustOrder)
{
isAdjustOrder = false;
Expand Down
1 change: 1 addition & 0 deletions contract/AElf.Contracts.Price/PriceContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public class PriceContractState : ContractState
public MappedState<string, Price> SwapTokenPriceInfo { get; set; }
public MappedState<Address, string, Price> ExchangeTokenPriceInfo { get; set; }
public StringState UnderlyingTokenSymbol { get; set; }
public SingletonState<Address> TokenSwapAddress { get; set; }
}
}
2 changes: 2 additions & 0 deletions contract/AElf.Contracts.Price/PriceContract_Constant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public partial class PriceContract
private const int Payment = 10_000_000;
private const int AggregatorOption = 2;
private const int MaxTracePathLimit = 3;

public const long Mantissa = 1000000000000000000L;
}
}
5 changes: 5 additions & 0 deletions contract/AElf.Contracts.Price/PriceContract_TraceSwapToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,10 @@ private string GetPriceReciprocalStr(string price)
{
return decimal.Round(GetPriceReciprocal(price), PriceDecimals).ToString();
}

private string GetPriceWithDecimal(decimal price)
{
return decimal.Round(price, PriceDecimals).ToString();
}
}
}
63 changes: 55 additions & 8 deletions contract/AElf.Contracts.Price/PriceContract_Views.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using AElf.Contracts.MultiToken;
using AElf.CSharp.Core;
using AElf.Types;
using Awaken.Contracts.Swap;
using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;

namespace AElf.Contracts.Price
Expand Down Expand Up @@ -67,21 +71,40 @@ public override Price GetExchangeTokenPriceInfo(GetExchangeTokenPriceInfoInput i
{
return new Price
{
Value = "1"
Value = Mantissa.ToString()
};
}

var tokenKey = GetTokenKey(input.TokenSymbol, input.TargetTokenSymbol, out var isReverse);
var priceInfo = State.ExchangeTokenPriceInfo[input.Organization][tokenKey];
if (!isReverse)
var reservesInput = new GetReservesInput
{
SymbolPair = { $"{input.TokenSymbol}-{input.TargetTokenSymbol}" }
}.ToByteString();
var tokenReserves = Context.Call<GetReservesOutput>(Context.Self, State.TokenSwapAddress.Value,
nameof(AwakenSwapContractContainer.AwakenSwapContractReferenceState.GetReserves), reservesInput);
Assert(tokenReserves.Results.Count == 1,
$"Token Pair does not exist: {input.TokenSymbol}-{input.TargetTokenSymbol}");
var tokenPair = tokenReserves.Results[0];
var tokenADecimals = State.TokenContract.GetTokenInfo.Call(new GetTokenInfoInput
{
Symbol = tokenPair.SymbolA
}).Decimals;
var tokenBDecimals = State.TokenContract.GetTokenInfo.Call(new GetTokenInfoInput
{
return priceInfo;
Symbol = tokenPair.SymbolB
}).Decimals;
if (input.TargetTokenSymbol == tokenPair.SymbolA)
{
return new Price
{
Timestamp = new Timestamp(),
Value = GetMantissaPrice(tokenPair.ReserveA, tokenPair.ReserveB, tokenADecimals, tokenBDecimals)
};
}

return new Price
{
Timestamp = priceInfo.Timestamp,
Value = GetPriceReciprocalStr(priceInfo.Value)
Timestamp = new Timestamp(),
Value = GetMantissaPrice(tokenPair.ReserveB, tokenPair.ReserveA, tokenBDecimals, tokenADecimals)
};
}

Expand Down Expand Up @@ -132,6 +155,11 @@ public override UnderlyingToken GetUnderlyingToken(Empty input)
};
}

public override Address GetTokenSwapAddress(Empty input)
{
return State.TokenSwapAddress.Value;
}

public override PriceTraceInfo GetSwapTokenInfo(GetSwapTokenInfoInput input)
{
return State.SwapTokenTraceInfo[input.Token];
Expand All @@ -154,5 +182,24 @@ public override QueryFee GetQueryFee(Empty input)
Fee = State.QueryFee.Value
};
}

public static string GetMantissaPrice(BigIntValue tokenReserve, BigIntValue targetTokenReserve, int tokenDecimals,
int reserveTokenDecimals)
{
var price = tokenReserve.Mul(GetDecimalMultiplier(reserveTokenDecimals)).Mul(Mantissa)
.Div(GetDecimalMultiplier(tokenDecimals)).Div(targetTokenReserve);
return price.Value;
}

public static long GetDecimalMultiplier(int decimals)
{
long multiplier = 1;
while (decimals -- > 0)
{
multiplier *= 10;
}

return multiplier;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
Expand Down
2 changes: 1 addition & 1 deletion contract/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<PackageReference Include="AElf.Sdk.CSharp" Version="1.0.0" />
<PackageReference Include="AElf.Sdk.CSharp" Version="1.2.0" />
</ItemGroup>
<Import Project="..\AElf.Contract.Tools.targets" />
</Project>
Loading