Skip to content

Commit ccb3982

Browse files
SunZhiCgitbook-bot
authored andcommitted
GITBOOK-1135: No subject
1 parent 56e4484 commit ccb3982

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed

SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
* [Unity](getting-started/platform-specific-guides/unity/README.md)
6666
* [Unity SDK Prerequisites](getting-started/platform-specific-guides/unity/unity-sdk-prerequisites.md)
6767
* [Social Login + Embeded Wallet](getting-started/platform-specific-guides/unity/social-login-+-embeded-wallet.md)
68+
* [Social Login + AA](getting-started/platform-specific-guides/unity/social-login-+-aa.md)
6869
* [Unreal](getting-started/platform-specific-guides/unreal.md)
6970
* [Cocos](getting-started/platform-specific-guides/cocos/README.md)
7071
* [Cocos SDK Prerequisites](getting-started/platform-specific-guides/cocos/cocos-sdk-prerequisites.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
# Social Login + AA
2+
3+
This guide will help you get started with Particle Unity AA SDK by setting up your environment, creating a wallet with social account, and sending a User Operation from it. By the end of this guide, you'll have a basic understanding of how to use the SDK and where to look for more advanced user cases.
4+
5+
If you face any problem anytime, you can always refer to the Particle[ ](https://github.com/Web3Auth/Web3Auth/discussions)Community for us to help you out as soon as possible!
6+
7+
Before read the guide, you should read [last guide](social-login-+-embeded-wallet.md), understand how to connect, AA service should be used with Connect Service.
8+
9+
**Now, let's start with last guide example.**
10+
11+
### Initialization AA in Unity
12+
13+
You can add these code below Connect Service Initialization.
14+
15+
Set your AccountName, there are the options, `BICONOMY | SIMPLE | CYBERCONNECT`.
16+
17+
```csharp
18+
ParticleAAInteraction.Init(AAAccountName.SIMPLE, AAVersionNumber.V1_0_0(), new Dictionary<int, string>());
19+
ParticleAAInteraction.EnableAAMode();
20+
```
21+
22+
After get account, you can get smart account address, then each time you send transaction, you should use smart account address to create transaction.
23+
24+
```csharp
25+
public async void GetSmartAccount()
26+
{
27+
try
28+
{
29+
var smartAccountResult = await EvmService.GetSmartAccount(new[]
30+
{
31+
new SmartAccountObject(AAAccountName.SIMPLE.ToString(), AAVersionNumber.V1_0_0().version,
32+
this._publicAddress)
33+
});
34+
this._smartAccountAddress = (string)JObject.Parse(smartAccountResult)["result"][0]["smartAccountAddress"];
35+
Debug.Log($"_smartAccountAddress: {_smartAccountAddress}");
36+
}
37+
catch (Exception e)
38+
{
39+
Debug.LogError($"An error occurred: {e.Message}");
40+
}
41+
}
42+
```
43+
44+
Let's send a transaction in AA, it use the same method `signAndSendTransaction`.
45+
46+
Show how to send transaction in AA mode and use native token to pay gas fee.
47+
48+
The method `SignAndSendTransaction` last parameter is `FeeMode`, if enable AA, default value is native, which means pay native for gas fee.
49+
50+
You can also use gasless and pay with token. For more detail, explore [Account Abstraction](../../../developers/account-abstraction/unity.md).
51+
52+
```csharp
53+
public async void SendTransaction()
54+
{
55+
if (this._publicAddress == null || this._walletType == null || this._smartAccountAddress == null)
56+
{
57+
Debug.Log("did not connect any account");
58+
return;
59+
}
60+
61+
try
62+
{
63+
// use smart account to create transaction
64+
var address = this._smartAccountAddress;
65+
const string to = "0x0000000000000000000000000000000000000000";
66+
var transaction =
67+
await EvmService.CreateTransaction(address, "0x", BigInteger.Parse("10000000000000000"), to);
68+
// call SignAndSendTransaction must use Account.publicAddress (Your EOA address) to send transaction
69+
var nativeResultData =
70+
await ParticleConnect.Instance.SignAndSendTransaction((WalletType)this._walletType, _publicAddress,
71+
transaction);
72+
if (nativeResultData.isSuccess)
73+
{
74+
Debug.Log(nativeResultData.data);
75+
var result = nativeResultData.data;
76+
Debug.Log($"result: {result}");
77+
}
78+
else
79+
{
80+
var errorData = JsonConvert.DeserializeObject<NativeErrorData>(nativeResultData.data);
81+
Debug.Log(errorData);
82+
}
83+
}
84+
catch (Exception e)
85+
{
86+
Debug.LogError($"An error occurred: {e.Message}");
87+
}
88+
}
89+
```
90+
91+
## AA support batch send multi transactions
92+
93+
Here we make two transactions, and call `ParticleAA.Instance.RpcGetFeeQuotes` to check if there transactions can send with pay native, gasless, or pay token.
94+
95+
96+
97+
<pre class="language-csharp"><code class="lang-csharp"><strong>public async void BatchSendTransactions()
98+
</strong>{
99+
if (this._publicAddress == null || this._walletType == null || this._smartAccountAddress == null)
100+
{
101+
Debug.Log("did not connect any account");
102+
return;
103+
}
104+
105+
tryc
106+
{
107+
// use smart account to create transaction
108+
var address = this._smartAccountAddress;
109+
const string to = "0x0000000000000000000000000000000000000000";
110+
// Now, create two transactions
111+
var transaction1 =
112+
await EvmService.CreateTransaction(address, "0x", BigInteger.Parse("10000000000000000"), to);
113+
var transaction2 =
114+
await EvmService.CreateTransaction(address, "0x", BigInteger.Parse("20000000000000000"), to);
115+
116+
// Before send
117+
// check if pay native available
118+
var feeQuotesResultData = await ParticleAA.Instance.RpcGetFeeQuotes(this._publicAddress,
119+
new List&#x3C;string> { transaction1, transaction2 });
120+
121+
var verifyingPaymasterNative = JObject.Parse(feeQuotesResultData.data)["verifyingPaymasterNative"];
122+
var nativeFeeQuote = verifyingPaymasterNative["feeQuote"];
123+
124+
var fee = BigInteger.Parse((string)nativeFeeQuote["fee"]);
125+
var balance = BigInteger.Parse((string)nativeFeeQuote["balance"]);
126+
127+
if (balance &#x3C; fee)
128+
{
129+
Debug.Log("native balance if not enough for gas fee");
130+
}
131+
else
132+
{
133+
Debug.Log("could send pay native");
134+
}
135+
// is balance >= fee, you can send with native
136+
// here is the code
137+
// var batchSendResultData =
138+
// await ParticleConnect.Instance.BatchSendTransactions((WalletType)this._walletType, _publicAddress,
139+
// new List&#x3C;string> { transaction1, transaction2 }, AAFeeMode.Native(feeQuotesResultData));
140+
141+
// check if pay gasless available
142+
var verifyingPaymasterGasless = JObject.Parse(feeQuotesResultData.data)["verifyingPaymasterGasless"];
143+
144+
if (verifyingPaymasterGasless == null)
145+
{
146+
Debug.Log("gasless is not available");
147+
}
148+
else
149+
{
150+
Debug.Log("could send gasless");
151+
}
152+
// is verifyingPaymasterGasless is not null, you can send gasless
153+
// here is the code
154+
// var batchSendResultData =
155+
// await ParticleConnect.Instance.BatchSendTransactions((WalletType)this._walletType, _publicAddress,
156+
// new List&#x3C;string> { transaction1, transaction2 }, AAFeeMode.Gasless(feeQuotesResultData));
157+
158+
// Now, let's check if you can send pay token
159+
// check if pay token available
160+
var tokenPaymaster = JObject.Parse(feeQuotesResultData.data)["tokenPaymaster"];
161+
Debug.Log($"TokenPaymaster = {tokenPaymaster}");
162+
163+
if (tokenPaymaster == null)
164+
{
165+
Debug.Log("pay token is not available");
166+
return;
167+
}
168+
else
169+
{
170+
}
171+
172+
JArray feeQuotes = (JArray)(tokenPaymaster["feeQuotes"]);
173+
174+
var overFeeQuotes = feeQuotes
175+
.Where(jt =>
176+
{
177+
var fee = BigInteger.Parse(jt["fee"].Value&#x3C;string>());
178+
var balance = BigInteger.Parse((string)jt["balance"].Value&#x3C;string>());
179+
180+
return balance >= fee;
181+
})
182+
.ToList();
183+
184+
185+
if (overFeeQuotes.Count == 0)
186+
{
187+
Debug.Log("no valid token for gas fee");
188+
return;
189+
}
190+
191+
Debug.Log("prepare send pay token");
192+
193+
// select the first feeQuote
194+
var feeQuote = overFeeQuotes[0];
195+
var tokenPaymasterAddress =
196+
JObject.Parse(feeQuotesResultData.data)["tokenPaymaster"]["tokenPaymasterAddress"].Value&#x3C;string>();
197+
198+
// call BatchSendTransactions must use Account.publicAddress (Your EOA address) to send transaction
199+
var batchSendResultData =
200+
await ParticleConnect.Instance.BatchSendTransactions((WalletType)this._walletType, _publicAddress,
201+
new List&#x3C;string> { transaction1, transaction2 }, AAFeeMode.Token(feeQuote, tokenPaymasterAddress));
202+
if (batchSendResultData.isSuccess)
203+
{
204+
var result = batchSendResultData.data;
205+
Debug.Log($"result: {result}");
206+
}
207+
else
208+
{
209+
var errorData = JsonConvert.DeserializeObject&#x3C;NativeErrorData>(batchSendResultData.data);
210+
Debug.Log(errorData);
211+
}
212+
}
213+
catch (Exception e)
214+
{
215+
Debug.LogError($"An error occurred: {e.Message}");
216+
}
217+
}
218+
</code></pre>
219+
220+
## Dive Deeper
221+
222+
In this guide, we learned how to use the Particle AA Flutter SDK for getting smart account, sending transactions. If you want to learn more about different use cases, check out the [Particle A](../../../developers/connect-service/sdks/flutter.md)[A](../../../developers/account-abstraction/flutter.md) page.

0 commit comments

Comments
 (0)