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

refactor: encapsulate Controller.cartridge #1153

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 3 additions & 3 deletions packages/keychain/src/hooks/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ export function useConnectionValue() {
}, [context, parent]);

const setController = useCallback((controller?: Controller) => {
if (controller && controller.cartridge && origin) {
posthog.identify(controller.cartridge.username(), {
if (controller && origin) {
posthog.identify(controller.username(), {
address: controller.address,
class: controller.cartridge.classHash,
class: controller.classHash(),
chainId: controller.chainId,
appId: origin,
});
Expand Down
7 changes: 2 additions & 5 deletions packages/keychain/src/hooks/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useCallback, useState } from "react";
import { num } from "starknet";
import { useConnection } from "./connection";

type TransactionHash = string;

interface DeployInterface {
deploySelf: (maxFee: string) => Promise<TransactionHash>;
deploySelf: (maxFee: string) => Promise<TransactionHash | undefined>;
isDeploying: boolean;
}

Expand All @@ -18,9 +17,7 @@ export const useDeploy = (): DeployInterface => {
if (!controller) return;
try {
setIsDeploying(true);
const { transaction_hash } = await controller.cartridge.deploySelf(
num.toHex(maxFee),
);
const { transaction_hash } = await controller.selfDeploy(maxFee);

return transaction_hash;
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions packages/keychain/src/hooks/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const useUpgrade = (controller?: Controller): UpgradeInterface => {
const current = CONTROLLER_VERSIONS.find(
(v) =>
addAddressPadding(v.hash) ===
addAddressPadding(controller.cartridge.classHash()),
addAddressPadding(controller.classHash()),
);
setCurrent(current);
setAvailable(current?.version !== LATEST_CONTROLLER.version);
Expand All @@ -110,7 +110,7 @@ export const useUpgrade = (controller?: Controller): UpgradeInterface => {
return [];
}

return [controller.cartridge.upgrade(LATEST_CONTROLLER.hash)];
return [controller.upgrade(LATEST_CONTROLLER.hash)];
}, [controller]);

const onUpgrade = useCallback(async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/keychain/src/pages/session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default function Session() {
onCallback({
username: controller.username(),
address: controller.address,
ownerGuid: controller.cartridge.ownerGuid(),
ownerGuid: controller.ownerGuid(),
transactionHash: transaction_hash,
expiresAt: String(SESSION_EXPIRATION),
});
Expand All @@ -129,7 +129,7 @@ export default function Session() {
onCallback({
username: controller.username(),
address: controller.address,
ownerGuid: controller.cartridge.ownerGuid(),
ownerGuid: controller.ownerGuid(),
alreadyRegistered: true,
expiresAt: String(SESSION_EXPIRATION),
});
Expand Down
2 changes: 1 addition & 1 deletion packages/keychain/src/utils/connection/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export function execute({
}

try {
let estimate = await account.cartridge.estimateInvokeFee(calls);
let estimate = await account.estimateInvokeFee(calls);
const maxFee = num.toHex(
num.addPercent(estimate.overall_fee, ESTIMATE_FEE_PERCENTAGE),
);
Expand Down
20 changes: 19 additions & 1 deletion packages/keychain/src/utils/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import {
SessionMetadata,
} from "@cartridge/account-wasm/controller";
import { SessionPolicies } from "@cartridge/presets";
import { DeployedAccountTransaction } from "@starknet-io/types-js";

export default class Controller extends Account {
cartridge: CartridgeAccount;
private cartridge: CartridgeAccount;

constructor({
appId,
Expand Down Expand Up @@ -72,6 +73,14 @@ export default class Controller extends Account {
return this.cartridge.username();
}

classHash() {
return this.cartridge.classHash();
}

ownerGuid() {
return this.cartridge.ownerGuid();
}

rpcUrl() {
return this.cartridge.rpcUrl();
}
Expand Down Expand Up @@ -260,6 +269,15 @@ export default class Controller extends Account {
}
}

async selfDeploy(maxFee: BigNumberish): Promise<DeployedAccountTransaction> {
const release = await mutex.obtain();
try {
return await this.cartridge.deploySelf(num.toHex(maxFee));
} finally {
release();
}
}

async delegateAccount(): Promise<string> {
const release = await mutex.obtain();
try {
Expand Down
Loading