From 2a603d4d52fea324091bdb0fc1678cd56d34bb44 Mon Sep 17 00:00:00 2001 From: bluecco Date: Wed, 19 Jun 2024 16:19:49 +0200 Subject: [PATCH] chore: update session keys for mainnet --- package.json | 2 +- pnpm-lock.yaml | 10 +- src/abi/DummyContract.json | 99 ++++++++++++++ src/components/AccountSection.tsx | 5 +- src/components/Actions/SessionKeysExecute.tsx | 125 ++++++++++++------ src/constants/index.ts | 3 + src/helpers/openSessionHelper.ts | 28 ++-- 7 files changed, 217 insertions(+), 55 deletions(-) create mode 100644 src/abi/DummyContract.json diff --git a/package.json b/package.json index d36c117..67a5e02 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "react-dom": "^18.3.1", "starknet": "^6.9.0", "starknetkit-latest": "npm:starknetkit@^1.1.9", - "starknetkit-next": "npm:starknetkit@^2.2.10" + "starknetkit-next": "npm:starknetkit@^2.2.11" }, "devDependencies": { "@types/lodash-es": "^4.17.12", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fef7649..0f12772 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -60,8 +60,8 @@ importers: specifier: npm:starknetkit@^1.1.9 version: starknetkit@1.1.9(starknet@6.9.0) starknetkit-next: - specifier: npm:starknetkit@^2.2.10 - version: starknetkit@2.2.10(starknet@6.9.0) + specifier: npm:starknetkit@^2.2.11 + version: starknetkit@2.2.11(starknet@6.9.0) devDependencies: '@types/lodash-es': specifier: ^4.17.12 @@ -2872,8 +2872,8 @@ packages: peerDependencies: starknet: ^6.7.0 - starknetkit@2.2.10: - resolution: {integrity: sha512-nOxyxCrXFI/mdAxg2SJiM9Wyt66h0ZIePaB1AJepLA3gUfcogej/hgChZJAr7SYueu2ANcns+eIGohb/sp48nQ==} + starknetkit@2.2.11: + resolution: {integrity: sha512-DQhOHdZx7Yp0cVtUDcmgQQko/Xb2HzOQwI4iZPF5cpnHJEZ1zr4saAUdtd6Xz8LFwlPCvQ8bmQTE65tU5JNWVw==} peerDependencies: starknet: ^6.9.0 @@ -6741,7 +6741,7 @@ snapshots: - uWebSockets.js - utf-8-validate - starknetkit@2.2.10(starknet@6.9.0): + starknetkit@2.2.11(starknet@6.9.0): dependencies: '@trpc/client': 10.45.2(@trpc/server@10.45.2) '@trpc/server': 10.45.2 diff --git a/src/abi/DummyContract.json b/src/abi/DummyContract.json new file mode 100644 index 0000000..d1707c1 --- /dev/null +++ b/src/abi/DummyContract.json @@ -0,0 +1,99 @@ +[ + { + "type": "impl", + "name": "MockDappImpl", + "interface_name": "argent::mocks::mock_dapp::IMockDapp" + }, + { + "type": "interface", + "name": "argent::mocks::mock_dapp::IMockDapp", + "items": [ + { + "type": "function", + "name": "set_number", + "inputs": [ + { + "name": "number", + "type": "core::felt252" + } + ], + "outputs": [], + "state_mutability": "external" + }, + { + "type": "function", + "name": "set_number_double", + "inputs": [ + { + "name": "number", + "type": "core::felt252" + } + ], + "outputs": [], + "state_mutability": "external" + }, + { + "type": "function", + "name": "set_number_times3", + "inputs": [ + { + "name": "number", + "type": "core::felt252" + } + ], + "outputs": [], + "state_mutability": "external" + }, + { + "type": "function", + "name": "increase_number", + "inputs": [ + { + "name": "number", + "type": "core::felt252" + } + ], + "outputs": [ + { + "type": "core::felt252" + } + ], + "state_mutability": "external" + }, + { + "type": "function", + "name": "throw_error", + "inputs": [ + { + "name": "number", + "type": "core::felt252" + } + ], + "outputs": [], + "state_mutability": "external" + }, + { + "type": "function", + "name": "get_number", + "inputs": [ + { + "name": "user", + "type": "core::starknet::contract_address::ContractAddress" + } + ], + "outputs": [ + { + "type": "core::felt252" + } + ], + "state_mutability": "view" + } + ] + }, + { + "type": "event", + "name": "argent::mocks::mock_dapp::MockDapp::Event", + "kind": "enum", + "variants": [] + } +] diff --git a/src/components/AccountSection.tsx b/src/components/AccountSection.tsx index 06a4607..a297080 100644 --- a/src/components/AccountSection.tsx +++ b/src/components/AccountSection.tsx @@ -12,6 +12,7 @@ import { useAtomValue } from "jotai" import { FC } from "react" import { constants } from "starknet" import { Section } from "./Section" +import { CHAIN_ID } from "@/constants" interface AccountSectionProps { address?: string @@ -40,7 +41,9 @@ const AccountSection: FC = ({ address, chainId }) => { onClick={() => { if (!lastTxHash) return window.open( - `https://sepolia.starkscan.co/tx/${lastTxHash}`, + CHAIN_ID === constants.NetworkName.SN_MAIN + ? `https://voyager.online/tx/${lastTxHash}` + : `https://sepolia.voyager.online/tx/${lastTxHash}`, "_blank", ) }} diff --git a/src/components/Actions/SessionKeysExecute.tsx b/src/components/Actions/SessionKeysExecute.tsx index 5fa4e61..7799ed4 100644 --- a/src/components/Actions/SessionKeysExecute.tsx +++ b/src/components/Actions/SessionKeysExecute.tsx @@ -1,5 +1,7 @@ import { + ARGENT_DUMMY_CONTRACT_ADDRESS, ARGENT_SESSION_SERVICE_BASE_URL, + CHAIN_ID, ETHTokenAddress, provider, } from "@/constants" @@ -10,12 +12,17 @@ import { sessionRequestAtom, } from "@/state/argentSessionState" import { connectorDataAtom } from "@/state/connectedWalletStarknetkitNext" -import { lastTxHashAtom, lastTxStatusAtom } from "@/state/transactionState" +import { + lastTxErrorAtom, + lastTxHashAtom, + lastTxStatusAtom, +} from "@/state/transactionState" import { buildSessionAccount } from "@argent/x-sessions" import { useAtom, useAtomValue, useSetAtom } from "jotai" import { useState } from "react" -import { Abi, Contract, Provider, stark } from "starknet" +import { Abi, Contract, Provider, constants, stark } from "starknet" import Erc20Abi from "../../abi/ERC20.json" +import DummyAbi from "../../abi/DummyContract.json" import { Button, Flex, Heading, Input } from "@chakra-ui/react" const SessionKeysExecute = () => { @@ -27,6 +34,7 @@ const SessionKeysExecute = () => { const connectorData = useAtomValue(connectorDataAtom) const [transactionStatus, setTransactionStatus] = useAtom(lastTxStatusAtom) const setLastTransactionHash = useSetAtom(lastTxHashAtom) + const setLastTxError = useSetAtom(lastTxErrorAtom) const buttonsDisabled = ["approve", "pending"].includes(transactionStatus) || @@ -36,6 +44,7 @@ const SessionKeysExecute = () => { try { e.preventDefault() setTransactionStatus("pending") + setLastTxError("") if (!accountSessionSignature || !sessionRequest) { throw new Error("No open session") } @@ -55,34 +64,63 @@ const SessionKeysExecute = () => { argentSessionServiceBaseUrl: ARGENT_SESSION_SERVICE_BASE_URL, }) - const erc20Contract = new Contract( - Erc20Abi as Abi, - ETHTokenAddress, - sessionAccount as any, - ) - - // https://www.starknetjs.com/docs/guides/use_erc20/#interact-with-an-erc20 - // check .populate - const transferCallData = erc20Contract.populate("transfer", { - recipient: connectorData.account, - amount: parseInputAmountToUint256(amount), - }) + if (CHAIN_ID === constants.NetworkName.SN_MAIN) { + const dummyContract = new Contract( + DummyAbi as Abi, + ARGENT_DUMMY_CONTRACT_ADDRESS, + sessionAccount as any, + ) + const transferCallData = dummyContract.populate("set_number", { + number: 1, + }) - // https://www.starknetjs.com/docs/guides/estimate_fees/#estimateinvokefee - const { suggestedMaxFee } = await sessionAccount.estimateInvokeFee({ - contractAddress: ETHTokenAddress, - entrypoint: "transfer", - calldata: transferCallData.calldata, - }) + // https://www.starknetjs.com/docs/guides/estimate_fees/#estimateinvokefee + const { suggestedMaxFee } = await sessionAccount.estimateInvokeFee({ + contractAddress: ARGENT_DUMMY_CONTRACT_ADDRESS, + entrypoint: "set_number", + calldata: transferCallData.calldata, + }) - // https://www.starknetjs.com/docs/guides/estimate_fees/#fee-limitation - const maxFee = (suggestedMaxFee * BigInt(15)) / BigInt(10) - // send to same account - const result = await erc20Contract.transfer(transferCallData.calldata, { - maxFee, - }) + // https://www.starknetjs.com/docs/guides/estimate_fees/#fee-limitation + const maxFee = (suggestedMaxFee * BigInt(15)) / BigInt(10) + // send to same account + const result = await dummyContract.set_number( + transferCallData.calldata, + { + maxFee, + }, + ) + setLastTransactionHash(result.transaction_hash) + } else { + const erc20Contract = new Contract( + Erc20Abi as Abi, + ETHTokenAddress, + sessionAccount as any, + ) + + // https://www.starknetjs.com/docs/guides/use_erc20/#interact-with-an-erc20 + // check .populate + const transferCallData = erc20Contract.populate("transfer", { + recipient: connectorData.account, + amount: parseInputAmountToUint256(amount), + }) + + // https://www.starknetjs.com/docs/guides/estimate_fees/#estimateinvokefee + const { suggestedMaxFee } = await sessionAccount.estimateInvokeFee({ + contractAddress: ETHTokenAddress, + entrypoint: "transfer", + calldata: transferCallData.calldata, + }) + + // https://www.starknetjs.com/docs/guides/estimate_fees/#fee-limitation + const maxFee = (suggestedMaxFee * BigInt(15)) / BigInt(10) + // send to same account + const result = await erc20Contract.transfer(transferCallData.calldata, { + maxFee, + }) + setLastTransactionHash(result.transaction_hash) + } - setLastTransactionHash(result.transaction_hash) setTransactionStatus("success") } catch (e) { console.error(e) @@ -100,19 +138,26 @@ const SessionKeysExecute = () => { onSubmit={submitSessionTransaction} w="fit-content" > - Transfer with session keys - setAmount(e.target.value)} - /> + {CHAIN_ID === constants.NetworkName.SN_MAIN ? ( + Invoke dummy function with session keys + ) : ( + <> + Transfer with session keys + setAmount(e.target.value)} + /> + + )} +