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 @@ -82,14 +82,18 @@ const { availableAmount } = await caveatEnforcerClient.getErc20PeriodTransferEnf

```typescript
import { createDelegation } from '@metamask/delegation-toolkit'
import { parseUnits } from 'viem'

// startDate should be in seconds.
const startDate = Math.floor(Date.now() / 1000);

export const delegation = createDelegation({
scope: {
type: 'erc20PeriodTransfer',
tokenAddress: '0xb4aE654Aca577781Ca1c5DE8FbE60c2F423f37da',
periodAmount: 1000000000000000000n,
periodAmount: parseUnits('10', 6),
periodDuration: 86400,
startDate: 1743763600,
startDate,
},
to: delegateAccount,
from: delegatorAccount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ Before creating a delegation, ensure that the delegator account (in this example

```typescript
import { createDelegation } from "@metamask/delegation-toolkit"
import { parseUnits } from "viem"

// USDC address on Ethereum Sepolia.
const tokenAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238";
const tokenAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"

const delegation = createDelegation({
to: delegateSmartAccount.address, // This example uses a delegate smart account
Expand All @@ -148,7 +149,8 @@ const delegation = createDelegation({
scope: {
type: "erc20TransferAmount",
tokenAddress,
maxAmount: 10000000n,
// 10 USDC
maxAmount: parseUnits("10", 6),
},
})
```
Expand All @@ -175,7 +177,7 @@ Bob can now redeem the delegation. The redeem transaction is sent to the `Delega
To prepare the calldata for the redeem transaction, use the [`redeemDelegations`](../../reference/delegation/index.md#redeemdelegations) method from `DelegationManager`.
Since Bob is redeeming a single delegation chain, use the [`SingleDefault`](../../concepts/delegation/index.md#execution-modes) execution mode.

Bob can redeem the delegation by submitting a user operation if his account is a smart account, or a regular transaction if his account is an EOA:
Bob can redeem the delegation by submitting a user operation if his account is a smart account, or a regular transaction if his account is an EOA. In this example, Bob transfers 1 USDC from Alice’s account to his own.

<Tabs>
<TabItem value="Redeem with a smart account">
Expand All @@ -184,10 +186,14 @@ Bob can redeem the delegation by submitting a user operation if his account is a
import { createExecution, ExecutionMode } from "@metamask/delegation-toolkit"
import { DelegationManager } from "@metamask/delegation-toolkit/contracts"
import { zeroAddress } from "viem"
import { callData } from "./config.ts"

const delegations = [signedDelegation]

const executions = createExecution({ target: zeroAddress })
// USDC address on Ethereum Sepolia.
const tokenAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"

const executions = createExecution({ target: tokenAddress, callData })

const redeemDelegationCalldata = DelegationManager.encode.redeemDelegations({
delegations: [delegations],
Expand Down Expand Up @@ -215,10 +221,14 @@ const userOperationHash = await bundlerClient.sendUserOperation({
import { createExecution, getDeleGatorEnvironment, ExecutionMode } from "@metamask/delegation-toolkit"
import { DelegationManager } from "@metamask/delegation-toolkit/contracts"
import { zeroAddress } from "viem"
import { callData } from "./config.ts"

const delegations = [signedDelegation]

const executions = createExecution({ target: zeroAddress })
// USDC address on Ethereum Sepolia.
const tokenAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"

const executions = createExecution({ target: tokenAddress, callData })

const redeemDelegationCalldata = DelegationManager.encode.redeemDelegations({
delegations: [delegations],
Expand All @@ -233,6 +243,21 @@ const transactionHash = await delegateWalletClient.sendTransaction({
})
```

</TabItem>

<TabItem value="config.ts">

```typescript
import { encodeFunctionData, erc20Abi, parseUnits } from "viem"

// calldata to transfer 1 USDC to delegate address.
export const callData = encodeFunctionData({
abi: erc20Abi,
args: [ delegateSmartAccount.address, parseUnits("1", 6) ],
functionName: 'transfer',
})
```

</TabItem>
</Tabs>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ Internally, this scope uses the [`erc20PeriodTransfer`](../../../reference/deleg

```typescript
import { createDelegation } from "@metamask/delegation-toolkit";
import { parseUnits } from "viem";

// startDate should be in seconds.
const startDate = Math.floor(Date.now() / 1000);

const delegation = createDelegation({
scope: {
type: "erc20PeriodTransfer",
tokenAddress: "0xb4aE654Aca577781Ca1c5DE8FbE60c2F423f37da",
periodAmount: 1000000000000000000n,
// USDC has 6 decimal places.
periodAmount: parseUnits("10", 6),
periodDuration: 86400,
startDate: 1743763600,
startDate,
},
to: delegateAccount,
from: delegatorAccount,
Expand All @@ -57,15 +62,20 @@ Internally, this scope uses the [`erc20Streaming`](../../../reference/delegation

```typescript
import { createDelegation } from "@metamask/delegation-toolkit";
import { parseUnits } from "viem";

// startTime should be in seconds.
const startTime = Math.floor(Date.now() / 1000);

const delegation = createDelegation({
scope: {
type: "erc20Streaming",
tokenAddress: "0xc11F3a8E5C7D16b75c9E2F60d26f5321C6Af5E92",
amountPerSecond: 100n,
initialAmount: 1000000n,
maxAmount: 10000000n,
startTime: 1703980800,
// USDC has 6 decimal places.
amountPerSecond: parseUnits("0.1", 6),
initialAmount: parseUnits("10", 6),
maxAmount: parseUnits("100", 6),
startTime,
},
to: delegateAccount,
from: delegatorAccount,
Expand All @@ -86,12 +96,14 @@ Internally, this scope uses the [`erc20TransferAmount`](../../../reference/deleg

```typescript
import { createDelegation } from "@metamask/delegation-toolkit";
import { parseUnits } from "viem";

const delegation = createDelegation({
scope: {
type: "erc20TransferAmount",
tokenAddress: "0xc11F3a8E5C7D16b75c9E2F60d26f5321C6Af5E92",
maxAmount: 10000n,
// USDC has 6 decimal places.
maxAmount: parseUnits("10", 6),
},
to: delegateAccount,
from: delegatorAccount,
Expand Down Expand Up @@ -135,13 +147,17 @@ Internally, this scope uses the [`exactCalldata`](../../../reference/delegation/

```typescript
import { createDelegation } from "@metamask/delegation-toolkit";
import { parseEther } from "viem";

// startDate should be in seconds.
const startDate = Math.floor(Date.now() / 1000);

const delegation = createDelegation({
scope: {
type: "nativeTokenPeriodTransfer",
periodAmount: 1000000000000000000n,
periodAmount: parseEther("0.01"),
periodDuration: 86400,
startDate: 1743763600,
startDate,
},
to: delegateAccount,
from: delegatorAccount,
Expand All @@ -162,14 +178,18 @@ Internally, this scope uses the [`exactCalldata`](../../../reference/delegation/

```typescript
import { createDelegation } from "@metamask/delegation-toolkit";
import { parseEther } from "viem";

// startTime should be in seconds.
const startTime = Math.floor(Date.now() / 1000);

const delegation = createDelegation({
scope: {
type: "nativeTokenStreaming",
amountPerSecond: 100n,
initialAmount: 1000000n,
maxAmount: 10000000n,
startTime: 1703980800,
amountPerSecond: parseEther("0.001"),
initialAmount: parseEther("0.01"),
maxAmount: parseEther("0.1"),
startTime,
},
to: delegateAccount,
from: delegatorAccount,
Expand All @@ -190,12 +210,12 @@ Internally, this scope uses the [`exactCalldata`](../../../reference/delegation/

```typescript
import { createDelegation } from "@metamask/delegation-toolkit";
import { parseEther } from "viem";

const delegation = createDelegation({
scope: {
type: "nativeTokenTransferAmount",
// 0.001 ETH in wei format.
maxAmount: 1000000000000000n,
maxAmount: parseEther("0.001"),
},
to: delegateAccount,
from: delegatorAccount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,11 @@ const transactionHash = await sessionAccountWalletClient.sendTransactionWithDele
<TabItem value="config.ts">

```typescript
import { encodeFunctionData, erc20Abi } from "viem";
import { encodeFunctionData, erc20Abi, parseUnits } from "viem";

export const calldata = encodeFunctionData({
abi: erc20Abi,
args: [ sessionAccount.address, 1000000n ],
args: [ sessionAccount.address, parseUnits("1", 6) ],
functionName: 'transfer',
});
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const userOperationHash = await bundlerClient.sendUserOperation({
calls: [
{
to: "0x1234567890123456789012345678901234567890",
value: parseEther("1")
value: parseEther("0.001"),
}
],
maxFeePerGas,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const userOperationHash = await bundlerClient.sendUserOperation({
calls: [
{
to: "0x1234567890123456789012345678901234567890",
value: parseEther("1")
value: parseEther("0.001")
}
],
maxFeePerGas,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const userOperationHash = await bundlerClient.sendUserOperation({
calls: [
{
to: "0x1234567890123456789012345678901234567890",
value: parseEther("1")
value: parseEther("0.001")
}
],
maxFeePerGas,
Expand Down
47 changes: 31 additions & 16 deletions delegation-toolkit/reference/delegation/caveat-enforcer-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,16 @@ import { getDeleGatorEnvironment } from '@metamask/delegation-toolkit'

const environment = getDeleGatorEnvironment(chain.id)

// Since current time is in seconds, we need to convert milliseconds to seconds.
const startDate = Math.floor(Date.now() / 1000)

export const delegation = createDelegation({
scope: {
type: 'erc20PeriodTransfer',
tokenAddress: '0xb4aE654Aca577781Ca1c5DE8FbE60c2F423f37da',
periodAmount: 1000000000000000000n,
periodAmount: parseUnits('10', 6),
periodDuration: 86400,
startDate: 1743763600,
startDate,
},
to: 'DELEGATE_ADDRESS',
from: 'DELEGATOR_ADDRESS',
Expand Down Expand Up @@ -140,17 +143,21 @@ const { availableAmount } = await caveatEnforcerClient.getErc20StreamingEnforcer
import { createDelegation } from '@metamask/delegation-toolkit'
import { sepolia as chain } from 'viem/chains'
import { getDeleGatorEnvironment } from '@metamask/delegation-toolkit'
import { parseUnits } from 'viem'

const environment = getDeleGatorEnvironment(chain.id)

// Since current time is in seconds, we need to convert milliseconds to seconds.
const startTime = Math.floor(Date.now() / 1000)

export const delegation = createDelegation({
scope: {
type: 'erc20Streaming',
tokenAddress: '0xc11F3a8E5C7D16b75c9E2F60d26f5321C6Af5E92',
amountPerSecond: 100n,
initialAmount: 1000000n,
maxAmount: 10000000n,
startTime: 1703980800,
amountPerSecond: parseUnits('0.1', 6),
initialAmount: parseUnits('1', 6),
maxAmount: parseUnits('10', 6),
startTime,
},
to: 'DELEGATE_ADDRESS',
from: 'DELEGATOR_ADDRESS',
Expand Down Expand Up @@ -191,16 +198,20 @@ const { availableAmount } = await caveatEnforcerClient.getNativeTokenPeriodTrans
```typescript
import { createDelegation } from '@metamask/delegation-toolkit'
import { sepolia as chain } from 'viem/chains'
import { parseEther } from 'viem'
import { getDeleGatorEnvironment } from '@metamask/delegation-toolkit'

const environment = getDeleGatorEnvironment(chain.id)

// Since current time is in seconds, we need to convert milliseconds to seconds.
const startDate = Math.floor(Date.now() / 1000)

export const delegation = createDelegation({
scope: {
type: 'nativeTokenPeriodTransfer',
periodAmount: 1000000000000000000n,
periodAmount: parseEther('0.01', 6),
periodDuration: 86400,
startDate: 1743763600,
startDate,
},
to: 'DELEGATE_ADDRESS',
from: 'DELEGATOR_ADDRESS',
Expand Down Expand Up @@ -245,13 +256,16 @@ import { getDeleGatorEnvironment } from '@metamask/delegation-toolkit'

const environment = getDeleGatorEnvironment(chain.id)

// Since current time is in seconds, we need to convert milliseconds to seconds.
const startTime = Math.floor(Date.now() / 1000)

export const delegation = createDelegation({
scope: {
type: "nativeTokenStreaming",
amountPerSecond: 100n,
initialAmount: 1000000n,
maxAmount: 10000000n,
startTime: 1703980800,
amountPerSecond: parseEther('0.001'),
initialAmount: parseEther('0.01'),
maxAmount: parseEther('0.1'),
startTime,
},
to: 'DELEGATE_ADDRESS',
from: 'DELEGATOR_ADDRESS',
Expand Down Expand Up @@ -300,6 +314,7 @@ const { availableAmount } = await caveatEnforcerClient.getMultiTokenPeriodEnforc
import { createDelegation, getDeleGatorEnvironment, ROOT_AUTHORITY } from '@metamask/delegation-toolkit'
import { createCaveatBuilder } from '@metamask/delegation-toolkit/utils'
import { sepolia as chain } from 'viem/chains'
import { parseUnits, parseEther } from 'viem'

const environment = getDeleGatorEnvironment(chain.id)
const caveatBuilder = createCaveatBuilder(environment)
Expand All @@ -311,8 +326,8 @@ const startDate = Math.floor(Date.now() / 1000);
const tokenConfigs = [
{
token: "0xb4aE654Aca577781Ca1c5DE8FbE60c2F423f37da",
// 1 token with 18 decimals.
periodAmount: 1000000000000000000n,
// 1 token with 6 decimals.
periodAmount: parseUnits('1', 6),
// 1 day in seconds.
periodDuration: 86400,
startDate
Expand All @@ -321,14 +336,14 @@ const tokenConfigs = [
// For native token use zeroAddress
token: zeroAddress,
// 0.01 ETH in wei.
periodAmount: 10000000000000000n,
periodAmount: parseEther('0.01'),
// 1 hour in seconds.
periodDuration: 3600,
startDate
}
]

const caveats = caveatBuilder.addCaveat('nativeTokenTransferAmount', 1000000n).addCaveat({
const caveats = caveatBuilder.addCaveat({
'multiTokenPeriod',
tokenConfigs
})
Expand Down
Loading
Loading