Skip to content

Commit

Permalink
Feature/waa s integration new api (#42)
Browse files Browse the repository at this point in the history
* Rename RegisterSessionPayload to WaaSPayload

* Send sign message intent to WaaS.

* Removed unused class.

* Setup for sending transactions using WaaS intents

* Put waas transaction arg parameters in alphabetical order as the API expects

* Rename SendTransactionReturn to SuccessfulTransactionReturn. Added FailedTransactionReturn

* Send transaction intent to WaaS. Demo sending successful transaction

* Handle failed transactions

* Drop WaaS session

* Refactor to re-use more code when sending messages to WaaS. Fix unmarshalling of DropSessionReturn

* Added DecimalNormalizer to make it easier to convert from human readable values to Eth values (adjusting for the decimal)

* Send erc20 transfer using WaaS

* Send ERC721 and send multiple transaction types at once

* Use adapter to generate a RawTransaction that transfers an ERC721 token

* Generate multiple RawTransactions using the adapter and batch them together for transactions (3 different implementations)

* Added custom json converter for DelayedEncodeData so that we can send delayedEncode transactions

* List WaaS Sessions

* Fixed canonical ordering for parameter types so that transactions go through.

* Remove todos that are no longer needed as they are done
  • Loading branch information
BellringerQuinn authored Jan 4, 2024
1 parent 4b3fbd0 commit 19d9d04
Show file tree
Hide file tree
Showing 73 changed files with 7,408 additions and 2,263 deletions.
8,014 changes: 6,017 additions & 1,997 deletions Assets/SequenceExamples/Scenes/Demo.unity

Large diffs are not rendered by default.

22 changes: 18 additions & 4 deletions Assets/SequenceExamples/Scripts/UI/LoginPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class LoginPanel : UIPanel
private LoginPage _loginPage;
private MultifactorAuthenticationPage _mfaPage;
private LoginSuccessPage _loginSuccessPage;
private WaaSDemoPage _waasDemoPage;

protected override void Awake()
{
base.Awake();
Expand All @@ -20,6 +22,8 @@ protected override void Awake()
_connectPage = GetComponentInChildren<ConnectPage>();
_loginPage = GetComponentInChildren<LoginPage>();
_mfaPage = GetComponentInChildren<MultifactorAuthenticationPage>();

_waasDemoPage = FindObjectOfType<WaaSDemoPage>();

ILogin loginHandler = new WaaSLogin(new AWSConfig(
"us-east-2",
Expand All @@ -34,12 +38,17 @@ protected override void Awake()
public void SetupLoginHandler(ILogin loginHandler)
{
_loginPage.SetupLogin(loginHandler);
_loginPage.LoginHandler.OnMFAEmailSent += OnMFAEmailSentHandler;
_loginPage.LoginHandler.OnMFAEmailFailedToSend += OnMFAEmailFailedToSendHandler;
loginHandler.OnMFAEmailSent += OnMFAEmailSentHandler;
loginHandler.OnMFAEmailFailedToSend += OnMFAEmailFailedToSendHandler;

_mfaPage.SetupLogin(loginHandler);
_mfaPage.LoginHandler.OnLoginSuccess += OnLoginSuccessHandler;
_mfaPage.LoginHandler.OnLoginFailed += OnLoginFailedHandler;
loginHandler.OnLoginSuccess += OnLoginSuccessHandler;
loginHandler.OnLoginFailed += OnLoginFailedHandler;

if (loginHandler is WaaSLogin waaSLogin)
{
waaSLogin.OnWaaSWalletCreated += OnWaaSWalletCreatedHandler;
}
}

public void OpenTransitionPanel()
Expand Down Expand Up @@ -86,5 +95,10 @@ private static void PassDeepLinkViaLocalServer()
}
}
#endif

private void OnWaaSWalletCreatedHandler(WaaSWallet wallet)
{
_waasDemoPage.Open(wallet);
}
}
}
271 changes: 271 additions & 0 deletions Assets/SequenceExamples/Scripts/UI/WaaSDemoPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
using System;
using System.ComponentModel;
using System.Numerics;
using System.Threading.Tasks;
using Sequence.Contracts;
using Sequence.Provider;
using Sequence.Transactions;
using Sequence.Utils;
using Sequence.WaaS;
using Sequence.WaaS.Authentication;
using SequenceSDK.Ethereum.Utils;
using SequenceSDK.WaaS;
using TMPro;
using UnityEngine;
using IWallet = Sequence.Wallet.IWallet;

namespace Sequence.Demo
{
public class WaaSDemoPage : UIPage
{
[SerializeField] private TextMeshProUGUI _resultText;

private WaaSWallet _wallet;
private Address _address;
private IWallet _adapter;

public override void Open(params object[] args)
{
_wallet =
args.GetObjectOfTypeIfExists<WaaSWallet>();
if (_wallet == default)
{
throw new SystemException(
$"Invalid use. {GetType().Name} must be opened with a {typeof(WaaSWallet)} as an argument");
}
_gameObject.SetActive(true);
_animator.AnimateIn( _openAnimationDurationInSeconds);

SetAddress();

_wallet.OnSignMessageComplete += OnSignMessageComplete;
_wallet.OnSendTransactionComplete += OnSuccessfulTransaction;
_wallet.OnSendTransactionFailed += OnFailedTransaction;
_wallet.OnDropSessionComplete += OnDropSessionComplete;

CreateAdapter();
}

private async Task CreateAdapter()
{
_adapter = await WaaSToWalletAdapter.CreateAsync(_wallet);
}

private async Task SetAddress()
{
var addressReturn = await _wallet.GetWalletAddress(new GetWalletAddressArgs(0));
_address = new Address(addressReturn.address);
}

public void SignMessage()
{
_wallet.SignMessage(new SignMessageArgs(_address, Chain.Polygon, "Hello World!"));
}

private void OnSignMessageComplete(SignMessageReturn result)
{
_resultText.text = result.signature;
}

public void SendTransfer()
{
_wallet.SendTransaction(new SendTransactionArgs(
_address,
Chain.Polygon,
new SequenceSDK.WaaS.Transaction[]
{
new RawTransaction("0x9766bf76b2E3e7BCB8c61410A3fC873f1e89b43f", "1")
}));
}

private void OnSuccessfulTransaction(SuccessfulTransactionReturn result)
{
_resultText.text = $"https://polygonscan.com/tx/{result.txHash}";
Debug.Log("Transaction successful: " + result.txHash);
}

private void OnFailedTransaction(FailedTransactionReturn result)
{
_resultText.text = result.error;
Debug.LogError("Transaction failed: " + result.error);
}

public void SendFailingTransfer()
{
_wallet.SendTransaction(new SendTransactionArgs(
_address,
Chain.Polygon,
new SequenceSDK.WaaS.Transaction[]
{
new RawTransaction("0x9766bf76b2E3e7BCB8c61410A3fC873f1e89b43f", "99000000000000000000")
}));
}

public void DropSession()
{
_wallet.DropThisSession();
}

private void OnDropSessionComplete(string droppedSessionId)
{
Debug.Log("Session dropped: " + droppedSessionId);
_wallet = null;
Close();
}

public void SendErc20Transfer()
{
_wallet.SendTransaction(new SendTransactionArgs(
_address, Chain.Polygon, new SequenceSDK.WaaS.Transaction[]
{
new SendERC20(
"0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",
"0x9766bf76b2E3e7BCB8c61410A3fC873f1e89b43f",
"1"),
}));
}

public void SendErc721Transfer()
{
_wallet.SendTransaction(new SendTransactionArgs(
_address, Chain.Polygon, new SequenceSDK.WaaS.Transaction[]
{
new SendERC721(
"0xa9a6A3626993D487d2Dbda3173cf58cA1a9D9e9f",
"0x9766bf76b2E3e7BCB8c61410A3fC873f1e89b43f",
"54530968763798660137294927684252503703134533114052628080002308208148824588621"),
}));
}

public void SendErc1155Transfer()
{
_wallet.SendTransaction(new SendTransactionArgs(
_address, Chain.Polygon, new SequenceSDK.WaaS.Transaction[]
{
new SendERC1155(
"0x44b3f42e2bf34f62868ff9e9dab7c2f807ba97cb",
"0x9766bf76b2E3e7BCB8c61410A3fC873f1e89b43f",
new SendERC1155Values[]
{
new SendERC1155Values("86", "1")
}),
}));
}

public void SendMultipleTransferTypes()
{
_wallet.SendTransaction(new SendTransactionArgs(
_address, Chain.Polygon, new SequenceSDK.WaaS.Transaction[]
{
new RawTransaction("0x9766bf76b2E3e7BCB8c61410A3fC873f1e89b43f", "1"),
new SendERC20(
"0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",
"0x9766bf76b2E3e7BCB8c61410A3fC873f1e89b43f",
"1"),
new RawTransaction("0x9766bf76b2E3e7BCB8c61410A3fC873f1e89b43f", "1"),
new SendERC721(
"0xa9a6A3626993D487d2Dbda3173cf58cA1a9D9e9f",
"0x9766bf76b2E3e7BCB8c61410A3fC873f1e89b43f",
"54530968763798660137294927684252503703134533114052628080002308208148824588621"),
new RawTransaction("0x9766bf76b2E3e7BCB8c61410A3fC873f1e89b43f", "1"),
}));
}

public void SendWithAdapter()
{
DoSendWithAdapter();
}

private async Task DoSendWithAdapter()
{
ERC721 nft = new ERC721("0xa9a6A3626993D487d2Dbda3173cf58cA1a9D9e9f");
var receipt = await nft.TransferFrom(_adapter.GetAddress(), "0x9766bf76b2E3e7BCB8c61410A3fC873f1e89b43f",
"54530968763798660137294927684252503703134533114052628080002308208148824588621")
.SendTransactionMethodAndWaitForReceipt(_adapter, new SequenceEthClient("https://polygon-bor.publicnode.com"));
Debug.LogError($"Transaction hash: {receipt.transactionHash}");

}

public void SendMultipleWithAdapter()
{
DoSendMultipleWithAdapter();
}

private async Task DoSendMultipleWithAdapter()
{
ERC721 nft = new ERC721("0xa9a6A3626993D487d2Dbda3173cf58cA1a9D9e9f");
ERC1155 sft = new ERC1155("0x44b3f42e2bf34f62868ff9e9dab7c2f807ba97cb");
SequenceEthClient client = new SequenceEthClient("https://polygon-bor.publicnode.com");
var nftTransfer = await nft.TransferFrom(_adapter.GetAddress(),
"0x9766bf76b2E3e7BCB8c61410A3fC873f1e89b43f",
"54530968763798660137294927684252503703134533114052628080002308208148824588621")(client, new ContractCall(_adapter.GetAddress()));
var sftTransfer = await sft.SafeTransferFrom(_adapter.GetAddress(),
"0x9766bf76b2E3e7BCB8c61410A3fC873f1e89b43f",
"86",
1)(client, new ContractCall(_adapter.GetAddress()));

var receipt = await _adapter.SendTransactionBatchAndWaitForReceipts(client, new EthTransaction[]
{
nftTransfer, sftTransfer
});
Debug.LogError($"Transaction hash: {receipt[0].transactionHash}");

// or

// _wallet.SendTransaction(new SendTransactionArgs(
// _address, Chain.Polygon, new SequenceSDK.WaaS.Transaction[]
// {
// new RawTransaction(nftTransfer),
// new RawTransaction(sftTransfer),
// }));
}

public void SendMultipleWithAdapter2()
{
DoSendMultipleWithAdapter2();
}

private async Task DoSendMultipleWithAdapter2()
{
ERC721 nft = new ERC721("0xa9a6A3626993D487d2Dbda3173cf58cA1a9D9e9f");
ERC1155 sft = new ERC1155("0x44b3f42e2bf34f62868ff9e9dab7c2f807ba97cb");
_wallet.SendTransaction(new SendTransactionArgs(
_address, Chain.Polygon, new SequenceSDK.WaaS.Transaction[]
{
new RawTransaction(nft.Contract, "transferFrom", _adapter.GetAddress().Value,
"0x9766bf76b2E3e7BCB8c61410A3fC873f1e89b43f",
BigInteger.Parse("54530968763798660137294927684252503703134533114052628080002308208148824588621")),
new RawTransaction(sft.Contract, "safeTransferFrom", _adapter.GetAddress().Value,
"0x9766bf76b2E3e7BCB8c61410A3fC873f1e89b43f",
86,
1, "data".ToByteArray()), // Todo figure out why data is required
}));
}

public void DelayedEncode()
{
_wallet.SendTransaction(new SendTransactionArgs(
_address, Chain.Polygon, new SequenceSDK.WaaS.Transaction[]
{
new DelayedEncode("0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359", "0", new DelayedEncodeData(
"transfer(address,uint256)",
new object[]
{
"0x9766bf76b2E3e7BCB8c61410A3fC873f1e89b43f", "1"
},
"transfer")),
}));
}

public void ListSessions()
{
_wallet.OnSessionsFound += OnSessionsListed;
_wallet.ListSessions();
}

private void OnSessionsListed(WaaSSession[] sessions)
{
_resultText.text = $"Found {sessions.Length} sessions";
}
}
}
3 changes: 3 additions & 0 deletions Assets/SequenceExamples/Scripts/UI/WaaSDemoPage.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Assets/SequenceSDK/Ethereum/Contract/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public async Task<string> Deploy(string bytecode, params object[] constructorArg
{
throw new NotImplementedException();
}

public string AssembleCallData(string functionName, params object[] functionArgs)
{
return GetData(functionName, functionArgs);
}

public CallContractFunctionTransactionCreator CallFunction(string functionName, params object[] functionArgs)
{
Expand Down
Loading

0 comments on commit 19d9d04

Please sign in to comment.