Skip to content

Commit

Permalink
Jitdeploy (#626)
Browse files Browse the repository at this point in the history
* JIT DEPLOYS

* Remove sync status stuff

* Add some error handing notes

* Sdk paymaster handle rpc errors

* Add deploy call to connection

* Fix fmt / build

* Fix execute prompt paymaster

---------

Co-authored-by: broody <[email protected]>
  • Loading branch information
tarrencev and broody authored Aug 29, 2024
1 parent 8940a6f commit f806926
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 495 deletions.
55 changes: 30 additions & 25 deletions examples/starknet-react-next/src/components/TransferEth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,33 @@ export const TransferEth = () => {
const explorer = useExplorer();
const [txnHash, setTxnHash] = useState<string>();

const execute = useCallback(async () => {
if (!account) {
return;
}
setSubmitted(true);
setTxnHash(undefined);
const execute = useCallback(
async (amount: string) => {
if (!account) {
return;
}
setSubmitted(true);
setTxnHash(undefined);

account
.execute([
{
contractAddress: ETH_CONTRACT,
entrypoint: "approve",
calldata: [account?.address, "0x1C6BF52634000", "0x0"],
},
{
contractAddress: ETH_CONTRACT,
entrypoint: "transfer",
calldata: [account?.address, "0x1C6BF52634000", "0x0"],
},
])
.then(({ transaction_hash }) => setTxnHash(transaction_hash))
.catch((e) => console.error(e))
.finally(() => setSubmitted(false));
}, [account]);
account
.execute([
{
contractAddress: ETH_CONTRACT,
entrypoint: "approve",
calldata: [account?.address, amount, "0x0"],
},
{
contractAddress: ETH_CONTRACT,
entrypoint: "transfer",
calldata: [account?.address, amount, "0x0"],
},
])
.then(({ transaction_hash }) => setTxnHash(transaction_hash))
.catch((e) => console.error(e))
.finally(() => setSubmitted(false));
},
[account],
);

if (!account) {
return null;
Expand All @@ -46,8 +49,10 @@ export const TransferEth = () => {
<div>
<h2>Transfer Eth</h2>
<p>Address: {ETH_CONTRACT}</p>

<Button onClick={() => execute()} disabled={submitted}>
<Button onClick={() => execute("0x0")} disabled={submitted}>
Transfer 0 ETH to self
</Button>
<Button onClick={() => execute("0x1C6BF52634000")} disabled={submitted}>
Transfer 0.005 ETH to self
</Button>
{txnHash && (
Expand Down
100 changes: 0 additions & 100 deletions packages/account-wasm/src/paymaster.rs

This file was deleted.

12 changes: 11 additions & 1 deletion packages/controller/src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,27 @@ class DeviceAccount extends Account {
false,
this.paymaster,
);

if (res.code === ResponseCodes.SUCCESS) {
return res as InvokeFunctionResponse;
}

this.modal.open();

if (res.code === ResponseCodes.NOT_DEPLOYED) {
res = await this.keychain.deploy();
if (res.code !== ResponseCodes.SUCCESS) {
return Promise.reject(res.message);
}
}

res = await this.keychain.execute(
calls,
abis,
transactionsDetail,
true,
this.paymaster,
);
this.modal.close();

if (res.code !== ResponseCodes.SUCCESS) {
return Promise.reject(res.message);
Expand All @@ -116,6 +124,8 @@ class DeviceAccount extends Account {
return res as InvokeFunctionResponse;
} catch (e) {
throw e;
} finally {
this.modal.close();
}
}

Expand Down
7 changes: 7 additions & 0 deletions packages/controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type Policy = {
export enum ResponseCodes {
SUCCESS = "SUCCESS",
NOT_CONNECTED = "NOT_CONNECTED",
NOT_DEPLOYED = "NOT_DEPLOYED",
NOT_ALLOWED = "NOT_ALLOWED",
CANCELED = "CANCELED",
}
Expand All @@ -59,6 +60,11 @@ export type ProbeReply = {
address: string;
};

export type DeployReply = {
code: ResponseCodes.SUCCESS;
transaction_hash: string;
};

export interface Keychain {
probe(rpcUrl?: string): Promise<ProbeReply | ConnectError>;
connect(
Expand All @@ -70,6 +76,7 @@ export interface Keychain {
reset(): void;
revoke(origin: string): void;

deploy(): Promise<DeployReply | ConnectError>;
estimateDeclareFee(
payload: DeclareContractPayload,
details?: EstimateFeeDetails,
Expand Down
67 changes: 4 additions & 63 deletions packages/keychain/src/components/DeploymentRequired.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,29 @@
import { constants } from "starknet";
import { Container, Footer, Content } from "components/layout";
import { useEffect, useState } from "react";
import { Button, Link, Text } from "@chakra-ui/react";
import { Button, Link } from "@chakra-ui/react";
import { ExternalIcon, PacmanIcon } from "@cartridge/ui";
import { useController } from "hooks/controller";
import { ErrorAlert } from "./ErrorAlert";
import { Funding } from "./Funding";
import NextLink from "next/link";
import { useDeploy } from "hooks/deploy";
import { useConnection } from "hooks/connection";
import { CARTRIDGE_DISCORD_LINK } from "const";

export function DeploymentRequired({
onClose,
children,
}: {
onClose: () => void;
children: React.ReactNode;
}) {
export function DeploymentRequired({ onClose }: { onClose: () => void }) {
const {
controller: { account },
} = useController();
const { hasPrefundRequest } = useConnection();
const { deployRequest, isDeployed } = useDeploy();
const [deployHash, setDeployHash] = useState<string>();
const [showFunding, setShowFunding] = useState(false);
const [error, setError] = useState<Error>();
const [showFunding, setShowFunding] = useState(true);

useEffect(() => {
if (isDeployed) {
return;
}

if (
account.chainId === constants.StarknetChainId.SN_MAIN ||
hasPrefundRequest
) {
setShowFunding(true);
return;
}

deployRequest(account.username)
.then((hash) => {
setDeployHash(hash);
})
.catch((e) => setError(e));
}, [
account.chainId,
account.username,
hasPrefundRequest,
isDeployed,
deployRequest,
]);

if (isDeployed) {
return <>{children}</>;
}
}, [account.chainId, account.username, hasPrefundRequest]);

if (showFunding)
return (
Expand Down Expand Up @@ -95,33 +63,6 @@ export function DeploymentRequired({
)}
</Content>
<Footer>
{error && (
<ErrorAlert
title="Account deployment error"
description={
<>
<Text mb={4} color="inherit">
Please come by{" "}
<Link
as={NextLink}
href={CARTRIDGE_DISCORD_LINK}
isExternal
display="inline-flex"
flexDir="row"
columnGap="0.1rem"
alignItems="center"
fontWeight="bold"
>
Discord
<ExternalIcon />
</Link>{" "}
and report this issue.
</Text>
<Text color="inherit">{error.message}</Text>
</>
}
/>
)}
<Button onClick={onClose}>Close</Button>
</Footer>
</Container>
Expand Down
Loading

0 comments on commit f806926

Please sign in to comment.