Skip to content

Commit 54b1739

Browse files
committed
Cleanup
1 parent 5e33c5d commit 54b1739

File tree

3 files changed

+141
-164
lines changed

3 files changed

+141
-164
lines changed

Thirdweb.Console/Program.cs

Lines changed: 125 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -147,166 +147,133 @@
147147

148148
#endregion
149149

150+
#region Engine Wallet
151+
152+
// // EngineWallet is compatible with IThirdwebWallet and can be used with any SDK method/extension
153+
// var engineWallet = await EngineWallet.Create(
154+
// client: client,
155+
// engineUrl: Environment.GetEnvironmentVariable("ENGINE_URL"),
156+
// authToken: Environment.GetEnvironmentVariable("ENGINE_ACCESS_TOKEN"),
157+
// walletAddress: Environment.GetEnvironmentVariable("ENGINE_BACKEND_WALLET_ADDRESS"),
158+
// timeoutSeconds: null, // no timeout
159+
// additionalHeaders: null // can set things like x-account-address if using basic session keys
160+
// );
161+
162+
// // Simple self transfer
163+
// var receipt = await engineWallet.Transfer(chainId: 11155111, toAddress: await engineWallet.GetAddress(), weiAmount: 0);
164+
// Console.WriteLine($"Receipt: {receipt}");
165+
166+
#endregion
167+
150168
#region EIP-7702
151169

152-
// --------------------------------------------------------------------------
153-
// Configuration
154-
// --------------------------------------------------------------------------
155-
156-
var chainWith7702 = 911867;
157-
var delegationContractAddress = "0xb012446cba783d0f7723daf96cf4c49005022307"; // MinimalAccount
158-
159-
// Required environment variables
160-
var backendWalletAddress = Environment.GetEnvironmentVariable("ENGINE_BACKEND_WALLET_ADDRESS") ?? throw new Exception("ENGINE_BACKEND_WALLET_ADDRESS is required");
161-
var engineUrl = Environment.GetEnvironmentVariable("ENGINE_URL") ?? throw new Exception("ENGINE_URL is required");
162-
var engineAccessToken = Environment.GetEnvironmentVariable("ENGINE_ACCESS_TOKEN") ?? throw new Exception("ENGINE_ACCESS_TOKEN is required");
163-
164-
// --------------------------------------------------------------------------
165-
// Initialize Engine Wallet
166-
// --------------------------------------------------------------------------
167-
168-
var engineWallet = await EngineWallet.Create(client, engineUrl, engineAccessToken, backendWalletAddress, 15);
169-
170-
// --------------------------------------------------------------------------
171-
// Delegation Contract Implementation
172-
// --------------------------------------------------------------------------
173-
174-
var delegationContract = await ThirdwebContract.Create(client, delegationContractAddress, chainWith7702);
175-
176-
// Initialize a (to-be) 7702 EOA
177-
var eoaWallet = await PrivateKeyWallet.Generate(client);
178-
var eoaWalletAddress = await eoaWallet.GetAddress();
179-
Console.WriteLine($"EOA address: {eoaWalletAddress}");
180-
181-
// Sign the authorization to point to the delegation contract
182-
var authorization = await eoaWallet.SignAuthorization(chainWith7702, delegationContractAddress, willSelfExecute: false);
183-
Console.WriteLine($"Authorization: {JsonConvert.SerializeObject(authorization, Formatting.Indented)}");
184-
185-
// Sign message for session key
186-
var sessionKeyParams = new SessionKeyParams_7702()
187-
{
188-
Signer = backendWalletAddress,
189-
NativeTokenLimitPerTransaction = 0,
190-
StartTimestamp = 0,
191-
EndTimestamp = Utils.GetUnixTimeStampNow() + (3600 * 24),
192-
ApprovedTargets = new List<string> { Constants.ADDRESS_ZERO },
193-
Uid = Guid.NewGuid().ToByteArray()
194-
};
195-
var sessionKeySig = await EIP712.GenerateSignature_SmartAccount_7702("MinimalAccount", "1", chainWith7702, eoaWalletAddress, sessionKeyParams, eoaWallet);
196-
197-
// Create call data for the session key
198-
var sessionKeyCallData = delegationContract.CreateCallData("createSessionKeyWithSig", sessionKeyParams, sessionKeySig.HexToBytes());
199-
200-
// Execute the delegation & session key creation!!!!!!!!!!
201-
var delegationReceipt = await engineWallet.ExecuteTransaction(new ThirdwebTransactionInput(chainId: chainWith7702, to: eoaWalletAddress, data: sessionKeyCallData, authorization: authorization));
202-
Console.WriteLine($"Delegation Execution Receipt: {JsonConvert.SerializeObject(delegationReceipt, Formatting.Indented)}");
203-
204-
// Verify contract code deployed to the EOA
205-
var rpc = ThirdwebRPC.GetRpcInstance(client, chainWith7702);
206-
var code = await rpc.SendRequestAsync<string>("eth_getCode", eoaWalletAddress, "latest");
207-
Console.WriteLine($"EOA code: {code}");
208-
209-
// The EOA is now a contract
210-
var eoaContract = await ThirdwebContract.Create(client, eoaWalletAddress, chainWith7702, delegationContract.Abi);
211-
212-
// --------------------------------------------------------------------------
213-
// Mint Tokens (DropERC20) to the EOA Using the backend session key
214-
// --------------------------------------------------------------------------
215-
216-
var erc20ContractAddress = "0xAA462a5BE0fc5214507FDB4fB2474a7d5c69065b"; // DropERC20
217-
var erc20Contract = await ThirdwebContract.Create(client, erc20ContractAddress, chainWith7702);
218-
219-
// Log ERC20 balance before mint
220-
var eoaBalanceBefore = await erc20Contract.ERC20_BalanceOf(eoaWalletAddress);
221-
Console.WriteLine($"EOA balance before: {eoaBalanceBefore}");
222-
223-
// Create execution call data (calling 'claim' on the DropERC20)
224-
var executeCallData = eoaContract.CreateCallData(
225-
"execute",
226-
new object[]
227-
{
228-
new List<Call>
229-
{
230-
new()
231-
{
232-
Data = erc20Contract
233-
.CreateCallData(
234-
"claim",
235-
new object[]
236-
{
237-
eoaWalletAddress, // receiver
238-
100, // quantity
239-
Constants.NATIVE_TOKEN_ADDRESS, // currency
240-
0, // pricePerToken
241-
new object[] { Array.Empty<byte>(), BigInteger.Zero, BigInteger.Zero, Constants.ADDRESS_ZERO }, // allowlistProof
242-
Array.Empty<byte>() // data
243-
}
244-
)
245-
.HexToBytes(),
246-
To = erc20ContractAddress,
247-
Value = BigInteger.Zero
248-
}
249-
}
250-
}
251-
);
252-
253-
var executeReceipt = await engineWallet.ExecuteTransaction(new ThirdwebTransactionInput(chainId: chainWith7702, to: eoaWalletAddress, data: executeCallData));
254-
Console.WriteLine($"Execute receipt: {JsonConvert.SerializeObject(executeReceipt, Formatting.Indented)}");
255-
256-
// Log ERC20 balance after mint
257-
var eoaBalanceAfter = await erc20Contract.ERC20_BalanceOf(eoaWalletAddress);
258-
Console.WriteLine($"EOA balance after: {eoaBalanceAfter}");
259-
260-
// --------------------------------------------------------------------------
261-
// Mint Tokens (DropERC20) to the EOA Using an alternative executor
262-
// --------------------------------------------------------------------------
263-
264-
// Executor wallet (managed)
265-
var executorWallet = await PrivateKeyWallet.Create(client, privateKey);
266-
267-
// Log ERC20 balance before mint
268-
eoaBalanceBefore = await erc20Contract.ERC20_BalanceOf(eoaWalletAddress);
269-
Console.WriteLine($"EOA balance before: {eoaBalanceBefore}");
270-
271-
// Sign wrapped calls 712 using an authorized session key (backend wallet in this case)
272-
var wrappedCalls = new WrappedCalls()
273-
{
274-
Calls = new List<Call>
275-
{
276-
new()
277-
{
278-
Data = erc20Contract
279-
.CreateCallData(
280-
"claim",
281-
new object[]
282-
{
283-
eoaWalletAddress, // receiver
284-
100, // quantity
285-
Constants.NATIVE_TOKEN_ADDRESS, // currency
286-
0, // pricePerToken
287-
new object[] { Array.Empty<byte>(), BigInteger.Zero, BigInteger.Zero, Constants.ADDRESS_ZERO }, // allowlistProof
288-
Array.Empty<byte>() // data
289-
}
290-
)
291-
.HexToBytes(),
292-
To = erc20ContractAddress,
293-
Value = BigInteger.Zero
294-
}
295-
},
296-
Uid = Guid.NewGuid().ToByteArray().BytesToHex().HexToBytes32()
297-
};
298-
var wrappedCallsSig = await EIP712.GenerateSignature_SmartAccount_7702_WrappedCalls("MinimalAccount", "1", chainWith7702, eoaWalletAddress, wrappedCalls, engineWallet);
299-
300-
// Create execution call data, this time in a way that can be broadcast by anyone
301-
executeCallData = eoaContract.CreateCallData("executeWithSig", wrappedCalls, wrappedCallsSig.HexToBytes());
302-
303-
var executeTx = await ThirdwebTransaction.Create(wallet: executorWallet, txInput: new ThirdwebTransactionInput(chainId: chainWith7702, to: eoaWalletAddress, data: executeCallData));
304-
executeReceipt = await ThirdwebTransaction.SendAndWaitForTransactionReceipt(executeTx);
305-
Console.WriteLine($"Execute receipt: {JsonConvert.SerializeObject(executeReceipt, Formatting.Indented)}");
306-
307-
// Log ERC20 balance after mint
308-
eoaBalanceAfter = await erc20Contract.ERC20_BalanceOf(eoaWalletAddress);
309-
Console.WriteLine($"EOA balance after: {eoaBalanceAfter}");
170+
// // --------------------------------------------------------------------------
171+
// // Configuration
172+
// // --------------------------------------------------------------------------
173+
174+
// var chainWith7702 = 911867;
175+
// var delegationContractAddress = "0xb012446cba783d0f7723daf96cf4c49005022307"; // MinimalAccount
176+
177+
// // Required environment variables
178+
// var backendWalletAddress = Environment.GetEnvironmentVariable("ENGINE_BACKEND_WALLET_ADDRESS") ?? throw new Exception("ENGINE_BACKEND_WALLET_ADDRESS is required");
179+
// var engineUrl = Environment.GetEnvironmentVariable("ENGINE_URL") ?? throw new Exception("ENGINE_URL is required");
180+
// var engineAccessToken = Environment.GetEnvironmentVariable("ENGINE_ACCESS_TOKEN") ?? throw new Exception("ENGINE_ACCESS_TOKEN is required");
181+
182+
// // --------------------------------------------------------------------------
183+
// // Initialize Engine Wallet
184+
// // --------------------------------------------------------------------------
185+
186+
// var engineWallet = await EngineWallet.Create(client, engineUrl, engineAccessToken, backendWalletAddress, 15);
187+
188+
// // --------------------------------------------------------------------------
189+
// // Delegation Contract Implementation
190+
// // --------------------------------------------------------------------------
191+
192+
// var delegationContract = await ThirdwebContract.Create(client, delegationContractAddress, chainWith7702);
193+
194+
// // Initialize a (to-be) 7702 EOA
195+
// var eoaWallet = await PrivateKeyWallet.Generate(client);
196+
// var eoaWalletAddress = await eoaWallet.GetAddress();
197+
// Console.WriteLine($"EOA address: {eoaWalletAddress}");
198+
199+
// // Sign the authorization to point to the delegation contract
200+
// var authorization = await eoaWallet.SignAuthorization(chainWith7702, delegationContractAddress, willSelfExecute: false);
201+
// Console.WriteLine($"Authorization: {JsonConvert.SerializeObject(authorization, Formatting.Indented)}");
202+
203+
// // Sign message for session key
204+
// var sessionKeyParams = new SessionKeyParams_7702()
205+
// {
206+
// Signer = backendWalletAddress,
207+
// NativeTokenLimitPerTransaction = 0,
208+
// StartTimestamp = 0,
209+
// EndTimestamp = Utils.GetUnixTimeStampNow() + (3600 * 24),
210+
// ApprovedTargets = new List<string> { Constants.ADDRESS_ZERO },
211+
// Uid = Guid.NewGuid().ToByteArray()
212+
// };
213+
// var sessionKeySig = await EIP712.GenerateSignature_SmartAccount_7702("MinimalAccount", "1", chainWith7702, eoaWalletAddress, sessionKeyParams, eoaWallet);
214+
215+
// // Create call data for the session key
216+
// var sessionKeyCallData = delegationContract.CreateCallData("createSessionKeyWithSig", sessionKeyParams, sessionKeySig.HexToBytes());
217+
218+
// // Execute the delegation & session key creation in one go, from the backend!
219+
// var delegationReceipt = await engineWallet.ExecuteTransaction(new ThirdwebTransactionInput(chainId: chainWith7702, to: eoaWalletAddress, data: sessionKeyCallData, authorization: authorization));
220+
// Console.WriteLine($"Delegation Execution Receipt: {JsonConvert.SerializeObject(delegationReceipt, Formatting.Indented)}");
221+
222+
// // Verify contract code deployed to the EOA
223+
// var rpc = ThirdwebRPC.GetRpcInstance(client, chainWith7702);
224+
// var code = await rpc.SendRequestAsync<string>("eth_getCode", eoaWalletAddress, "latest");
225+
// Console.WriteLine($"EOA code: {code}");
226+
227+
// // The EOA is now a contract
228+
// var eoaContract = await ThirdwebContract.Create(client, eoaWalletAddress, chainWith7702, delegationContract.Abi);
229+
230+
// // --------------------------------------------------------------------------
231+
// // Mint Tokens (DropERC20) to the EOA Using the backend session key
232+
// // --------------------------------------------------------------------------
233+
234+
// var erc20ContractAddress = "0xAA462a5BE0fc5214507FDB4fB2474a7d5c69065b"; // DropERC20
235+
// var erc20Contract = await ThirdwebContract.Create(client, erc20ContractAddress, chainWith7702);
236+
237+
// // Log ERC20 balance before mint
238+
// var eoaBalanceBefore = await erc20Contract.ERC20_BalanceOf(eoaWalletAddress);
239+
// Console.WriteLine($"EOA balance before: {eoaBalanceBefore}");
240+
241+
// // Create execution call data (calling 'claim' on the DropERC20)
242+
// var executeCallData = eoaContract.CreateCallData(
243+
// "execute",
244+
// new object[]
245+
// {
246+
// new List<Call>
247+
// {
248+
// new()
249+
// {
250+
// Data = erc20Contract
251+
// .CreateCallData(
252+
// "claim",
253+
// new object[]
254+
// {
255+
// eoaWalletAddress, // receiver
256+
// 100, // quantity
257+
// Constants.NATIVE_TOKEN_ADDRESS, // currency
258+
// 0, // pricePerToken
259+
// new object[] { Array.Empty<byte>(), BigInteger.Zero, BigInteger.Zero, Constants.ADDRESS_ZERO }, // allowlistProof
260+
// Array.Empty<byte>() // data
261+
// }
262+
// )
263+
// .HexToBytes(),
264+
// To = erc20ContractAddress,
265+
// Value = BigInteger.Zero
266+
// }
267+
// }
268+
// }
269+
// );
270+
271+
// var executeReceipt = await engineWallet.ExecuteTransaction(new ThirdwebTransactionInput(chainId: chainWith7702, to: eoaWalletAddress, data: executeCallData));
272+
// Console.WriteLine($"Execute receipt: {JsonConvert.SerializeObject(executeReceipt, Formatting.Indented)}");
273+
274+
// // Log ERC20 balance after mint
275+
// var eoaBalanceAfter = await erc20Contract.ERC20_BalanceOf(eoaWalletAddress);
276+
// Console.WriteLine($"EOA balance after: {eoaBalanceAfter}");
310277

311278
#endregion
312279

Thirdweb/Thirdweb.Extensions/ThirdwebExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,9 @@ public static async Task<NFT> ERC721_GetNFT(this ThirdwebContract contract, BigI
12351235
}
12361236
catch (Exception e)
12371237
{
1238+
#pragma warning disable IDE0059 // Unnecessary assignment of a value
12381239
metadata = new NFTMetadata { Description = e.Message };
1240+
#pragma warning restore IDE0059 // Unnecessary assignment of a value
12391241
}
12401242
metadata.Id = tokenId.ToString();
12411243

@@ -1386,7 +1388,9 @@ public static async Task<NFT> ERC1155_GetNFT(this ThirdwebContract contract, Big
13861388
}
13871389
catch (Exception e)
13881390
{
1391+
#pragma warning disable IDE0059 // Unnecessary assignment of a value
13891392
metadata = new NFTMetadata { Description = e.Message };
1393+
#pragma warning restore IDE0059 // Unnecessary assignment of a value
13901394
}
13911395
metadata.Id = tokenId.ToString();
13921396

Thirdweb/Thirdweb.Utils/Utils.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,13 @@ public static async Task<BigInteger> FetchGasPrice(ThirdwebClient client, BigInt
944944
return (gasPrice, gasPrice);
945945
}
946946

947+
// Arbitrum, Arbitrum Nova & Arbitrum Sepolia
948+
if (chainId == (BigInteger)42161 || chainId == (BigInteger)42170 || chainId == (BigInteger)421614)
949+
{
950+
var gasPrice = await FetchGasPrice(client, chainId, withBump).ConfigureAwait(false);
951+
return (gasPrice, gasPrice);
952+
}
953+
947954
try
948955
{
949956
var block = await rpc.SendRequestAsync<JObject>("eth_getBlockByNumber", "latest", true).ConfigureAwait(false);
@@ -1167,17 +1174,16 @@ public static List<EIP7702Authorization> DecodeAutorizationList(byte[] authoriza
11671174
foreach (var rlpElement in decodedList)
11681175
{
11691176
var decodedItem = (RLPCollection)rlpElement;
1177+
var signature = RLPSignedDataDecoder.DecodeSignature(decodedItem, 3);
11701178
var authorizationListItem = new EIP7702Authorization
11711179
{
11721180
ChainId = new HexBigInteger(decodedItem[0].RLPData.ToBigIntegerFromRLPDecoded()).HexValue,
11731181
Address = decodedItem[1].RLPData.BytesToHex().ToChecksumAddress(),
1174-
Nonce = new HexBigInteger(decodedItem[2].RLPData.ToBigIntegerFromRLPDecoded()).HexValue
1182+
Nonce = new HexBigInteger(decodedItem[2].RLPData.ToBigIntegerFromRLPDecoded()).HexValue,
1183+
YParity = signature.V.BytesToHex(),
1184+
R = signature.R.BytesToHex(),
1185+
S = signature.S.BytesToHex()
11751186
};
1176-
var signature = RLPSignedDataDecoder.DecodeSignature(decodedItem, 3);
1177-
authorizationListItem.YParity = signature.V.BytesToHex();
1178-
authorizationListItem.R = signature.R.BytesToHex();
1179-
authorizationListItem.S = signature.S.BytesToHex();
1180-
11811187
authorizationLists.Add(authorizationListItem);
11821188
}
11831189

0 commit comments

Comments
 (0)