Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add openSetDelegate(address) #553

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const DelegateAccount = () => {
setDelegateAddress(delegate?.toString() || "");
setIsDelegateSupported(true);
} catch (e: any) {
console.log(e)
console.log(e);
// controller doesnt support delegateAccount, ignore
}
}, [account, chainId, cartridgeConnector]);
Expand Down Expand Up @@ -76,12 +76,16 @@ export const DelegateAccount = () => {
type="text"
min-width="420px"
value={delegateAddressInput}
onChange={(e:any ) => setDelegateAddressInput(e.target.value)}
onChange={(e: any) => setDelegateAddressInput(e.target.value)}
/>
<Button onClick={() => execute()} disabled={submitted}>
Set Delegate
</Button>
</div>

<div className="flex gap-2">
<Button onClick={() => cartridgeConnector.openSetDelegate(delegateAddressInput)}>Open Set Delegate</Button>
</div>
</>
) : (
<p>Not supported!</p>
Expand Down
4 changes: 4 additions & 0 deletions packages/connector/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class ControllerConnector extends Connector {
return await this.controller.delegateAccount();
}

async openSetDelegate(address: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this could just be setDelegate, since if the session token is authorized, we could set it without the modal (in the future)

return await this.controller.openSetDelegate(address)
}

async openMenu() {
return await this.controller.openMenu();
}
Expand Down
14 changes: 14 additions & 0 deletions packages/controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ class Controller {
return true;
}

async openSetDelegate(address: string) {
if (!this.keychain || !this.modal) {
console.error(new NotReadyToConnect().message);
return null;
}
this.modal.open();
const res = await this.keychain.openSetDelegate(address);
this.modal.close();
if (res && (res as ConnectError).code === ResponseCodes.NOT_CONNECTED) {
return false;
}
return true;
}

private initModal() {
if (typeof document === "undefined") return;

Expand Down
1 change: 1 addition & 0 deletions packages/controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface Keychain {
): Promise<ExecuteReply | ConnectError>;
logout(): Promise<void>;
openMenu(): Promise<void | ConnectError>;
openSetDelegate(address: string): Promise<void | ConnectError>;
session(): Promise<Session>;
sessions(): Promise<{
[key: string]: Session;
Expand Down
4 changes: 3 additions & 1 deletion packages/keychain/src/components/SetDelegate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import { CallData, num } from "starknet";
export function SetDelegate({
onClose,
onSetDelegate,
defaultAddress,
}: {
onClose: () => void;
onSetDelegate: (address: string) => void;
defaultAddress: string;
}) {
const [delegateAddress, setDelegateAddress] = useState("");
const [delegateAddress, setDelegateAddress] = useState(defaultAddress);
const [isValid, setIsValid] = useState(true);

useEffect(() => {
Expand Down
1 change: 1 addition & 0 deletions packages/keychain/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ function Home() {
onSetDelegate={(delegateAddress) => {
setDelegateTransaction(ctx, delegateAddress);
}}
defaultAddress={ctx.account}
/>
</DeploymentRequired>
);
Expand Down
Loading