This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #394 from jopmiddelkamp/feature/fee_bump_result_su…
…pport Fix for missing fee bump result (issue 388)
- Loading branch information
Showing
7 changed files
with
299 additions
and
13 deletions.
There are no files selected for viewing
213 changes: 213 additions & 0 deletions
213
stellar-dotnet-sdk-test/operations/FeeBumpOperationTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using stellar_dotnet_sdk; | ||
using xdrSDK = stellar_dotnet_sdk.xdr; | ||
using System.Threading.Tasks; | ||
using stellar_dotnet_sdk.responses; | ||
using stellar_dotnet_sdk.responses.results; | ||
|
||
namespace stellar_dotnet_sdk_test.operations; | ||
|
||
[TestClass] | ||
public class FeeBumpOperationTest | ||
{ | ||
[TestMethod] | ||
public async Task TransactionResultSuccess() | ||
{ | ||
Network.UseTestNetwork(); | ||
using var server = new Server("https://horizon-testnet.stellar.org"); | ||
|
||
var sourceKeyPair = KeyPair.Random(); | ||
var sponsorKeyPair = KeyPair.Random(); | ||
|
||
await Task.WhenAll( | ||
server.TestNetFriendBot | ||
.FundAccount(sourceKeyPair.AccountId) | ||
.Execute(), | ||
server.TestNetFriendBot | ||
.FundAccount(sponsorKeyPair.AccountId) | ||
.Execute() | ||
); | ||
|
||
var sourceAccount = await server.Accounts.Account(sourceKeyPair.AccountId); | ||
|
||
var transactionBuilder = new TransactionBuilder(sourceAccount); | ||
|
||
var paymentOperationBuilder = new PaymentOperation.Builder( | ||
sponsorKeyPair, | ||
new AssetTypeNative(), | ||
"10" | ||
); | ||
transactionBuilder.AddOperation( | ||
paymentOperationBuilder.Build() | ||
); | ||
var transaction = transactionBuilder.Build(); | ||
transaction.Sign(sourceKeyPair); | ||
|
||
var feeBumpTransaction = TransactionBuilder.BuildFeeBumpTransaction( | ||
sponsorKeyPair, | ||
transaction | ||
); | ||
feeBumpTransaction.Sign(sponsorKeyPair); | ||
|
||
var response = await server.SubmitTransaction(feeBumpTransaction); | ||
|
||
Assert.IsTrue(response.IsSuccess()); | ||
Assert.IsFalse(string.IsNullOrEmpty(response.Hash)); | ||
Assert.IsInstanceOfType(response.Result, typeof(FeeBumpTransactionResultSuccess)); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TransactionResultFailed() | ||
{ | ||
Network.UseTestNetwork(); | ||
using var server = new Server("https://horizon-testnet.stellar.org"); | ||
|
||
var sourceKeyPair = KeyPair.Random(); | ||
var sponsorKeyPair = KeyPair.Random(); | ||
|
||
await Task.WhenAll( | ||
server.TestNetFriendBot | ||
.FundAccount(sourceKeyPair.AccountId) | ||
.Execute(), | ||
server.TestNetFriendBot | ||
.FundAccount(sponsorKeyPair.AccountId) | ||
.Execute() | ||
); | ||
|
||
var sourceAccount = await server.Accounts.Account(sourceKeyPair.AccountId); | ||
|
||
var transactionBuilder = new TransactionBuilder(sourceAccount); | ||
|
||
var paymentOperationBuilder = new PaymentOperation.Builder( | ||
sponsorKeyPair, | ||
new AssetTypeNative(), | ||
"100000" | ||
); | ||
transactionBuilder.AddOperation( | ||
paymentOperationBuilder.Build() | ||
); | ||
var transaction = transactionBuilder.Build(); | ||
transaction.Sign(sourceKeyPair); | ||
|
||
var feeBumpTransaction = TransactionBuilder.BuildFeeBumpTransaction( | ||
sponsorKeyPair, | ||
transaction | ||
); | ||
feeBumpTransaction.Sign(sponsorKeyPair); | ||
|
||
var response = await server.SubmitTransaction(feeBumpTransaction); | ||
|
||
Assert.IsFalse(response.IsSuccess()); | ||
|
||
var result = response.Result as FeeBumpTransactionResultFailed; | ||
Assert.IsNotNull(result); | ||
|
||
var innerResult = result.InnerResultPair.Result as TransactionResultFailed; | ||
Assert.IsNotNull(innerResult); | ||
Assert.IsInstanceOfType(innerResult.Results[0], typeof(PaymentUnderfunded)); | ||
} | ||
|
||
[TestMethod] | ||
public async Task InsufficientFee() | ||
{ | ||
Network.UseTestNetwork(); | ||
using var server = new Server("https://horizon-testnet.stellar.org"); | ||
|
||
var sourceKeyPair = KeyPair.Random(); | ||
var sponsorKeyPair = KeyPair.Random(); | ||
|
||
await Task.WhenAll( | ||
server.TestNetFriendBot | ||
.FundAccount(sourceKeyPair.AccountId) | ||
.Execute(), | ||
server.TestNetFriendBot | ||
.FundAccount(sponsorKeyPair.AccountId) | ||
.Execute() | ||
); | ||
|
||
var sourceAccount = await server.Accounts.Account(sourceKeyPair.AccountId); | ||
|
||
const uint maxFee = 2000000u; | ||
|
||
var transactionBuilder = new TransactionBuilder(sourceAccount); | ||
transactionBuilder.SetFee(maxFee); | ||
|
||
var paymentOperationBuilder = new PaymentOperation.Builder( | ||
sponsorKeyPair, | ||
new AssetTypeNative(), | ||
"1000" | ||
); | ||
transactionBuilder.AddOperation( | ||
paymentOperationBuilder.Build() | ||
); | ||
transactionBuilder.AddOperation( | ||
paymentOperationBuilder.Build() | ||
); | ||
var transaction = transactionBuilder.Build(); | ||
transaction.Sign(sourceKeyPair); | ||
|
||
var exception = Assert.ThrowsException<Exception>(() => { | ||
TransactionBuilder.BuildFeeBumpTransaction( | ||
sponsorKeyPair, | ||
transaction, | ||
maxFee / 2 | ||
); | ||
}); | ||
|
||
Assert.AreEqual($"Invalid fee, it should be at least {maxFee} stroops", exception.Message); | ||
} | ||
|
||
[TestMethod] | ||
public async Task SufficientFee() | ||
{ | ||
Network.UseTestNetwork(); | ||
using var server = new Server("https://horizon-testnet.stellar.org"); | ||
|
||
var sourceKeyPair = KeyPair.Random(); | ||
var sponsorKeyPair = KeyPair.Random(); | ||
|
||
await Task.WhenAll( | ||
server.TestNetFriendBot | ||
.FundAccount(sourceKeyPair.AccountId) | ||
.Execute(), | ||
server.TestNetFriendBot | ||
.FundAccount(sponsorKeyPair.AccountId) | ||
.Execute() | ||
); | ||
|
||
var sourceAccount = await server.Accounts.Account(sourceKeyPair.AccountId); | ||
|
||
const uint maxFee = 2000000u; | ||
|
||
var transactionBuilder = new TransactionBuilder(sourceAccount); | ||
transactionBuilder.SetFee(maxFee); | ||
|
||
var paymentOperationBuilder = new PaymentOperation.Builder( | ||
sponsorKeyPair, | ||
new AssetTypeNative(), | ||
"1000" | ||
); | ||
transactionBuilder.AddOperation( | ||
paymentOperationBuilder.Build() | ||
); | ||
transactionBuilder.AddOperation( | ||
paymentOperationBuilder.Build() | ||
); | ||
var transaction = transactionBuilder.Build(); | ||
transaction.Sign(sourceKeyPair); | ||
|
||
var feeBumpTransaction = TransactionBuilder.BuildFeeBumpTransaction( | ||
sponsorKeyPair, | ||
transaction, | ||
maxFee | ||
); | ||
feeBumpTransaction.Sign(sponsorKeyPair); | ||
|
||
var response = await server.SubmitTransaction(feeBumpTransaction); | ||
|
||
Assert.IsTrue(response.IsSuccess()); | ||
Assert.IsFalse(string.IsNullOrEmpty(response.Hash)); | ||
Assert.IsInstanceOfType(response.Result, typeof(FeeBumpTransactionResultSuccess)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
stellar-dotnet-sdk/responses/FeeBumpTransactionResultFailed.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace stellar_dotnet_sdk.responses; | ||
|
||
/// <summary> | ||
/// One of the operations in the inner transaction failed (none were applied). | ||
/// </summary> | ||
public class FeeBumpTransactionResultFailed : TransactionResult | ||
{ | ||
public override bool IsSuccess => false; | ||
|
||
public InnerTransactionResultPair InnerResultPair { get; set; } | ||
} |
11 changes: 11 additions & 0 deletions
11
stellar-dotnet-sdk/responses/FeeBumpTransactionResultSuccess.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace stellar_dotnet_sdk.responses; | ||
|
||
/// <summary> | ||
/// All operations in the inner transaction succeeded. | ||
/// </summary> | ||
public class FeeBumpTransactionResultSuccess : TransactionResult | ||
{ | ||
public override bool IsSuccess => true; | ||
|
||
public InnerTransactionResultPair InnerResultPair { get; set; } | ||
} |
31 changes: 31 additions & 0 deletions
31
stellar-dotnet-sdk/responses/InnerTransactionResultPair.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using stellar_dotnet_sdk.xdr; | ||
|
||
namespace stellar_dotnet_sdk.responses; | ||
|
||
public class InnerTransactionResultPair | ||
{ | ||
public string TransactionHash { get; set; } | ||
|
||
public TransactionResult Result { get; set; } | ||
|
||
public static InnerTransactionResultPair FromXdr(string encoded) | ||
{ | ||
byte[] bytes = Convert.FromBase64String(encoded); | ||
var result = xdr.InnerTransactionResultPair.Decode(new xdr.XdrDataInputStream(bytes)); | ||
return FromXdr(result); | ||
} | ||
|
||
public static InnerTransactionResultPair FromXdr(xdr.InnerTransactionResultPair result) | ||
{ | ||
var writer = new XdrDataOutputStream(); | ||
xdr.InnerTransactionResult.Encode(writer, result.Result); | ||
var xdrTransaction = Convert.ToBase64String(writer.ToArray()); | ||
|
||
return new InnerTransactionResultPair | ||
{ | ||
TransactionHash = Convert.ToBase64String(result.TransactionHash.InnerValue), | ||
Result = TransactionResult.FromXdr(xdrTransaction), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters