Skip to content
Merged
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 @@ -9,7 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"@metamask/delegation-toolkit": "^0.12.0",
"@metamask/delegation-toolkit": "^0.13.0",
"@tanstack/react-query": "^5.76.1",
"next": "15.3.4",
"permissionless": "^0.2.46",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default function CreateDelegateCodeBlock() {
content: (
<>
{" "}
<Property>signatory</Property>: {`{ `}
<Property>signer</Property>: {`{ `}
<Variable>account</Variable>
{` }`},
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
Method,
Property,
StringLiteral,
NumberLiteral,
} from "@/components/CodeBlock";

export default function CreateDelegationCodeBlock() {
Expand All @@ -16,22 +15,14 @@ export default function CreateDelegationCodeBlock() {
},
{
prefix: ">" as const,
content: (
<>
<Keyword>const</Keyword> <Variable>caveats</Variable> ={" "}
<Method>createCaveatBuilder</Method>(<Variable>smartAccount</Variable>
.<Property>environment</Property>)
</>
),
content: <br />,
},
{
prefix: ">" as const,
content: (
<>
{" "}
.<Method>addCaveat</Method>(
<StringLiteral>"limitedCalls"</StringLiteral>,{" "}
<NumberLiteral>1</NumberLiteral>)
<Keyword>const</Keyword> <Variable>delegation</Variable> ={" "}
<Method>createDelegation</Method>({`{`}
</>
),
},
Expand All @@ -40,20 +31,21 @@ export default function CreateDelegationCodeBlock() {
content: (
<>
{" "}
.<Method>build</Method>()
<Property>scope</Property>: {`{`}
</>
),
},
{
prefix: ">" as const,
content: <br />,
},
{
prefix: ">" as const,
content: (
<>
<Keyword>const</Keyword> <Variable>delegation</Variable> ={" "}
<Method>createDelegation</Method>({`{`}
{" "}
<Property>type</Property>: <StringLiteral>"nativeTokenTransferAmount"</StringLiteral>,
<br />
{" "}
<Property>maxAmount</Property>: <Method>parseEther</Method>(<StringLiteral>"0.001"</StringLiteral>),
<br />
{` }`},
</>
),
},
Expand Down Expand Up @@ -82,7 +74,8 @@ export default function CreateDelegationCodeBlock() {
content: (
<>
{" "}
<Property>caveats</Property>: <Variable>caveats</Variable>,
<Property>environment</Property>: <Variable>delegateSmartAccount</Variable>.
<Property>environment</Property>,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: Incorrect Environment Reference in Delegation Code

The CreateDelegationCodeBlock.tsx example uses delegateSmartAccount.environment when creating a delegation. This should be smartAccount.environment (the delegator's environment) to align with the delegator.environment expected by delegationUtils.ts.

Additional Locations (2)

Fix in Cursor Fix in Web

</>
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default function CreateDelegatorCodeBlock() {
content: (
<>
{" "}
<Property>signatory</Property>: {`{ `}
<Property>signer</Property>: {`{ `}
<Variable>walletClient</Variable>
{` }`},
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default function RedeemDelegationCodeBlock() {
content: (
<>
{" "}
<Property>modes</Property>: [<Variable>SINGLE_DEFAULT_MODE</Variable>
<Property>modes</Property>: [<Variable>ExecutionMode.SingleDefault</Variable>
],
</>
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function useDelegateSmartAccount() {
implementation: Implementation.Hybrid,
deployParams: [account.address, [], [], []],
deploySalt: "0x",
signatory: { account },
signer: { account },
}).then((smartAccount) => {
setSmartAccount(smartAccount);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function useDelegatorSmartAccount(): {
implementation: Implementation.Hybrid,
deployParams: [address, [], [], []],
deploySalt: "0x",
signatory: { walletClient },
signer: { walletClient },
}).then((smartAccount) => {
setSmartAccount(smartAccount);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
import {
createCaveatBuilder,
createDelegation,
createExecution,
Delegation,
MetaMaskSmartAccount,
ExecutionMode,
} from "@metamask/delegation-toolkit";
import { DelegationManager } from "@metamask/delegation-toolkit/contracts";
import { SINGLE_DEFAULT_MODE } from "@metamask/delegation-toolkit/utils";
import { Address, Hex, zeroAddress } from "viem";
import { Address, Hex, parseEther, zeroAddress } from "viem";

export function prepareRootDelegation(
delegator: MetaMaskSmartAccount,
delegate: Address,
): Delegation {
// The following caveat enforcer is a simple example that limits
// the number of executions the delegate can perform on the delegator's
// The following scope is a simple example that limits
// the number of native token transfers the delegate can perform on the delegator's
// behalf.

// You can add more caveat enforcers to the delegation as needed to restrict
// You can add more caveats to the delegation as needed to restrict
// the delegate's actions. Checkout delegation-toolkit docs for more
// information on restricting delegate's actions.

// Restricting a delegation:
// https://docs.metamask.io/delegation-toolkit/how-to/create-delegation/restrict-delegation/
const caveats = createCaveatBuilder(delegator.environment)
.addCaveat("limitedCalls", 1)
.build();

return createDelegation({
scope: {
type: "nativeTokenTransferAmount",
maxAmount: parseEther("0.001"),
},
to: delegate,
from: delegator.address,
caveats: caveats,
environment: delegator.environment,
});
}

export function prepareRedeemDelegationData(delegation: Delegation): Hex {
const execution = createExecution({ target: zeroAddress });
const data = DelegationManager.encode.redeemDelegations({
delegations: [[delegation]],
modes: [SINGLE_DEFAULT_MODE],
modes: [ExecutionMode.SingleDefault],
executions: [[execution]],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"@farcaster/miniapp-sdk": "^0.1.7",
"@farcaster/miniapp-wagmi-connector": "^1.0.0",
"@metamask/delegation-toolkit": "^0.11.0",
"@metamask/delegation-toolkit": "^0.13.0",
"@tanstack/react-query": "^5.76.1",
"permissionless": "^0.2.46",
"viem": "^2.29.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default function CreateDelegateCodeBlock() {
content: (
<>
{" "}
<Property>signatory</Property>: {`{ `}
<Property>signer</Property>: {`{ `}
<Variable>account</Variable>
{` }`},
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
Method,
Property,
StringLiteral,
NumberLiteral,
} from "@/components/CodeBlock";

export default function CreateDelegationCodeBlock() {
Expand All @@ -16,22 +15,14 @@ export default function CreateDelegationCodeBlock() {
},
{
prefix: ">" as const,
content: (
<>
<Keyword>const</Keyword> <Variable>caveats</Variable> ={" "}
<Method>createCaveatBuilder</Method>(<Variable>smartAccount</Variable>
.<Property>environment</Property>)
</>
),
content: <br />,
},
{
prefix: ">" as const,
content: (
<>
{" "}
.<Method>addCaveat</Method>(
<StringLiteral>"limitedCalls"</StringLiteral>,{" "}
<NumberLiteral>1</NumberLiteral>)
<Keyword>const</Keyword> <Variable>delegation</Variable> ={" "}
<Method>createDelegation</Method>({`{`}
</>
),
},
Expand All @@ -40,20 +31,21 @@ export default function CreateDelegationCodeBlock() {
content: (
<>
{" "}
.<Method>build</Method>()
<Property>scope</Property>: {`{`}
</>
),
},
{
prefix: ">" as const,
content: <br />,
},
{
prefix: ">" as const,
content: (
<>
<Keyword>const</Keyword> <Variable>delegation</Variable> ={" "}
<Method>createDelegation</Method>({`{`}
{" "}
<Property>type</Property>: <StringLiteral>"nativeTokenTransferAmount"</StringLiteral>,
<br />
{" "}
<Property>maxAmount</Property>: <Method>parseEther</Method>(<StringLiteral>"0.001"</StringLiteral>),
<br />
{` }`},
</>
),
},
Expand Down Expand Up @@ -82,7 +74,8 @@ export default function CreateDelegationCodeBlock() {
content: (
<>
{" "}
<Property>caveats</Property>: <Variable>caveats</Variable>,
<Property>environment</Property>: <Variable>delegateSmartAccount</Variable>.
<Property>environment</Property>,
</>
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default function CreateDelegatorCodeBlock() {
content: (
<>
{" "}
<Property>signatory</Property>: {`{ `}
<Property>signer</Property>: {`{ `}
<Variable>walletClient</Variable>
{` }`},
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function RedeemDelegationCodeBlock() {
content: (
<>
{" "}
<Property>modes</Property>: [<Variable>SINGLE_DEFAULT_MODE</Variable>
<Property>modes</Property>: [<Variable>ExecutionMode.SingleDefault</Variable>
],
</>
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function useDelegateSmartAccount() {
implementation: Implementation.Hybrid,
deployParams: [account.address, [], [], []],
deploySalt: "0x",
signatory: { account },
signer: { account },
}).then((smartAccount) => {
setSmartAccount(smartAccount);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function useDelegatorSmartAccount(): {
implementation: Implementation.Hybrid,
deployParams: [address, [], [], []],
deploySalt: "0x",
signatory: { walletClient },
signer: { walletClient },
}).then((smartAccount) => {
setSmartAccount(smartAccount);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
import {
createCaveatBuilder,
createDelegation,
createExecution,
Delegation,
DelegationFramework,
MetaMaskSmartAccount,
SINGLE_DEFAULT_MODE,
ExecutionMode,
} from "@metamask/delegation-toolkit";
import { Address, Hex } from "viem";
import { DelegationManager } from "@metamask/delegation-toolkit/contracts";
import { Address, Hex, parseEther, zeroAddress } from "viem";

export function prepareRootDelegation(
delegator: MetaMaskSmartAccount,
delegate: Address
): Delegation {
// The following caveat enforcer is a simple example that limits
// the number of executions the delegate can perform on the delegator's
// The following scope is a simple example that limits
// the number of native token transfers the delegate can perform on the delegator's
// behalf.

// You can add more caveat enforcers to the delegation as needed to restrict
// You can add more caveats to the delegation as needed to restrict
// the delegate's actions. Checkout delegation-toolkit docs for more
// information on restricting delegate's actions.

// Restricting a delegation:
// https://docs.metamask.io/delegation-toolkit/how-to/create-delegation/restrict-delegation/
const caveats = createCaveatBuilder(delegator.environment)
.addCaveat("limitedCalls", 1);

return createDelegation({
scope: {
type: "nativeTokenTransferAmount",
maxAmount: parseEther("0.001"),
},
to: delegate,
from: delegator.address,
caveats: caveats,
environment: delegator.environment,
});
}

export function prepareRedeemDelegationData(delegation: Delegation): Hex {
const execution = createExecution();
const data = DelegationFramework.encode.redeemDelegations({
const execution = createExecution({ target: zeroAddress });
const data = DelegationManager.encode.redeemDelegations({
delegations: [[delegation]],
modes: [SINGLE_DEFAULT_MODE],
modes: [ExecutionMode.SingleDefault],
executions: [[execution]],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"@metamask/delegation-toolkit": "^0.12.0",
"@metamask/delegation-toolkit": "^0.13.0",
"@tanstack/react-query": "^5.81.5",
"permissionless": "^0.2.47",
"next": "15.3.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function useSmartAccount(): {
implementation: Implementation.Hybrid,
deployParams: [address, [], [], []],
deploySalt: "0x",
signatory: { walletClient },
signer: { walletClient },
}).then((smartAccount) => {
setSmartAccount(smartAccount);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"react": "^19.0.0",
"react-dom": "^19.0.0",
"@metamask/delegation-toolkit": "^0.12.0",
"@metamask/delegation-toolkit": "^0.13.0",
"@tanstack/react-query": "^5.76.1",
"permissionless": "^0.2.46",
"viem": "^2.29.2",
Expand Down
Loading
Loading