Skip to content

Commit c87a478

Browse files
Feature/fee options (#116)
* Fee options api integration. Added basic demo page to make fee options request. Some refactoring to demo pages. Fixed a few namespaces * Decorate fee options return using the indexer so that it includes a bool specifying if the user has enough of a particular token to pay the fee * Updated SendTransactionFeeOptionsPage (used in the demo page) such that it sends the transaction with the first FeeOption in the user's wallet. This involved also updating the IWallet interface and implementation * Increment package version --------- Co-authored-by: Taylan Pince <[email protected]>
1 parent 112a83a commit c87a478

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+4762
-1181
lines changed

Assets/Samples~/DemoScene/Demo.unity

Lines changed: 4087 additions & 1048 deletions
Large diffs are not rendered by default.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Numerics;
3+
using Sequence.Utils;
4+
using Sequence.WaaS;
5+
using TMPro;
6+
using UnityEngine;
7+
8+
namespace Sequence.Demo
9+
{
10+
public abstract class DemoPage : UIPage
11+
{
12+
[SerializeField] protected TextMeshProUGUI _walletAddressText;
13+
[SerializeField] protected Chain _chain;
14+
15+
protected IWallet _wallet;
16+
private BigInteger _maxAmount = 0;
17+
18+
public override void Open(params object[] args)
19+
{
20+
base.Open(args);
21+
_wallet = args.GetObjectOfTypeIfExists<IWallet>();
22+
if (_wallet == default)
23+
{
24+
throw new SystemException(
25+
$"Invalid use. {GetType().Name} must be opened with a {typeof(IWallet)} as an argument");
26+
}
27+
28+
_walletAddressText.text = _wallet.GetWalletAddress();
29+
}
30+
}
31+
}

Assets/SequenceExamples/Scripts/UI/Demos/DemoPage.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using Sequence.Utils;
3+
4+
namespace Sequence.Demo
5+
{
6+
public abstract class DemoPanel : UIPanel
7+
{
8+
private TransitionPanel _transitionPanel;
9+
10+
public override void Open(params object[] args)
11+
{
12+
base.Open(args);
13+
_transitionPanel = args.GetObjectOfTypeIfExists<TransitionPanel>();
14+
if (_transitionPanel == default)
15+
{
16+
throw new SystemException(
17+
$"Invalid use. {GetType().Name} must be opened with a {typeof(TransitionPanel)} as an argument");
18+
}
19+
}
20+
21+
public override void Close()
22+
{
23+
base.Close();
24+
_transitionPanel.OpenWithDelay(_closeAnimationDurationInSeconds);
25+
}
26+
27+
public override void Back(params object[] injectAdditionalParams)
28+
{
29+
if (_pageStack.Count <= 1)
30+
{
31+
Close();
32+
}
33+
else
34+
{
35+
base.Back(injectAdditionalParams);
36+
}
37+
}
38+
}
39+
}

Assets/SequenceExamples/Scripts/UI/Demos/DemoPanel.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/SequenceExamples/Scripts/UI/Demos/SendTransactionPage.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,19 @@
99

1010
namespace Sequence.Demo
1111
{
12-
public class SendTransactionPage : UIPage
12+
public class SendTransactionPage : DemoPage
1313
{
14-
[SerializeField] private TextMeshProUGUI _walletAddressText;
1514
[SerializeField] private TMP_InputField _toAddressInputField;
1615
[SerializeField] private TMP_InputField _amountInputField;
1716
[SerializeField] private TextMeshProUGUI _transactionHashText;
1817
[SerializeField] private TextMeshProUGUI _sendText;
19-
[SerializeField] private Chain _chain;
2018

21-
private IWallet _wallet;
2219
private ChainIndexer _indexer;
2320
private BigInteger _maxAmount = 0;
2421

2522
public override void Open(params object[] args)
2623
{
2724
base.Open(args);
28-
_wallet = args.GetObjectOfTypeIfExists<IWallet>();
29-
if (_wallet == default)
30-
{
31-
throw new SystemException(
32-
$"Invalid use. {GetType().Name} must be opened with a {typeof(IWallet)} as an argument");
33-
}
34-
35-
_walletAddressText.text = _wallet.GetWalletAddress();
3625

3726
_wallet.OnSendTransactionComplete += OnTransactionSuccess;
3827
_wallet.OnSendTransactionFailed += OnTransactionFailed;

Assets/SequenceExamples/Scripts/UI/Demos/SendTransactionPanel.cs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,8 @@
66

77
namespace Sequence.Demo
88
{
9-
public class SendTransactionPanel : UIPanel
9+
public class SendTransactionPanel : DemoPanel
1010
{
11-
private TransitionPanel _transitionPanel;
12-
13-
public override void Open(params object[] args)
14-
{
15-
base.Open(args);
16-
_transitionPanel = args.GetObjectOfTypeIfExists<TransitionPanel>();
17-
if (_transitionPanel == default)
18-
{
19-
throw new SystemException(
20-
$"Invalid use. {GetType().Name} must be opened with a {typeof(TransitionPanel)} as an argument");
21-
}
22-
}
23-
24-
public override void Close()
25-
{
26-
base.Close();
27-
_transitionPanel.OpenWithDelay(_closeAnimationDurationInSeconds);
28-
}
29-
30-
public override void Back(params object[] injectAdditionalParams)
31-
{
32-
if (_pageStack.Count <= 1)
33-
{
34-
Close();
35-
}
36-
else
37-
{
38-
base.Back(injectAdditionalParams);
39-
}
40-
}
11+
4112
}
4213
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System;
2+
using System.Numerics;
3+
using System.Threading.Tasks;
4+
using Sequence.Utils;
5+
using Sequence.WaaS;
6+
using TMPro;
7+
using UnityEngine;
8+
9+
namespace Sequence.Demo
10+
{
11+
public class SendTransactionWithFeeOptionsPage : DemoPage
12+
{
13+
[SerializeField] private TMP_InputField _toAddressInputField;
14+
[SerializeField] private TMP_InputField _amountInputField;
15+
[SerializeField] private TextMeshProUGUI _transactionHashText;
16+
[SerializeField] private TextMeshProUGUI _sendText;
17+
18+
private ChainIndexer _indexer;
19+
private BigInteger _maxAmount = 0;
20+
21+
public override void Open(params object[] args)
22+
{
23+
base.Open(args);
24+
25+
_wallet.OnSendTransactionComplete += OnTransactionSuccess;
26+
_wallet.OnSendTransactionFailed += OnTransactionFailed;
27+
28+
_sendText.text = $"Send {ChainDictionaries.GasCurrencyOf[_chain]}";
29+
30+
_indexer = new ChainIndexer(_chain);
31+
GetMaxAmount();
32+
}
33+
34+
public override void Close()
35+
{
36+
base.Close();
37+
38+
_wallet.OnSendTransactionComplete -= OnTransactionSuccess;
39+
_wallet.OnSendTransactionFailed -= OnTransactionFailed;
40+
}
41+
42+
private void OnTransactionSuccess(SuccessfulTransactionReturn transactionReturn)
43+
{
44+
_transactionHashText.text = $"{ChainDictionaries.BlockExplorerOf[_chain]}tx/{transactionReturn.txHash}";
45+
}
46+
47+
private void OnTransactionFailed(FailedTransactionReturn transactionReturn)
48+
{
49+
Debug.LogError($"Transaction failed: {transactionReturn.error}");
50+
}
51+
52+
public void GetFeeOptions()
53+
{
54+
Address toAddress = GetAddress();
55+
string amount = DecimalNormalizer.Normalize(float.Parse(_amountInputField.text));
56+
57+
WaitForFeeOptionsAndSubmitFirstAvailable(toAddress, amount);
58+
}
59+
60+
private async Task WaitForFeeOptionsAndSubmitFirstAvailable(Address toAddress, string amount)
61+
{
62+
Transaction[] transactions = new Transaction[]
63+
{
64+
new RawTransaction(toAddress, amount)
65+
};
66+
FeeOptionsResponse response = await _wallet.GetFeeOptions(_chain, transactions);
67+
68+
int options = response.FeeOptions.Length;
69+
for (int i = 0; i < options; i++)
70+
{
71+
if (response.FeeOptions[i].InWallet)
72+
{
73+
await _wallet.SendTransactionWithFeeOptions(_chain, transactions, response.FeeOptions[i].FeeOption,
74+
response.FeeQuote);
75+
return;
76+
}
77+
}
78+
79+
Debug.LogError("The user does not have enough of the valid FeeOptions in their wallet");
80+
}
81+
82+
private Address GetAddress()
83+
{
84+
Address toAddress;
85+
try
86+
{
87+
toAddress = new Address(_toAddressInputField.text);
88+
}
89+
catch (Exception e)
90+
{
91+
Debug.LogError($"Invalid address: {_toAddressInputField.text}");
92+
return null;
93+
}
94+
95+
return toAddress;
96+
}
97+
98+
public void OpenBlockExplorer()
99+
{
100+
Application.OpenURL(_transactionHashText.text);
101+
}
102+
103+
private async Task GetMaxAmount()
104+
{
105+
EtherBalance ethBalance = await _indexer.GetEtherBalance(_wallet.GetWalletAddress());
106+
_maxAmount = ethBalance.balanceWei;
107+
}
108+
109+
public void Max()
110+
{
111+
_amountInputField.text = DecimalNormalizer.ReturnToNormalString(_maxAmount);
112+
}
113+
}
114+
}

Assets/SequenceExamples/Scripts/UI/Demos/SendTransactionWithFeeOptionsPage.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Sequence.Demo
2+
{
3+
public class SendTransactionWithFeeOptionsPanel : DemoPanel
4+
{
5+
6+
}
7+
}

0 commit comments

Comments
 (0)