Skip to content

Commit

Permalink
Added LedgerEntry, SorobanResources and SorobanTransactionData classes (
Browse files Browse the repository at this point in the history
Beans-BV#3)

* Renamed class Signer to SignerUtil.
  • Loading branch information
cuongph87 authored Jan 19, 2024
1 parent 5e18722 commit 19531a7
Show file tree
Hide file tree
Showing 77 changed files with 4,634 additions and 1,226 deletions.
1,126 changes: 1,126 additions & 0 deletions stellar-dotnet-sdk-test/LedgerEntryTest.cs

Large diffs are not rendered by default.

204 changes: 163 additions & 41 deletions stellar-dotnet-sdk-test/LedgerKeyTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using stellar_dotnet_sdk;
using stellar_dotnet_sdk.xdr;
using LedgerKey = stellar_dotnet_sdk.LedgerKey;
using SCSymbol = stellar_dotnet_sdk.SCSymbol;

namespace stellar_dotnet_sdk_test
{
Expand All @@ -10,44 +14,53 @@ public class LedgerKeyTest
public void TestLedgerKeyAccount()
{
var keypair = KeyPair.FromAccountId("GCFRHRU5YRI3IN3IMRMYGWWEG2PX2B6MYH2RJW7NEDE2PTYPISPT3RU7");
var ledgerKey = LedgerKey.Account(keypair);
var xdr = ledgerKey.ToXdr();
var back = LedgerKey.FromXdr(xdr);

var account = (LedgerKeyAccount)back;

Assert.IsNotNull(account);
Assert.AreEqual(keypair.AccountId, account.Account.AccountId);
var ledgerKey = (LedgerKeyAccount)LedgerKey.Account(keypair);

// Act
var ledgerKeyXdrBase64 = ledgerKey.ToXdrBase64();
var decodedLedgerKey = (LedgerKeyAccount)LedgerKey.FromXdrBase64(ledgerKeyXdrBase64);

// Assert
Assert.AreEqual(ledgerKey.Account.AccountId, decodedLedgerKey.Account.AccountId);
}

[TestMethod]
public void TestLedgerKeyData()
public void TestLedgerKeyDataWithTooLongName()
{
var keypair = KeyPair.FromAccountId("GCFRHRU5YRI3IN3IMRMYGWWEG2PX2B6MYH2RJW7NEDE2PTYPISPT3RU7");
var ledgerKey = LedgerKey.Data(keypair, "Test Data");
var xdr = ledgerKey.ToXdr();
var back = LedgerKey.FromXdr(xdr);

var data = (LedgerKeyData)back;

Assert.IsNotNull(data);
Assert.AreEqual("Test Data", data.DataName);
Assert.AreEqual(keypair.AccountId, data.Account.AccountId);
var dataName = "This is a 73 characters long string which is too strong for String64 type";
var ex = Assert.ThrowsException<ArgumentException>(() => LedgerKey.Data(keypair, dataName));
Assert.IsTrue(ex.Message.Contains("String64 cannot exceed 64 characters"));
}

[TestMethod]
public void TestLedgerKeyDataWithValidName()
{
var keypair = KeyPair.FromAccountId("GCFRHRU5YRI3IN3IMRMYGWWEG2PX2B6MYH2RJW7NEDE2PTYPISPT3RU7");
var ledgerKey = (LedgerKeyData)LedgerKey.Data(keypair, "Test Data");

// Act
var ledgerKeyXdrBase64 = ledgerKey.ToXdrBase64();
var decodedLedgerKey = (LedgerKeyData)LedgerKey.FromXdrBase64(ledgerKeyXdrBase64);

// Assert
Assert.AreEqual(ledgerKey.DataName, decodedLedgerKey.DataName);
Assert.AreEqual(ledgerKey.Account.AccountId, decodedLedgerKey.Account.AccountId);
}

[TestMethod]
public void TestLedgerKeyOffer()
{
var keypair = KeyPair.FromAccountId("GCFRHRU5YRI3IN3IMRMYGWWEG2PX2B6MYH2RJW7NEDE2PTYPISPT3RU7");
var ledgerKey = LedgerKey.Offer(keypair, 1234);
var xdr = ledgerKey.ToXdr();
var back = LedgerKey.FromXdr(xdr);

var offer = (LedgerKeyOffer)back;

Assert.IsNotNull(offer);
Assert.AreEqual(1234, offer.OfferId);
Assert.AreEqual(keypair.AccountId, offer.Seller.AccountId);
var ledgerKey = (LedgerKeyOffer)LedgerKey.Offer(keypair, 1234);

// Act
var ledgerKeyXdrBase64 = ledgerKey.ToXdrBase64();
var decodedLedgerKey = (LedgerKeyOffer)LedgerKey.FromXdrBase64(ledgerKeyXdrBase64);
// Assert
Assert.AreEqual(ledgerKey.OfferId, decodedLedgerKey.OfferId);
Assert.AreEqual(ledgerKey.Seller.AccountId, decodedLedgerKey.Seller.AccountId);
}

[TestMethod]
Expand All @@ -57,14 +70,14 @@ public void TestLedgerKeyTrustline()
var issuer = KeyPair.FromAccountId("GB24C27VKWCBG7NTCT4J2L4MXJGYC3K3SQ4JOTCSPOVVEN7EZEB43XNE");
var asset = TrustlineAsset.CreateNonNativeAsset("ABCD", issuer.AccountId);
var ledgerKey = LedgerKey.Trustline(keypair, asset);
var xdr = ledgerKey.ToXdr();
var back = LedgerKey.FromXdr(xdr);

var trustline = (LedgerKeyTrustline)back;

Assert.IsNotNull(trustline);
Assert.AreEqual("ABCD:GB24C27VKWCBG7NTCT4J2L4MXJGYC3K3SQ4JOTCSPOVVEN7EZEB43XNE", ((TrustlineAsset.Wrapper)trustline.Asset).Asset.CanonicalName());
Assert.AreEqual(keypair.AccountId, trustline.Account.AccountId);

// Act
var ledgerKeyXdrBase64 = ledgerKey.ToXdrBase64();
var decodedLedgerKey = (LedgerKeyTrustline)LedgerKey.FromXdrBase64(ledgerKeyXdrBase64);

// Assert
Assert.AreEqual("ABCD:GB24C27VKWCBG7NTCT4J2L4MXJGYC3K3SQ4JOTCSPOVVEN7EZEB43XNE", ((TrustlineAsset.Wrapper)decodedLedgerKey.Asset).Asset.CanonicalName());
Assert.AreEqual(keypair.AccountId, decodedLedgerKey.Account.AccountId);
}


Expand All @@ -73,13 +86,122 @@ public void TestLedgerKeyClaimableBalance()
{
var balanceId = Util.HexToBytes("c582697b67cbec7f9ce64f4dc67bfb2bfd26318bb9f964f4d70e3f41f650b1e6");
var ledgerKey = LedgerKey.ClaimableBalance(balanceId);
var xdr = ledgerKey.ToXdr();
var back = LedgerKey.FromXdr(xdr);

// Act
var ledgerKeyXdrBase64 = ledgerKey.ToXdrBase64();
var decodedLedgerKey = (LedgerKeyClaimableBalance)LedgerKey.FromXdrBase64(ledgerKeyXdrBase64);

var claimableBalance = (LedgerKeyClaimableBalance)back;
// Assert
CollectionAssert.AreEqual(balanceId, decodedLedgerKey.BalanceId);
}

Assert.IsNotNull(claimableBalance);
Assert.AreEqual(balanceId, claimableBalance.BalanceId);
[TestMethod]
public void TestLedgerKeyLiquidityPool()
{
var hash = new byte[]
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2 };
var ledgerKey = LedgerKey.LiquidityPool(new LiquidityPoolID(hash));

// Act
var ledgerKeyXdrBase64 = ledgerKey.ToXdrBase64();
var decodedLedgerKey = (LedgerKeyLiquidityPool)LedgerKey.FromXdrBase64(ledgerKeyXdrBase64);

// Assert
CollectionAssert.AreEqual(hash, decodedLedgerKey.LiquidityPoolID.Hash);
}

[TestMethod]
public void TestLedgerKeyContractDataWithContractBeingContractId()
{
var contractId = new SCContractId("CAC2UYJQMC4ISUZ5REYB2AMDC44YKBNZWG4JB6N6GBL66CEKQO3RDSAB");
var key = new SCSymbol("kk");

var durability = ContractDataDurability.Create(ContractDataDurability.ContractDataDurabilityEnum.PERSISTENT);
var ledgerKey = (LedgerKeyContractData)LedgerKey.ContractData(contractId, key, durability);

// Act
var ledgerKeyXdrBase64 = ledgerKey.ToXdrBase64();
var decodedLedgerKey = (LedgerKeyContractData)LedgerKey.FromXdrBase64(ledgerKeyXdrBase64);

// Assert
Assert.AreEqual(contractId.InnerValue, ((SCContractId)decodedLedgerKey.Contract).InnerValue);
Assert.AreEqual(key.InnerValue, ((SCSymbol)decodedLedgerKey.Key).InnerValue);
Assert.AreEqual(ledgerKey.Durability.InnerValue, decodedLedgerKey.Durability.InnerValue);
}

[TestMethod]
public void TestLedgerKeyContractDataWithWithContractBeingAccountId()
{
var accountId = new SCAccountId("GCZFMH32MF5EAWETZTKF3ZV5SEVJPI53UEMDNSW55WBR75GMZJU4U573");
var key = new SCInt64(122);

var durability = ContractDataDurability.Create(ContractDataDurability.ContractDataDurabilityEnum.TEMPORARY);
var ledgerKey = (LedgerKeyContractData)LedgerKey.ContractData(accountId, key, durability);

// Act
var ledgerKeyXdrBase64 = ledgerKey.ToXdrBase64();
var decodedLedgerKey = (LedgerKeyContractData)LedgerKey.FromXdrBase64(ledgerKeyXdrBase64);

// Assert
Assert.AreEqual(accountId.InnerValue, ((SCAccountId)decodedLedgerKey.Contract).InnerValue);
Assert.AreEqual(key.InnerValue, ((SCInt64)decodedLedgerKey.Key).InnerValue);
Assert.AreEqual(ledgerKey.Durability.InnerValue, decodedLedgerKey.Durability.InnerValue);
}

[TestMethod]
public void TestLedgerKeyContractCodeCreationFromValidHashString()
{
var ledgerKey = (LedgerKeyContractCode)LedgerKey.ContractCode("AQIDBAUGBwgJAAECAwQFBgcICQABAgMEBQYHCAkAAQI=");

// Act
var ledgerKeyXdrBase64 = ledgerKey.ToXdrBase64();
var decodedLedgerKey = (LedgerKeyContractCode)LedgerKey.FromXdrBase64(ledgerKeyXdrBase64);

// Assert
CollectionAssert.AreEqual(ledgerKey.Hash.InnerValue, decodedLedgerKey.Hash.InnerValue);
}

[TestMethod]
public void TestLedgerKeyContractCodeCreationFromInvalidHashString()
{
var ex = Assert.ThrowsException<ArgumentException>(() => LedgerKey.ContractCode("AQIDBAUGBwgJAAECAwQFBgcICQABAgMEBQYHCAkAAg=="));
Assert.IsTrue(ex.Message.Contains("Hash must have exactly 32 bytes"));
}

[TestMethod]
public void TestLedgerKeyConfigSetting()
{
var ledgerKey = (LedgerKeyConfigSetting)LedgerKey.ConfigSetting(new ConfigSettingID()
{
InnerValue = ConfigSettingID.ConfigSettingIDEnum.CONFIG_SETTING_STATE_ARCHIVAL
});

// Act
var ledgerKeyXdrBase64 = ledgerKey.ToXdrBase64();
var decodedLedgerKey = (LedgerKeyConfigSetting)LedgerKey.FromXdrBase64(ledgerKeyXdrBase64);

// Assert
Assert.AreEqual(ledgerKey.ConfigSettingID.InnerValue, decodedLedgerKey.ConfigSettingID.InnerValue);
}

[TestMethod]
public void TestLedgerKeyTTLCreationFromValidHashString()
{
var ledgerKey = (LedgerKeyTTL)LedgerKey.TTL("AQIDBAUGBwgJAAECAwQFBgcICQABAgMEBQYHCAkAAQI=");

// Act
var ledgerKeyXdrBase64 = ledgerKey.ToXdrBase64();
var decodedLedgerKey = (LedgerKeyTTL)LedgerKey.FromXdrBase64(ledgerKeyXdrBase64);

// Assert
CollectionAssert.AreEqual(ledgerKey.Key.InnerValue, decodedLedgerKey.Key.InnerValue);
}

[TestMethod]
public void TestLedgerKeyTTLCreationFromInvalidHashString()
{
var ex = Assert.ThrowsException<ArgumentException>(() => LedgerKey.ContractCode("AQIDBAUGBwgJAAECAwQFBgcICQABAgMEBQYHCAkAAQIDBA=="));
Assert.IsTrue(ex.Message.Contains("Hash must have exactly 32 bytes"));
}
}
}
Loading

0 comments on commit 19531a7

Please sign in to comment.