Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: claim a claimable balance #7

Merged
merged 3 commits into from
Jan 27, 2025
Merged
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
3 changes: 0 additions & 3 deletions StellarAssetSdk.Tests/AssetIssuerTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using StellarDotnetSdk;

namespace StellarAssetSdk.Tests;

Expand All @@ -22,7 +20,6 @@ public async Task GenerateAccount()
{
var server = StellarAssetSdk.Server.Local();
var res = await server.GenerateAndFund();

Console.WriteLine(res.AccountId);
var account = await server.RpcServer.GetAccount(res.AccountId);
Console.WriteLine(account.SequenceNumber);
Expand Down
21 changes: 19 additions & 2 deletions StellarAssetSdk/AssetIssuer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Transaction = StellarDotnetSdk.Transactions.Transaction;
using Asset = StellarDotnetSdk.Assets.Asset;
using ClaimPredicate = StellarDotnetSdk.Claimants.ClaimPredicate;
using dotnetstandard_bip32;


namespace StellarAssetSdk;
Expand All @@ -26,7 +27,7 @@

public readonly Asset Eurcv = new Eurcv(sgIssuer);
public readonly Account SgIssuer = sgIssuer;
public readonly Account SgOperator = sgOperator;

Check warning on line 30 in StellarAssetSdk/AssetIssuer.cs

View workflow job for this annotation

GitHub Actions / pack_and_test

Parameter 'Account sgOperator' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

/*
# Example of issuing an asset
Expand Down Expand Up @@ -175,7 +176,23 @@
*/
public async Task<Transaction> ClaimClaimableBalanceTransaction(Account receiver, byte[] claimableBalanceId)
{
// TODO: use example above
throw new NotImplementedException("ClaimClaimableBalanceTransaction");
// Adding some zeros to match the real claimable balance id
var claimableIdHex = claimableBalanceId.ToStringHex();

var setTrustlineFlags =
new SetTrustlineFlagsOperation(Eurcv, SgOperator.KeyPair, AuthorizeTrustline, NoFlags);
var clearSetTrustlineFlags =
new SetTrustlineFlagsOperation(Eurcv, SgOperator.KeyPair, NoFlags, AuthorizeTrustline);
var claimClaimableBalance =
new ClaimClaimableBalanceOperation(claimableIdHex, receiver.MuxedAccount);

var tx = await server.NewTransactionBuilder(SgIssuer);

return tx
.SetFee(1000)
.AddOperation(setTrustlineFlags)
.AddOperation(claimClaimableBalance)
.AddOperation(clearSetTrustlineFlags)
.Build();
}
}
19 changes: 5 additions & 14 deletions StellarAssetSdk/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using dotnetstandard_bip32;
using StellarDotnetSdk.Accounts;
using StellarDotnetSdk.Xdr;

namespace StellarAssetSdk;

public class Runner
Expand Down Expand Up @@ -42,22 +41,14 @@ public static async Task Run(Server server, KeyPair sgIssuer, KeyPair sgOperator
res = await server.SubmitTransaction(createClaimableBalance);
var txRes = TransactionResult.Decode(new XdrDataInputStream(Convert.FromBase64String(res.ResultXdr!)))!;
var resRes = txRes.Result.Results[0]!;

// Look up the ID for the claimable balance to be used to claim it.
var balanceId = resRes.Tr.CreateClaimableBalanceResult.BalanceID.V0.InnerValue!;
Console.WriteLine(balanceId.ToStringHex());

// Claim Balance
// TODO: use // ClaimClaimableBalanceTransaction
try
{
var claimBalance = await assetIssuer.ClaimClaimableBalanceTransaction(sgOperatorAccount, balanceId);
}
catch (NotImplementedException e)
{
Console.WriteLine(e.Message);
}


// Claim the claimable balance from sgOperator
var claimBalanceTx = await assetIssuer.ClaimClaimableBalanceTransaction(sgOperatorAccount, balanceId);
claimBalanceTx.Sign(sgOperator);
res = await server.SubmitTransaction(claimBalanceTx);
}
}
Loading