Skip to content

Commit

Permalink
chore: update session keys for mainnet
Browse files Browse the repository at this point in the history
  • Loading branch information
bluecco committed Jun 19, 2024
1 parent a6f38ee commit 2a603d4
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 55 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

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

99 changes: 99 additions & 0 deletions src/abi/DummyContract.json
Original file line number Diff line number Diff line change
@@ -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": []
}
]
5 changes: 4 additions & 1 deletion src/components/AccountSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,7 +41,9 @@ const AccountSection: FC<AccountSectionProps> = ({ 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",
)
}}
Expand Down
125 changes: 85 additions & 40 deletions src/components/Actions/SessionKeysExecute.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
ARGENT_DUMMY_CONTRACT_ADDRESS,
ARGENT_SESSION_SERVICE_BASE_URL,
CHAIN_ID,
ETHTokenAddress,
provider,
} from "@/constants"
Expand All @@ -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 = () => {
Expand All @@ -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) ||
Expand All @@ -36,6 +44,7 @@ const SessionKeysExecute = () => {
try {
e.preventDefault()
setTransactionStatus("pending")
setLastTxError("")
if (!accountSessionSignature || !sessionRequest) {
throw new Error("No open session")
}
Expand All @@ -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)
Expand All @@ -100,19 +138,26 @@ const SessionKeysExecute = () => {
onSubmit={submitSessionTransaction}
w="fit-content"
>
<Heading as="h2">Transfer with session keys</Heading>
<Input
//className="p-2 rounded-lg max-w-96"
p="2"
rounded="lg"
type="text"
id="transfer-amount"
name="fname"
placeholder="Amount"
value={amount}
disabled={!accountSessionSignature}
onChange={(e) => setAmount(e.target.value)}
/>
{CHAIN_ID === constants.NetworkName.SN_MAIN ? (
<Heading as="h2">Invoke dummy function with session keys</Heading>
) : (
<>
<Heading as="h2">Transfer with session keys</Heading>
<Input
//className="p-2 rounded-lg max-w-96"
p="2"
rounded="lg"
type="text"
id="transfer-amount"
name="fname"
placeholder="Amount"
value={amount}
disabled={!accountSessionSignature}
onChange={(e) => setAmount(e.target.value)}
/>
</>
)}

<Button
p="2"
rounded="lg"
Expand Down
3 changes: 3 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export const ETHTokenAddress =
export const DAITokenAddress =
"0x00da114221cb83fa859dbdb4c44beeaa0bb37c7537ad5ae66fe5e0efd20e6eb3"

export const ARGENT_DUMMY_CONTRACT_ADDRESS =
"0x001c515f991f706039696a54f6f33730e9b0e8cc5d04187b13c2c714401acfd4"

export const CHAIN_ID =
process.env.NEXT_PUBLIC_CHAIN_ID === constants.NetworkName.SN_MAIN
? constants.NetworkName.SN_MAIN
Expand Down
28 changes: 20 additions & 8 deletions src/helpers/openSessionHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ETHTokenAddress } from "@/constants"
import {
ARGENT_DUMMY_CONTRACT_ADDRESS,
CHAIN_ID,
ETHTokenAddress,
} from "@/constants"
import { DappKey } from "@argent/x-sessions"
import { ec } from "starknet"
import { constants, ec } from "starknet"
import { parseUnits } from "./token"

/* Hardcoded values for session example */
Expand All @@ -25,12 +29,20 @@ const STRKFees = [
},
]

const allowedMethods = [
{
"Contract Address": ETHTokenAddress,
selector: "transfer",
},
]
const allowedMethods =
CHAIN_ID === constants.NetworkName.SN_MAIN
? [
{
"Contract Address": ARGENT_DUMMY_CONTRACT_ADDRESS,
selector: "set_number",
},
]
: [
{
"Contract Address": ETHTokenAddress,
selector: "transfer",
},
]

const expiry = Math.floor((Date.now() + 1000 * 60 * 60 * 24) / 1000) as any

Expand Down

0 comments on commit 2a603d4

Please sign in to comment.