Skip to content

Commit 8d0c2fb

Browse files
AyushBherwani1998“AyushBherwani1998”alexandratran
authored
Update Delegation Toolkit API reference
* update smart account API reference * add disableDelegation in API reference * add new API reference for smart account * add parameters * Apply suggestions from code review Co-authored-by: Alexandra Carrillo <12214231+alexandratran@users.noreply.github.com> --------- Co-authored-by: “AyushBherwani1998” <“ayush.bherwani1998@gmail.com”> Co-authored-by: Alexandra Carrillo <12214231+alexandratran@users.noreply.github.com>
1 parent 8393e7a commit 8d0c2fb

File tree

2 files changed

+197
-0
lines changed

2 files changed

+197
-0
lines changed

delegation-toolkit/reference/api/delegation.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,47 @@ overrideDeployedEnvironment(
250250
// add-end
251251
```
252252

253+
## `disableDelegation`
254+
255+
Encodes the calldata for disabling a delegation.
256+
257+
### Parameters
258+
259+
| Name | Type | Required | Description |
260+
| --- | --- | --- | --- |
261+
| `delegation` | `Delegation` | Yes | The delegation to be disabled. |
262+
263+
### Example
264+
265+
<Tabs>
266+
<TabItem value="example.ts">
267+
268+
```ts
269+
import { DelegationManager } from "@metamask/delegation-toolkit/contracts";
270+
import { delegation } from "./delegation.ts";
271+
272+
const disableDelegationData = DelegationManager.encode.disableDelegation({
273+
delegation,
274+
});
275+
```
276+
277+
</TabItem>
278+
<TabItem value="delegation.ts">
279+
280+
```ts
281+
import { createDelegation } from "@metamask/delegation-toolkit";
282+
283+
export const delegation = createDelegation({
284+
from: "0x7E48cA6b7fe6F3d57fdd0448B03b839958416fC1",
285+
to: "0x2B2dBd1D5fbeB77C4613B66e9F35dBfE12cB0488",
286+
// Empty caveats array - we recommend adding appropriate restrictions
287+
caveats: [],
288+
});
289+
```
290+
291+
</TabItem>
292+
</Tabs>
293+
253294
## `getDeleGatorEnvironment`
254295

255296
Resolves the `DeleGatorEnvironment` for a chain.

delegation-toolkit/reference/api/smart-account.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,162 @@ export const bundlerClient = createBundlerClient({
116116
</TabItem>
117117
</Tabs>
118118

119+
## `encodeCalls`
120+
121+
Encodes calls for execution by a MetaMask smart account. If there's a single call directly to the smart account, it returns the call data directly. For multiple calls or calls to other addresses, it creates executions and encodes them for the smart account's `execute` function.
122+
123+
The execution mode is set to `SINGLE_DEFAULT_MODE` for a single call to other address, or `BATCH_DEFAULT_MODE` for multiple calls.
124+
125+
### Parameters
126+
127+
| Name | Type | Required | Description |
128+
| ---- | ---- | -------- | ----------- |
129+
| `calls` | `Call[]` | Yes | List of calls to be encoded. |
130+
131+
### Example
132+
133+
<Tabs>
134+
<TabItem value ="example.ts">
135+
136+
```ts
137+
import { smartAccount } from "./config.ts";
138+
139+
const calls = [{
140+
to: zeroAddress,
141+
data: "0x",
142+
value: 0n
143+
}];
144+
145+
const executeCallData = await smartAccount.encodeCalls(calls);
146+
```
147+
148+
</TabItem>
149+
150+
<TabItem value ="config.ts">
151+
152+
```ts
153+
import { createPublicClient, http } from "viem";
154+
import { privateKeyToAccount } from "viem/accounts";
155+
import { sepolia as chain } from "viem/chains";
156+
import {
157+
Implementation,
158+
toMetaMaskSmartAccount,
159+
} from "@metamask/delegation-toolkit";
160+
161+
const publicClient = createPublicClient({
162+
chain,
163+
transport: http(),
164+
});
165+
166+
const delegatorAccount = privateKeyToAccount("0x...");
167+
168+
export const smartAccount = await toMetaMaskSmartAccount({
169+
client: publicClient,
170+
implementation: Implementation.Hybrid,
171+
deployParams: [delegatorAccount.address, [], [], []],
172+
deploySalt: "0x",
173+
signatory: { account: delegatorAccount },
174+
});
175+
```
176+
177+
</TabItem>
178+
</Tabs>
179+
180+
## `getFactoryArgs`
181+
182+
Returns the factory address and factory data that can be used to deploy a smart account.
183+
184+
### Example
185+
186+
<Tabs>
187+
<TabItem value ="example.ts">
188+
189+
```ts
190+
import { smartAccount } from "./config.ts";
191+
192+
const { factory, factoryData } = await smartAccount.getFactoryArgs();
193+
```
194+
195+
</TabItem>
196+
197+
<TabItem value ="config.ts">
198+
199+
```ts
200+
import { createPublicClient, http } from "viem";
201+
import { privateKeyToAccount } from "viem/accounts";
202+
import { sepolia as chain } from "viem/chains";
203+
import {
204+
Implementation,
205+
toMetaMaskSmartAccount,
206+
} from "@metamask/delegation-toolkit";
207+
208+
const publicClient = createPublicClient({
209+
chain,
210+
transport: http(),
211+
});
212+
213+
const delegatorAccount = privateKeyToAccount("0x...");
214+
215+
export const smartAccount = await toMetaMaskSmartAccount({
216+
client: publicClient,
217+
implementation: Implementation.Hybrid,
218+
deployParams: [delegatorAccount.address, [], [], []],
219+
deploySalt: "0x",
220+
signatory: { account: delegatorAccount },
221+
});
222+
```
223+
224+
</TabItem>
225+
</Tabs>
226+
227+
228+
## `getNonce`
229+
230+
Returns the nonce for a smart account.
231+
232+
### Example
233+
234+
<Tabs>
235+
<TabItem value ="example.ts">
236+
237+
```ts
238+
import { smartAccount } from "./config.ts";
239+
240+
const nonce = await smartAccount.getNonce();
241+
```
242+
243+
</TabItem>
244+
245+
<TabItem value ="config.ts">
246+
247+
```ts
248+
import { createPublicClient, http } from "viem";
249+
import { privateKeyToAccount } from "viem/accounts";
250+
import { sepolia as chain } from "viem/chains";
251+
import {
252+
Implementation,
253+
toMetaMaskSmartAccount,
254+
} from "@metamask/delegation-toolkit";
255+
256+
const publicClient = createPublicClient({
257+
chain,
258+
transport: http(),
259+
});
260+
261+
const delegatorAccount = privateKeyToAccount("0x...");
262+
263+
export const smartAccount = await toMetaMaskSmartAccount({
264+
client: publicClient,
265+
implementation: Implementation.Hybrid,
266+
deployParams: [delegatorAccount.address, [], [], []],
267+
deploySalt: "0x",
268+
signatory: { account: delegatorAccount },
269+
});
270+
```
271+
272+
</TabItem>
273+
</Tabs>
274+
119275
## `signDelegation`
120276

121277
Signs the delegation and returns the delegation signature.

0 commit comments

Comments
 (0)