Skip to content

Commit ae1552e

Browse files
committed
fix: lint issues, formatting
1 parent 149ee3e commit ae1552e

11 files changed

Lines changed: 132 additions & 73 deletions

File tree

packages/delegation-toolkit/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
"@metamask/eslint-config": "^12.0.0",
126126
"@metamask/eslint-config-nodejs": "^12.0.0",
127127
"@metamask/eslint-config-typescript": "^12.0.0",
128+
"@metamask/utils": "^11.4.0",
128129
"@types/node": "^20.10.6",
129130
"@types/sinon": "^17.0.3",
130131
"@typescript-eslint/eslint-plugin": "^5.42.1",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { read as disabledDelegations } from './methods/disabledDelegations';
12
import { read as getAnyDelegate } from './methods/getAnyDelegate';
23
import { read as getRootAuthority } from './methods/getRootAuthority';
3-
import { read as disabledDelegations } from './methods/disabledDelegations';
44

55
export { getAnyDelegate, getRootAuthority, disabledDelegations };

packages/delegation-toolkit/src/DelegationFramework/ERC20PeriodTransferEnforcer/methods/getAvailableAmount.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ export const read = async ({
1717
delegationManager,
1818
terms,
1919
}: ReadGetAvailableAmountParameters) => {
20-
const result = await readContract(client, {
21-
address: contractAddress,
22-
abi: ERC20PeriodTransferEnforcer.abi,
23-
functionName: 'getAvailableAmount',
24-
args: [delegationHash, delegationManager, terms],
25-
});
26-
27-
const [availableAmount, isNewPeriod, currentPeriod] = result;
20+
const [availableAmount, isNewPeriod, currentPeriod] = await readContract(
21+
client,
22+
{
23+
address: contractAddress,
24+
abi: ERC20PeriodTransferEnforcer.abi,
25+
functionName: 'getAvailableAmount',
26+
args: [delegationHash, delegationManager, terms],
27+
},
28+
);
2829

2930
return {
3031
availableAmount,

packages/delegation-toolkit/src/DelegationFramework/ERC20StreamingEnforcer/methods/getAvailableAmount.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const read = async ({
3535
// Check if state exists (startTime != 0)
3636
if (startTime !== 0n) {
3737
// State exists, calculate available amount using the stored state
38-
const availableAmount = _getAvailableAmount({
38+
const availableAmount = getAvailableAmount({
3939
initialAmount,
4040
maxAmount,
4141
amountPerSecond,
@@ -66,7 +66,7 @@ export const read = async ({
6666
] = decodedTerms;
6767

6868
// Simulate using decoded terms with spent = 0
69-
const availableAmount = _getAvailableAmount({
69+
const availableAmount = getAvailableAmount({
7070
initialAmount: decodedInitialAmount,
7171
maxAmount: decodedMaxAmount,
7272
amountPerSecond: decodedAmountPerSecond,
@@ -81,9 +81,18 @@ export const read = async ({
8181
};
8282

8383
/**
84-
* Replicates the internal _getAvailableAmount logic from the smart contract
84+
* Replicates the internal _getAvailableAmount logic from the smart contract.
85+
*
86+
* @param allowance - The allowance object containing all parameters.
87+
* @param allowance.initialAmount - The initial amount available.
88+
* @param allowance.maxAmount - The maximum amount allowed.
89+
* @param allowance.amountPerSecond - The amount streamed per second.
90+
* @param allowance.startTime - The start time of the streaming.
91+
* @param allowance.spent - The amount already spent.
92+
* @param allowance.currentTimestamp - The current timestamp.
93+
* @returns The available amount that can be spent.
8594
*/
86-
function _getAvailableAmount(allowance: {
95+
function getAvailableAmount(allowance: {
8796
initialAmount: bigint;
8897
maxAmount: bigint;
8998
amountPerSecond: bigint;

packages/delegation-toolkit/src/DelegationFramework/MultiTokenPeriodEnforcer/methods/getAvailableAmount.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ export const read = async ({
1919
terms,
2020
args,
2121
}: ReadGetAvailableAmountParameters) => {
22-
const result = await readContract(client, {
23-
address: contractAddress,
24-
abi: MultiTokenPeriodEnforcer.abi,
25-
functionName: 'getAvailableAmount',
26-
args: [delegationHash, delegationManager, terms, args],
27-
});
28-
29-
const [availableAmount, isNewPeriod, currentPeriod] = result;
22+
const [availableAmount, isNewPeriod, currentPeriod] = await readContract(
23+
client,
24+
{
25+
address: contractAddress,
26+
abi: MultiTokenPeriodEnforcer.abi,
27+
functionName: 'getAvailableAmount',
28+
args: [delegationHash, delegationManager, terms, args],
29+
},
30+
);
3031

3132
return {
3233
availableAmount,

packages/delegation-toolkit/src/DelegationFramework/NativeTokenPeriodTransferEnforcer/methods/getAvailableAmount.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ export const read = async ({
1717
delegationManager,
1818
terms,
1919
}: ReadGetAvailableAmountParameters) => {
20-
const result = await readContract(client, {
21-
address: contractAddress,
22-
abi: NativeTokenPeriodTransferEnforcer.abi,
23-
functionName: 'getAvailableAmount',
24-
args: [delegationHash, delegationManager, terms],
25-
});
26-
27-
const [availableAmount, isNewPeriod, currentPeriod] = result;
20+
const [availableAmount, isNewPeriod, currentPeriod] = await readContract(
21+
client,
22+
{
23+
address: contractAddress,
24+
abi: NativeTokenPeriodTransferEnforcer.abi,
25+
functionName: 'getAvailableAmount',
26+
args: [delegationHash, delegationManager, terms],
27+
},
28+
);
2829

2930
return {
3031
availableAmount,

packages/delegation-toolkit/src/DelegationFramework/NativeTokenStreamingEnforcer/methods/getAvailableAmount.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const read = async ({
3535
// Check if state exists (startTime != 0)
3636
if (startTime !== 0n) {
3737
// State exists, calculate available amount using the stored state
38-
const availableAmount = _getAvailableAmount({
38+
const availableAmount = getAvailableAmount({
3939
initialAmount,
4040
maxAmount,
4141
amountPerSecond,
@@ -65,7 +65,7 @@ export const read = async ({
6565
] = decodedTerms;
6666

6767
// Simulate using decoded terms with spent = 0
68-
const availableAmount = _getAvailableAmount({
68+
const availableAmount = getAvailableAmount({
6969
initialAmount: decodedInitialAmount,
7070
maxAmount: decodedMaxAmount,
7171
amountPerSecond: decodedAmountPerSecond,
@@ -80,9 +80,18 @@ export const read = async ({
8080
};
8181

8282
/**
83-
* Replicates the internal _getAvailableAmount logic from the smart contract
83+
* Replicates the internal _getAvailableAmount logic from the smart contract.
84+
*
85+
* @param allowance - The allowance object containing all parameters.
86+
* @param allowance.initialAmount - The initial amount available.
87+
* @param allowance.maxAmount - The maximum amount allowed.
88+
* @param allowance.amountPerSecond - The amount streamed per second.
89+
* @param allowance.startTime - The start time of the streaming.
90+
* @param allowance.spent - The amount already spent.
91+
* @param allowance.currentTimestamp - The current timestamp.
92+
* @returns The available amount that can be spent.
8493
*/
85-
function _getAvailableAmount(allowance: {
94+
function getAvailableAmount(allowance: {
8695
initialAmount: bigint;
8796
maxAmount: bigint;
8897
amountPerSecond: bigint;

packages/delegation-toolkit/src/actions/getCaveatAvailableAmount.ts

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,88 @@ import type { Address, Hex, PublicClient } from 'viem';
22

33
import * as ERC20PeriodTransferEnforcer from '../DelegationFramework/ERC20PeriodTransferEnforcer';
44
import * as ERC20StreamingEnforcer from '../DelegationFramework/ERC20StreamingEnforcer';
5+
import * as MultiTokenPeriodEnforcer from '../DelegationFramework/MultiTokenPeriodEnforcer';
56
import * as NativeTokenPeriodTransferEnforcer from '../DelegationFramework/NativeTokenPeriodTransferEnforcer';
67
import * as NativeTokenStreamingEnforcer from '../DelegationFramework/NativeTokenStreamingEnforcer';
7-
import * as MultiTokenPeriodEnforcer from '../DelegationFramework/MultiTokenPeriodEnforcer';
88
import type { DeleGatorEnvironment } from '../types';
99

1010
/**
1111
* Configuration for caveat enforcer client
1212
*/
13-
export interface CaveatEnforcerClientConfig {
13+
export type CaveatEnforcerClientConfig = {
1414
environment: DeleGatorEnvironment;
15-
}
15+
};
1616

1717
/**
1818
* Shared base params for all enforcer actions
1919
*/
20-
export interface BaseCaveatParams {
20+
export type BaseCaveatParams = {
2121
delegationHash: Hex;
2222
delegationManager?: Address;
2323
enforcerAddress?: Address;
2424
terms: Hex;
25-
}
25+
};
2626

2727
/**
28-
* Parameters for ERC20 period transfer enforcer
28+
* Parameters for ERC20 period transfer enforcer.
2929
*/
30-
export interface ERC20PeriodTransferParams extends BaseCaveatParams {}
30+
export type ERC20PeriodTransferParams = Record<string, never> &
31+
BaseCaveatParams;
3132

3233
/**
33-
* Parameters for ERC20 streaming enforcer
34+
* Parameters for ERC20 streaming enforcer.
3435
*/
35-
export interface ERC20StreamingParams extends BaseCaveatParams {}
36+
export type ERC20StreamingParams = Record<string, never> & BaseCaveatParams;
3637

3738
/**
38-
* Parameters for MultiTokenPeriodEnforcer
39+
* Parameters for MultiTokenPeriodEnforcer.
3940
*/
40-
export interface MultiTokenPeriodParams extends BaseCaveatParams {
41+
export type MultiTokenPeriodParams = {
4142
args: Hex;
42-
}
43+
} & BaseCaveatParams;
4344

4445
/**
45-
* Parameters for native token period transfer enforcer
46+
* Parameters for native token period transfer enforcer.
4647
*/
47-
export interface NativeTokenPeriodTransferParams extends BaseCaveatParams {}
48+
export type NativeTokenPeriodTransferParams = Record<string, never> &
49+
BaseCaveatParams;
4850

4951
/**
50-
* Parameters for native token streaming enforcer
52+
* Parameters for native token streaming enforcer.
5153
*/
52-
export interface NativeTokenStreamingParams extends BaseCaveatParams {}
54+
export type NativeTokenStreamingParams = Record<string, never> &
55+
BaseCaveatParams;
5356

5457
/**
5558
* Return type for period-based transfer enforcers
5659
*/
57-
export interface PeriodTransferResult {
60+
export type PeriodTransferResult = {
5861
availableAmount: bigint;
5962
isNewPeriod: boolean;
6063
currentPeriod: bigint;
61-
}
64+
};
6265

6366
/**
6467
* Return type for streaming enforcers
6568
*/
66-
export interface StreamingResult {
69+
export type StreamingResult = {
6770
availableAmount: bigint;
68-
}
71+
};
6972

7073
/**
71-
* Resolves the delegation manager address from parameters or environment
74+
* Resolves the delegation manager address from parameters or environment.
75+
*
76+
* @param params - The parameters object.
77+
* @param params.delegationManager - The delegation manager address.
78+
* @param environment - The delegator environment.
79+
* @returns The resolved delegation manager address.
7280
*/
7381
function resolveDelegationManager(
7482
params: { delegationManager?: Address },
7583
environment: DeleGatorEnvironment,
7684
): Address {
7785
const delegationManager =
78-
params.delegationManager || environment.DelegationManager;
86+
params.delegationManager ?? environment.DelegationManager;
7987

8088
if (!delegationManager) {
8189
throw new Error('Delegation manager address not found');
@@ -85,7 +93,13 @@ function resolveDelegationManager(
8593
}
8694

8795
/**
88-
* Resolves the enforcer address from parameters or environment
96+
* Resolves the enforcer address from parameters or environment.
97+
*
98+
* @param enforcerName - The name of the enforcer.
99+
* @param params - The parameters object.
100+
* @param params.enforcerAddress - The enforcer address.
101+
* @param environment - The delegator environment.
102+
* @returns The resolved enforcer address.
89103
*/
90104
function resolveEnforcerAddress(
91105
enforcerName: keyof DeleGatorEnvironment['caveatEnforcers'],
@@ -105,12 +119,18 @@ function resolveEnforcerAddress(
105119
}
106120

107121
/**
108-
* Caveat enforcer actions for extending viem clients
122+
* Caveat enforcer actions for extending viem clients.
123+
*
124+
* @param config - The configuration for caveat enforcers.
125+
* @returns The client extension with caveat enforcer actions.
109126
*/
110127
export const caveatEnforcerActions =
111128
(config: CaveatEnforcerClientConfig) => (client: PublicClient) => ({
112129
/**
113-
* Get available amount for ERC20 period transfer enforcer
130+
* Get available amount for ERC20 period transfer enforcer.
131+
*
132+
* @param params - The parameters for the ERC20 period transfer enforcer.
133+
* @returns Promise resolving to the period transfer result.
114134
*/
115135
getErc20PeriodTransferEnforcerAvailableAmount: async (
116136
params: ERC20PeriodTransferParams,
@@ -135,7 +155,10 @@ export const caveatEnforcerActions =
135155
},
136156

137157
/**
138-
* Get available amount for ERC20 streaming enforcer
158+
* Get available amount for ERC20 streaming enforcer.
159+
*
160+
* @param params - The parameters for the ERC20 streaming enforcer.
161+
* @returns Promise resolving to the streaming result.
139162
*/
140163
getErc20StreamingEnforcerAvailableAmount: async (
141164
params: ERC20StreamingParams,
@@ -160,7 +183,10 @@ export const caveatEnforcerActions =
160183
},
161184

162185
/**
163-
* Get available amount for multi-token period enforcer
186+
* Get available amount for multi-token period enforcer.
187+
*
188+
* @param params - The parameters for the multi-token period enforcer.
189+
* @returns Promise resolving to the period transfer result.
164190
*/
165191
getMultiTokenPeriodEnforcerAvailableAmount: async (
166192
params: MultiTokenPeriodParams,
@@ -186,7 +212,10 @@ export const caveatEnforcerActions =
186212
},
187213

188214
/**
189-
* Get available amount for native token period transfer enforcer
215+
* Get available amount for native token period transfer enforcer.
216+
*
217+
* @param params - The parameters for the native token period transfer enforcer.
218+
* @returns Promise resolving to the period transfer result.
190219
*/
191220
getNativeTokenPeriodTransferEnforcerAvailableAmount: async (
192221
params: NativeTokenPeriodTransferParams,
@@ -211,7 +240,10 @@ export const caveatEnforcerActions =
211240
},
212241

213242
/**
214-
* Get available amount for native token streaming enforcer
243+
* Get available amount for native token streaming enforcer.
244+
*
245+
* @param params - The parameters for the native token streaming enforcer.
246+
* @returns Promise resolving to the streaming result.
215247
*/
216248
getNativeTokenStreamingEnforcerAvailableAmount: async (
217249
params: NativeTokenStreamingParams,
@@ -237,13 +269,17 @@ export const caveatEnforcerActions =
237269
});
238270

239271
/**
240-
* Type for client extended with caveat enforcer actions
272+
* Type for client extended with caveat enforcer actions.
241273
*/
242274
export type CaveatEnforcerClient = PublicClient &
243275
ReturnType<ReturnType<typeof caveatEnforcerActions>>;
244276

245277
/**
246-
* Create a viem client extended with caveat enforcer actions
278+
* Create a viem client extended with caveat enforcer actions.
279+
*
280+
* @param client - The viem public client.
281+
* @param config - The configuration for caveat enforcers.
282+
* @returns The extended client with caveat enforcer actions.
247283
*/
248284
export function createCaveatEnforcerClient(
249285
client: PublicClient,

0 commit comments

Comments
 (0)