Skip to content

Commit

Permalink
Add LedgerKeyClaimableBalance constructor that accepts a string (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongph87 authored Oct 25, 2024
1 parent 023fd56 commit bb56963
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 30 deletions.
41 changes: 36 additions & 5 deletions StellarDotnetSdk.Tests/LedgerKeyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,53 @@ public void TestLedgerKeyTrustline()
Assert.AreEqual(keypair.AccountId, decodedLedgerKey.Account.AccountId);
}


[TestMethod]
public void TestLedgerKeyClaimableBalance()
public void TestLedgerKeyClaimableBalanceStringConstructorValid()
{
var balanceId = Util.HexToBytes("c582697b67cbec7f9ce64f4dc67bfb2bfd26318bb9f964f4d70e3f41f650b1e6");

const string balanceId = "d1d73327fc560cc09f54a11c7a64180611e1f480f3bf60117e41d19d9593b780";
var ledgerKey = LedgerKey.ClaimableBalance(balanceId);

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

// Assert
CollectionAssert.AreEqual(balanceId, decodedLedgerKey.BalanceId);
Assert.AreEqual("d1d73327fc560cc09f54a11c7a64180611e1f480f3bf60117e41d19d9593b780",
decodedLedgerKey.BalanceId.ToLower());
}

[TestMethod]
public void TestLedgerKeyClaimableBalanceStringConstructorInvalid()
{
const string balanceId = "00000000d1d73327fc560cc09f54a11c7a64180611e1f480f3bf60117e41d19d9593b780";
var ex = Assert.ThrowsException<ArgumentException>(() =>
LedgerKey.ClaimableBalance(balanceId));
Assert.IsTrue(ex.Message.Contains("Claimable balance ID cannot exceed 64 characters."));
}

[TestMethod]
public void TestLedgerKeyClaimableBalanceByteArrayConstructorValid()
{
const string balanceId = "d1d73327fc560cc09f54a11c7a64180611e1f480f3bf60117e41d19d9593b780";
var ledgerKey = LedgerKey.ClaimableBalance(Convert.FromHexString(balanceId));

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

// Assert
Assert.AreEqual(balanceId, decodedLedgerKey.BalanceId.ToLower());
}

[TestMethod]
public void TestLedgerKeyClaimableBalanceByteArrayConstructorInvalid()
{
const string balanceId = "00000000d1d73327fc560cc09f54a11c7a64180611e1f480f3bf60117e41d19d9593b780";
var ex = Assert.ThrowsException<ArgumentException>(() =>
LedgerKey.ClaimableBalance(Convert.FromHexString(balanceId)));
Assert.IsTrue(ex.Message.Contains("Claimable balance ID byte array must have exactly 32 bytes."));
}

[TestMethod]
public void TestLedgerKeyLiquidityPool()
{
Expand Down
6 changes: 3 additions & 3 deletions StellarDotnetSdk.Tests/Operations/OperationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -733,14 +733,14 @@ public void TestRevokeClaimableBalanceSponsorshipOperation()
{
var operation =
RevokeLedgerEntrySponsorshipOperation.ForClaimableBalance(
"00000000d1d73327fc560cc09f54a11c7a64180611e1f480f3bf60117e41d19d9593b780");
"d1d73327fc560cc09f54a11c7a64180611e1f480f3bf60117e41d19d9593b780");

var xdrOperation = operation.ToXdr();
var decodedOperation = (RevokeLedgerEntrySponsorshipOperation)Operation.FromXdr(xdrOperation);

Assert.IsNull(decodedOperation.SourceAccount);
CollectionAssert.AreEqual(((LedgerKeyClaimableBalance)operation.LedgerKey).BalanceId,
((LedgerKeyClaimableBalance)decodedOperation.LedgerKey).BalanceId);
Assert.AreEqual(((LedgerKeyClaimableBalance)operation.LedgerKey).BalanceId,
((LedgerKeyClaimableBalance)decodedOperation.LedgerKey).BalanceId.ToLower());
}

[TestMethod]
Expand Down
2 changes: 1 addition & 1 deletion StellarDotnetSdk.Tests/SorobanServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ public async Task TestGetLedgerEntriesOfTypeClaimableBalance()
Assert.IsNotNull(ledgerEntry);
Assert.IsNotNull(ledgerKey);
Assert.AreEqual("299a32106238f3b2d84d4142783fe320253bcda775d1bfb7accdb533021ddccf",
Convert.ToHexString(ledgerKey.BalanceId).ToLower());
ledgerKey.BalanceId.ToLower());
Assert.AreEqual(457593U, ledgerEntry.LastModifiedLedgerSeq);
Assert.AreEqual("native", ledgerEntry.Asset.Type);
Assert.AreEqual(200000000L, ledgerEntry.Amount);
Expand Down
10 changes: 10 additions & 0 deletions StellarDotnetSdk/LedgerKeys/LedgerKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public static LedgerKey ClaimableBalance(byte[] balanceId)
return new LedgerKeyClaimableBalance(balanceId);
}

/// <summary>Constructs a new <c>LedgerKeyClaimableBalance</c> from the given hex-encoded claimable balance ID.</summary>
/// <param name="balanceIdHexString">
/// Hex-encoded ID of the claimable balance entry.
/// For example: <c>d1d73327fc560cc09f54a11c7a64180611e1f480f3bf60117e41d19d9593b780</c>.
/// </param>
public static LedgerKey ClaimableBalance(string balanceIdHexString)
{
return new LedgerKeyClaimableBalance(balanceIdHexString);
}

public static LedgerKey Data(KeyPair account, string dataName)
{
return new LedgerKeyData(account, dataName);
Expand Down
32 changes: 21 additions & 11 deletions StellarDotnetSdk/LedgerKeys/LedgerKeyClaimableBalance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,35 @@ namespace StellarDotnetSdk.LedgerKeys;
public class LedgerKeyClaimableBalance : LedgerKey
{
/// <summary>
/// Constructs a <c>LedgerKeyClaimableBalance</c> object from a byte array.
/// Constructs a <c>LedgerKeyClaimableBalance</c> object from a 32-byte array.
/// </summary>
/// <param name="balanceId"></param>
public LedgerKeyClaimableBalance(byte[] balanceId)
/// <param name="balanceIdByteArray">Byte array representation of the claimable balance entry.</param>
public LedgerKeyClaimableBalance(byte[] balanceIdByteArray)
{
BalanceId = balanceId;
if (balanceIdByteArray.Length != 32)
{
throw new ArgumentException("Claimable balance ID byte array must have exactly 32 bytes.", nameof(balanceIdByteArray));
}
BalanceId = Convert.ToHexString(balanceIdByteArray);
}

/// <summary>
/// Constructs a <c>LedgerKeyClaimableBalance</c> object from a hex-encoded string.
/// Constructs a <c>LedgerKeyClaimableBalance</c> from given hex-encoded claimable balance ID.
/// </summary>
/// <param name="balanceId">Hex-encoded string of a claimable balance ID.</param>
public LedgerKeyClaimableBalance(string balanceId)
/// <param name="balanceIdHexString">
/// Hex-encoded ID of the claimable balance entry.
/// For example: <c>d1d73327fc560cc09f54a11c7a64180611e1f480f3bf60117e41d19d9593b780</c>.
/// </param>
public LedgerKeyClaimableBalance(string balanceIdHexString)
{
BalanceId = Convert.FromHexString(balanceId);
if (balanceIdHexString.Length > 64)
{
throw new ArgumentException("Claimable balance ID cannot exceed 64 characters.", nameof(balanceIdHexString));
}
BalanceId = balanceIdHexString;
}

// TODO: Considering changing this to string
public byte[] BalanceId { get; }
public string BalanceId { get; }

public override Xdr.LedgerKey ToXdr()
{
Expand All @@ -40,7 +50,7 @@ public override Xdr.LedgerKey ToXdr()
{
InnerValue = ClaimableBalanceIDType.ClaimableBalanceIDTypeEnum.CLAIMABLE_BALANCE_ID_TYPE_V0,
},
V0 = new Hash(BalanceId),
V0 = new Hash(Convert.FromHexString(BalanceId)),
},
},
};
Expand Down
6 changes: 3 additions & 3 deletions StellarDotnetSdk/Operations/ClaimClaimableBalanceOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public class ClaimClaimableBalanceOperation : Operation
/// <summary>
/// Constructs a new <c>ClaimClaimableBalanceOperation</c>.
/// </summary>
/// <param name="balanceId">The hex-encoded ID of the ClaimableBalanceEntry to be claimed.</param>
/// <param name="balanceIdHexString">The hex-encoded ID of the ClaimableBalanceEntry to be claimed.</param>
/// <param name="sourceAccount">(Optional) Source account of the operation.</param>
public ClaimClaimableBalanceOperation(string balanceId, IAccountId? sourceAccount = null) : this(
Util.HexToBytes(balanceId), sourceAccount)
public ClaimClaimableBalanceOperation(string balanceIdHexString, IAccountId? sourceAccount = null)
: this(Util.HexToBytes(balanceIdHexString), sourceAccount)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,16 @@ public static RevokeLedgerEntrySponsorshipOperation ForAccount(KeyPair account,
/// <summary>
/// Constructs new revoke balance entry sponsorship operation.
/// </summary>
/// <param name="balanceId">
/// <param name="balanceIdHexString">
/// Hex-encoded ID of the claimable balance entry to be revoked.
/// For example:
/// Either <c>00000000d1d73327fc560cc09f54a11c7a64180611e1f480f3bf60117e41d19d9593b780</c>
/// or
/// <c>d1d73327fc560cc09f54a11c7a64180611e1f480f3bf60117e41d19d9593b780</c> is accepted.
/// For example: <c>d1d73327fc560cc09f54a11c7a64180611e1f480f3bf60117e41d19d9593b780</c>.
/// </param>
public static RevokeLedgerEntrySponsorshipOperation ForClaimableBalance(
string balanceId,
string balanceIdHexString,
IAccountId? sourceAccount = null)
{
return new RevokeLedgerEntrySponsorshipOperation(
new LedgerKeyClaimableBalance(balanceId),
new LedgerKeyClaimableBalance(balanceIdHexString),
sourceAccount);
}

Expand Down

0 comments on commit bb56963

Please sign in to comment.