Skip to content

Commit f2bf251

Browse files
authored
Merge pull request #145 from IOTA-NET/140-problem-with-claiming-outputs
Fix ClaimOutputs
2 parents 71a009f + e693707 commit f2bf251

File tree

11 files changed

+188
-12
lines changed

11 files changed

+188
-12
lines changed

csharp/IotaWalletNet/IotaWalletNet.Application/AccountContext/Commands/ClaimOutputs/ClaimOutputsCommandHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ public class ClaimOutputsCommandHandler : IRequestHandler<ClaimOutputsCommand, C
88
{
99
public async Task<ClaimOutputsResponse> Handle(ClaimOutputsCommand request, CancellationToken cancellationToken)
1010
{
11-
ClaimOutputsCommandMessage message = new ClaimOutputsCommandMessage(request.Username, request.OutputIds);
11+
ClaimOutputsCommandMessageData messageData = new ClaimOutputsCommandMessageData(request.OutputIds);
12+
ClaimOutputsCommandMessage message = new ClaimOutputsCommandMessage(request.Username, messageData);
1213
string messageJson = JsonConvert.SerializeObject(message);
1314

1415
RustBridgeGenericResponse genericResponse = await request.Account.SendMessageAsync(messageJson);

csharp/IotaWalletNet/IotaWalletNet.Application/AccountContext/Commands/ClaimOutputs/ClaimOutputsCommandMessage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace IotaWalletNet.Application.AccountContext.Commands.ClaimOutputs
44
{
5-
public class ClaimOutputsCommandMessage : AccountMessage<List<string>>
5+
public class ClaimOutputsCommandMessage : AccountMessage<ClaimOutputsCommandMessageData>
66
{
77
private const string METHOD_NAME = "claimOutputs";
8-
public ClaimOutputsCommandMessage(string username, List<string> outputIds)
9-
: base(username, METHOD_NAME, outputIds)
8+
public ClaimOutputsCommandMessage(string username, ClaimOutputsCommandMessageData messageData)
9+
: base(username, METHOD_NAME, messageData)
1010
{
1111

1212
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace IotaWalletNet.Application.AccountContext.Commands.ClaimOutputs
2+
{
3+
public class ClaimOutputsCommandMessageData
4+
{
5+
public ClaimOutputsCommandMessageData(List<string> outputIdsToClaim)
6+
{
7+
OutputIdsToClaim = outputIdsToClaim;
8+
}
9+
10+
public List<string> OutputIdsToClaim { get; set; }
11+
}
12+
}

csharp/IotaWalletNet/IotaWalletNet.Application/AccountContext/Queries/GetOutputsWithAdditionalUnlockConditions/GetOutputsWithAdditionalUnlockConditionsMessageData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ public class GetOutputsWithAdditionalUnlockConditionsMessageData
66
{
77
public GetOutputsWithAdditionalUnlockConditionsMessageData(OutputTypeToClaim outputTypeToClaim)
88
{
9-
OutputsToClaim = outputTypeToClaim;
9+
OutputsToClaim = outputTypeToClaim.ToString();
1010
}
1111

12-
public OutputTypeToClaim OutputsToClaim { get; set; }
12+
public string OutputsToClaim { get; set; }
1313
}
1414
}

csharp/IotaWalletNet/IotaWalletNet.Application/AccountContext/Queries/GetOutputsWithAdditionalUnlockConditions/GetOutputsWithAdditionalUnlockConditionsQuery.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ namespace IotaWalletNet.Application.AccountContext.Queries.GetOutputsWithAdditio
66
{
77
public class GetOutputsWithAdditionalUnlockConditionsQuery : IRequest<GetOutputsWithAdditionalUnlockConditionsResponse>
88
{
9+
private OutputTypeToClaim _outputsTypeToClaim;
10+
911
public GetOutputsWithAdditionalUnlockConditionsQuery(OutputTypeToClaim outputsTypeToClaim, string username, IAccount account)
1012
{
1113
OutputsTypeToClaim = outputsTypeToClaim;
1214
Username = username;
1315
Account = account;
1416
}
1517

16-
public OutputTypeToClaim OutputsTypeToClaim { get; set; }
18+
public OutputTypeToClaim OutputsTypeToClaim { get => _outputsTypeToClaim; set => _outputsTypeToClaim = value; }
1719

1820
public string Username { get; set; }
1921

csharp/IotaWalletNet/IotaWalletNet.Application/AccountContext/Queries/GetOutputsWithAdditionalUnlockConditions/GetOutputsWithAdditionalUnlockConditionsQueryHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public async Task<GetOutputsWithAdditionalUnlockConditionsResponse> Handle(GetOu
1414

1515
RustBridgeGenericResponse genericResponse = await request.Account.SendMessageAsync(messageJson);
1616
GetOutputsWithAdditionalUnlockConditionsResponse response = genericResponse.As<GetOutputsWithAdditionalUnlockConditionsResponse>()!;
17-
17+
1818
return response;
1919
}
2020
}

csharp/IotaWalletNet/IotaWalletNet.Domain/Common/Extensions/NumberExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ private static string PreprocessHexEncodedAmountString(string hexEncodedAmount)
3030
hexEncodedAmount = "0" + hexEncodedAmount;
3131

3232
return hexEncodedAmount;
33-
return new string(hexEncodedAmount.AsEnumerable().Reverse().ToArray());
3433
}
3534

3635
/// <summary>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using IotaWalletNet.Application.AccountContext.Queries.GetOutputsWithAdditionalUnlockConditions;
2+
using IotaWalletNet.Application.Common.Extensions;
3+
using IotaWalletNet.Application.Common.Interfaces;
4+
using IotaWalletNet.Domain.Common.Models.Coin;
5+
using IotaWalletNet.Domain.Common.Models.Output;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using static IotaWalletNet.Application.WalletContext.Queries.GetAccount.GetAccountQueryHandler;
8+
9+
namespace IotaWalletNet.Main.Examples.Outputs_and_Transactions.Claim_Outputs
10+
{
11+
public static class ClaimOutputsExample
12+
{
13+
public static async Task Run()
14+
{
15+
//Register all of the dependencies into a collection of services
16+
IServiceCollection services = new ServiceCollection().AddIotaWalletServices();
17+
18+
//Install services to service provider which is used for dependency injection
19+
IServiceProvider serviceProvider = services.BuildServiceProvider();
20+
21+
//Use serviceprovider to create a scope, which disposes of all services at end of scope
22+
using (IServiceScope scope = serviceProvider.CreateScope())
23+
{
24+
//Request IWallet service from service provider
25+
IWallet wallet = scope.ServiceProvider.GetRequiredService<IWallet>();
26+
27+
//Build wallet using a fluent-style configuration api
28+
wallet = wallet
29+
.ConfigureWalletOptions()
30+
.SetCoinType(TypeOfCoin.Shimmer)
31+
.SetStoragePath("./walletdb")
32+
.Then()
33+
.ConfigureClientOptions()
34+
.AddNodeUrl("https://api.testnet.shimmer.network")
35+
.SetFaucetUrl("https://faucet.testnet.shimmer.network")
36+
.IsFallbackToLocalPow()
37+
.IsLocalPow()
38+
.Then()
39+
.ConfigureSecretManagerOptions()
40+
.SetPassword("password")
41+
.SetSnapshotPath("./mystronghold")
42+
.Then()
43+
.Initialize();
44+
45+
46+
//Let's retrieve our cookiemonster account
47+
(GetAccountResponse accountResponse, IAccount? account) = await wallet.GetAccountAsync("cookiemonster");
48+
Console.WriteLine($"GetAccountAsync: {accountResponse}");
49+
50+
if (account == null)
51+
{
52+
Console.WriteLine("There was a problem retreiving the account.");
53+
return;
54+
}
55+
56+
//Sync account so that we can get the latest changes from the tangle
57+
await account.SyncAccountAsync();
58+
59+
// Get outputs with unlock conditions
60+
GetOutputsWithAdditionalUnlockConditionsResponse getOutputsWithAdditionalUnlockConditionsResponse =
61+
await account.GetOutputsWithAdditionalUnlockConditionsAsync(OutputTypeToClaim.All);
62+
63+
//Retrieve all their outputids
64+
List<string> outputIds = getOutputsWithAdditionalUnlockConditionsResponse.Payload!;
65+
66+
if(outputIds.Any())
67+
{
68+
await account.ClaimOutputsAsync(outputIds);
69+
}
70+
71+
}
72+
}
73+
74+
75+
}
76+
77+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Claim Outputs Example
2+
3+
## Code Example
4+
5+
The following example will:
6+
7+
1. Initialize your wallet
8+
2. Retrieve an account
9+
3. Sync account
10+
4. Check if there are any outputs with Additional Unlock Conditions
11+
5. Claim these outputs
12+
13+
```cs
14+
15+
public static class ClaimOutputsExample
16+
{
17+
public static async Task Run()
18+
{
19+
//Register all of the dependencies into a collection of services
20+
IServiceCollection services = new ServiceCollection().AddIotaWalletServices();
21+
22+
//Install services to service provider which is used for dependency injection
23+
IServiceProvider serviceProvider = services.BuildServiceProvider();
24+
25+
//Use serviceprovider to create a scope, which disposes of all services at end of scope
26+
using (IServiceScope scope = serviceProvider.CreateScope())
27+
{
28+
//Request IWallet service from service provider
29+
IWallet wallet = scope.ServiceProvider.GetRequiredService<IWallet>();
30+
31+
//Build wallet using a fluent-style configuration api
32+
wallet = wallet
33+
.ConfigureWalletOptions()
34+
.SetCoinType(TypeOfCoin.Shimmer)
35+
.SetStoragePath("./walletdb")
36+
.Then()
37+
.ConfigureClientOptions()
38+
.AddNodeUrl("https://api.testnet.shimmer.network")
39+
.SetFaucetUrl("https://faucet.testnet.shimmer.network")
40+
.IsFallbackToLocalPow()
41+
.IsLocalPow()
42+
.Then()
43+
.ConfigureSecretManagerOptions()
44+
.SetPassword("password")
45+
.SetSnapshotPath("./mystronghold")
46+
.Then()
47+
.Initialize();
48+
49+
50+
//Let's retrieve our cookiemonster account
51+
(GetAccountResponse accountResponse, IAccount? account) = await wallet.GetAccountAsync("cookiemonster");
52+
Console.WriteLine($"GetAccountAsync: {accountResponse}");
53+
54+
if (account == null)
55+
{
56+
Console.WriteLine("There was a problem retreiving the account.");
57+
return;
58+
}
59+
60+
//Sync account so that we can get the latest changes from the tangle
61+
await account.SyncAccountAsync();
62+
63+
// Get outputs with unlock conditions
64+
GetOutputsWithAdditionalUnlockConditionsResponse getOutputsWithAdditionalUnlockConditionsResponse =
65+
await account.GetOutputsWithAdditionalUnlockConditionsAsync(OutputTypeToClaim.All);
66+
67+
//Retrieve all their outputids
68+
List<string> outputIds = getOutputsWithAdditionalUnlockConditionsResponse.Payload!;
69+
70+
if(outputIds.Any())
71+
{
72+
await account.ClaimOutputsAsync(outputIds);
73+
}
74+
75+
}
76+
}
77+
78+
79+
}
80+
81+
82+
```

csharp/IotaWalletNet/IotaWalletNet.Main/Examples/Outputs and Transactions/Request Tokens from Faucet/RequestTokensFromFaucetExample.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ public static async Task Run()
6161

6262

6363
}
64+
6465
}

0 commit comments

Comments
 (0)