Skip to content

Commit

Permalink
Upgraded to .NET 8 with additional improvements (#15)
Browse files Browse the repository at this point in the history
- Upgraded to .NET 8 and C# 12.
- Updated names (namespaces, classes, variables, directories,..) to comply with C# standard.
- Reorganized classes.
- Refactored operation response classes.
- Refactored effect response classes.
- Refactored Result classes.
- Added missing result classes.
- Improved operation classes.
- Refactored JSON converters.
- Removed Operation builder classes.
- Updated XDR schemes to latest version.
- Refactored ClaimAtom classes and added missing unit tests.
- Refactored utility classes in Tests project.
  • Loading branch information
cuongph87 authored May 13, 2024
1 parent 58ef92b commit d65260b
Show file tree
Hide file tree
Showing 1,617 changed files with 35,584 additions and 39,264 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/pack_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ on:
types:
- opened
- synchronize
branches:
- feature/**
env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
DOTNET_NOLOGO: true
Expand Down
86 changes: 86 additions & 0 deletions StellarDotnetSdk.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Threading.Tasks;
using StellarDotnetSdk.Accounts;
using StellarDotnetSdk.Operations;
using StellarDotnetSdk.Transactions;
using SysConsole = System.Console;

namespace StellarDotnetSdk.Console;

public static class Program
{
private const int DefaultFee = 1000000;

public static async Task Main(string[] args)
{
// Network.UsePublicNetwork();
using var server = new Server("https://horizon-testnet.stellar.org");

var paymentsWithoutTransactions = await server.Payments.Execute().ConfigureAwait(false);
var paymentsWithTransactions = await server.Payments.IncludeTransaction().Execute().ConfigureAwait(false);

await CreateAccount(server).ConfigureAwait(false);

SysConsole.ReadLine();
}

private static async Task CreateAccount(Server server)
{
var source = KeyPair.FromSecretSeed("SDR4PTKMR5TAQQCL3RI2MLXXSXQDIR7DCAONQNQP6UCDZCD4OVRWXUHI");
SysConsole.WriteLine("Source account: {TO_BE_CONFIGURED}");
var destination = KeyPair.Random();
SysConsole.WriteLine("Destination account: " + destination.AccountId);

var sourceAccount = await server.Accounts.Account(source.AccountId).ConfigureAwait(false);
var transaction = new TransactionBuilder(sourceAccount)
.SetFee(DefaultFee)
.AddOperation(new CreateAccountOperation(destination, "1"))
.Build();

transaction.Sign(source);

var response = await server.SubmitTransaction(transaction).ConfigureAwait(false);
if (response.IsSuccess)
{
SysConsole.WriteLine("Create account response: " + response.Hash);
await DeleteAccount(server, destination, source).ConfigureAwait(false);
}
else
{
SysConsole.WriteLine("Create account failed.");
SysConsole.WriteLine("TransactionResultCode: " +
response.SubmitTransactionResponseExtras.ExtrasResultCodes.TransactionResultCode ?? "null");
SysConsole.WriteLine("TransactionResultCodeOperations: " + string.Join(", ",
response.SubmitTransactionResponseExtras.ExtrasResultCodes.OperationsResultCodes));
}
}

private static async Task DeleteAccount(Server server, KeyPair source, KeyPair destination)
{
var accountToDelete = await server.Accounts.Account(source.AccountId).ConfigureAwait(false);
var transaction = new TransactionBuilder(accountToDelete)
.SetFee(DefaultFee)
.AddOperation(new AccountMergeOperation(destination))
.Build();

transaction.Sign(source);

var feeBumpTransaction = TransactionBuilder.BuildFeeBumpTransaction(destination, transaction, DefaultFee);

feeBumpTransaction.Sign(destination);

var response = await server.SubmitTransaction(feeBumpTransaction).ConfigureAwait(false);
if (response.IsSuccess)
{
SysConsole.WriteLine("Delete account response: " + response.Hash);
}
else
{
SysConsole.WriteLine("Delete account failed.");
SysConsole.WriteLine("TransactionResultCode: " +
response.SubmitTransactionResponseExtras.ExtrasResultCodes.TransactionResultCode);
SysConsole.WriteLine("TransactionResultCodeOperations: " + string.Join(", ",
response.SubmitTransactionResponseExtras.ExtrasResultCodes.OperationsResultCodes));
}
}
}
21 changes: 21 additions & 0 deletions StellarDotnetSdk.Console/StellarDotnetSdk.Console.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<AssemblyName>StellarDotnetSdk.Console</AssemblyName>
<RootNamespace>StellarDotnetSdk.Console</RootNamespace>
<LangVersion>12</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\StellarDotnetSdk\StellarDotnetSdk.csproj"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="NSec.Cryptography" Version="22.4.0"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using stellar_dotnet_sdk;

namespace stellar_dotnet_sdk_test;

[TestClass]
public class AccountFlagTest
{
[TestMethod]
public void TestValues()
{
Assert.AreEqual(1, (int)AccountFlag.AUTH_REQUIRED_FLAG);
Assert.AreEqual(2, (int)AccountFlag.AUTH_REVOCABLE_FLAG);
Assert.AreEqual(4, (int)AccountFlag.AUTH_IMMUTABLE_FLAG);
Assert.AreEqual(8, (int)AccountFlag.AUTH_CLAWBACK_FLAG);
}
using Microsoft.VisualStudio.TestTools.UnitTesting;
using StellarDotnetSdk.Accounts;

namespace StellarDotnetSdk.Tests.Accounts;

[TestClass]
public class AccountFlagTest
{
[TestMethod]
public void TestValues()
{
Assert.AreEqual(1, (int)AccountFlag.AUTH_REQUIRED_FLAG);
Assert.AreEqual(2, (int)AccountFlag.AUTH_REVOCABLE_FLAG);
Assert.AreEqual(4, (int)AccountFlag.AUTH_IMMUTABLE_FLAG);
Assert.AreEqual(8, (int)AccountFlag.AUTH_CLAWBACK_FLAG);
}
}
Original file line number Diff line number Diff line change
@@ -1,69 +1,68 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using stellar_dotnet_sdk;

namespace stellar_dotnet_sdk_test;

[TestClass]
public class AccountTest
{
[TestMethod]
public void TestNullArguments()
{
Assert.ThrowsException<ArgumentNullException>(() => new Account((string)null, 10L));
Assert.ThrowsException<ArgumentNullException>(() => new Account(KeyPair.Random().AccountId, null));
}

[TestMethod]
public void TestWithStringArgumentIsKeyPair()
{
var keypair = KeyPair.Random();
var account = new Account(keypair.Address, 7);
Assert.AreEqual(account.AccountId, keypair.AccountId);
Assert.AreEqual(account.KeyPair.AccountId, keypair.AccountId);
}

[TestMethod]
public void TestWithMuxedAccount()
{
var keypair = KeyPair.Random();
var muxed = new MuxedAccountMed25519(keypair, 10);
var account = new Account(muxed, 7);
Assert.AreNotEqual(account.AccountId, keypair.AccountId);
Assert.AreEqual(account.AccountId, muxed.AccountId);
Assert.AreEqual(account.KeyPair.AccountId, keypair.AccountId);
}

[TestMethod]
public void TestGetIncrementedSequenceNumber()
{
var random = KeyPair.Random();
var account = new Account(random.AccountId, 100L);
long incremented;
incremented = account.IncrementedSequenceNumber;
Assert.AreEqual(100L, account.SequenceNumber);
Assert.AreEqual(101L, incremented);
incremented = account.IncrementedSequenceNumber;
Assert.AreEqual(100L, account.SequenceNumber);
Assert.AreEqual(101L, incremented);
}

[TestMethod]
public void TestIncrementSequenceNumber()
{
var random = KeyPair.Random();
var account = new Account(random.AccountId, 100L);
account.IncrementSequenceNumber();
Assert.AreEqual(account.SequenceNumber, 101L);
}

[TestMethod]
public void TestGetters()
{
var keypair = KeyPair.Random();
var account = new Account(keypair.AccountId, 100L);
Assert.AreEqual(account.KeyPair.AccountId, keypair.AccountId);
Assert.AreEqual(account.AccountId, keypair.AccountId);
Assert.AreEqual(account.SequenceNumber, 100L);
}
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using StellarDotnetSdk.Accounts;

namespace StellarDotnetSdk.Tests.Accounts;

[TestClass]
public class AccountTest
{
[TestMethod]
public void TestNullArguments()
{
Assert.ThrowsException<ArgumentNullException>(() => new Account((string)null, 10L));

Check warning on line 13 in StellarDotnetSdk.Tests/Accounts/AccountTest.cs

View workflow job for this annotation

GitHub Actions / pack_and_test

Converting null literal or possible null value to non-nullable type.

Check warning on line 13 in StellarDotnetSdk.Tests/Accounts/AccountTest.cs

View workflow job for this annotation

GitHub Actions / pack_and_test

Cannot convert null literal to non-nullable reference type.
Assert.ThrowsException<ArgumentNullException>(() => new Account(KeyPair.Random().AccountId, null));
}

[TestMethod]
public void TestWithStringArgumentIsKeyPair()
{
var keypair = KeyPair.Random();
var account = new Account(keypair.Address, 7);
Assert.AreEqual(account.AccountId, keypair.AccountId);
Assert.AreEqual(account.KeyPair.AccountId, keypair.AccountId);
}

[TestMethod]
public void TestWithMuxedAccount()
{
var keypair = KeyPair.Random();
var muxed = new MuxedAccountMed25519(keypair, 10);
var account = new Account(muxed, 7);
Assert.AreNotEqual(account.AccountId, keypair.AccountId);
Assert.AreEqual(account.AccountId, muxed.AccountId);
Assert.AreEqual(account.KeyPair.AccountId, keypair.AccountId);
}

[TestMethod]
public void TestGetIncrementedSequenceNumber()
{
var random = KeyPair.Random();
var account = new Account(random.AccountId, 100L);
var incremented = account.IncrementedSequenceNumber;
Assert.AreEqual(100L, account.SequenceNumber);
Assert.AreEqual(101L, incremented);
incremented = account.IncrementedSequenceNumber;
Assert.AreEqual(100L, account.SequenceNumber);
Assert.AreEqual(101L, incremented);
}

[TestMethod]
public void TestIncrementSequenceNumber()
{
var random = KeyPair.Random();
var account = new Account(random.AccountId, 100L);
account.IncrementSequenceNumber();
Assert.AreEqual(account.SequenceNumber, 101L);
}

[TestMethod]
public void TestGetters()
{
var keypair = KeyPair.Random();
var account = new Account(keypair.AccountId, 100L);
Assert.AreEqual(account.KeyPair.AccountId, keypair.AccountId);
Assert.AreEqual(account.AccountId, keypair.AccountId);
Assert.AreEqual(account.SequenceNumber, 100L);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using dotnetstandard_bip32;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using stellar_dotnet_sdk;
using StellarDotnetSdk.Accounts;

namespace stellar_dotnet_sdk_test;
namespace StellarDotnetSdk.Tests.Accounts;

[TestClass]
public class KeyPairBip39Tests
Expand Down
Loading

0 comments on commit d65260b

Please sign in to comment.