Skip to content

Commit

Permalink
chore: add deployments and abi
Browse files Browse the repository at this point in the history
  • Loading branch information
marthendalnunes committed Dec 11, 2024
1 parent a81c4f5 commit 56797dd
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 6 deletions.
11 changes: 11 additions & 0 deletions apps/api-delegations/src/processing/process-delegation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Delegation } from 'universal-types';
import { getDelegationType } from './utils/get-delegation-type.js';
import { processLimitOrderDelegation } from './process-limit-order-delegation.js';

export async function processDelegation(delegation: Delegation) {
const delegationType = getDelegationType(delegation);

if (delegationType === 'LimitOrder') {
return processLimitOrderDelegation(delegation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Delegation } from 'universal-types';

export async function processLimitOrderDelegation(delegation: Delegation) {
console.log('Processing Limit Order Delegation:', delegation);
}
21 changes: 21 additions & 0 deletions apps/api-delegations/src/processing/utils/get-delegation-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Delegation } from 'universal-types';
import { universalDeployments } from 'universal-data';

// TODO: Extract type/function and make it globally available
export type DelegationType = 'LimitOrder';

export function getDelegationType(
delegation: Delegation,
): DelegationType | null {
// TODO: Add more robust logic to determine delegation type
if (
delegation.caveats.some(
({ enforcer }) =>
enforcer.toLowerCase() ===
universalDeployments.ExternalHookEnforcer.toLowerCase(),
)
) {
return 'LimitOrder';
}
return null;
}
17 changes: 11 additions & 6 deletions apps/api-delegations/src/routes/delegations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
postDelegationSchema,
} from '../validation.js';
import type { DelegationWithMetadata } from 'universal-types';
import { processDelegation } from '../processing/process-delegation.js';

const delegationsRouter = new Hono()
// Get a delegation by its hash
Expand Down Expand Up @@ -51,23 +52,27 @@ const delegationsRouter = new Hono()
zValidator('json', postDelegationSchema),
// TODO: validate the signature of the delegation
async (c) => {
const delegation = c.req.valid('json');
const rawDelegation = c.req.valid('json');

const format = JSON.parse(
JSON.stringify(delegation, (_key, value) =>
const delegation = JSON.parse(
JSON.stringify(rawDelegation, (_key, value) =>
typeof value === 'bigint' ? value.toString() : value,
),
);

// Save the delegation to the database
try {
await insertDelegationDb(format);
await Promise.all([
// Process any necessary delegation side effects
processDelegation(delegation),
// Save the delegation to the database
insertDelegationDb(delegation),
]);
} catch (error) {
console.error(error);
return c.json({ error: 'failed to save delegation' }, 500);
}

return c.json({ delegation: format }, 200);
return c.json({ delegation }, 200);
},
)
.patch(
Expand Down
62 changes: 62 additions & 0 deletions packages/universal-data/src/abis/caveat-enforcer-abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export const caveatEnforcerAbi = [
{
type: 'function',
name: 'afterAllHook',
inputs: [
{ name: '', type: 'bytes', internalType: 'bytes' },
{ name: '', type: 'bytes', internalType: 'bytes' },
{ name: '', type: 'bytes32', internalType: 'ModeCode' },
{ name: '', type: 'bytes', internalType: 'bytes' },
{ name: '', type: 'bytes32', internalType: 'bytes32' },
{ name: '', type: 'address', internalType: 'address' },
{ name: '', type: 'address', internalType: 'address' },
],
outputs: [],
stateMutability: 'nonpayable',
},
{
type: 'function',
name: 'afterHook',
inputs: [
{ name: '', type: 'bytes', internalType: 'bytes' },
{ name: '', type: 'bytes', internalType: 'bytes' },
{ name: '', type: 'bytes32', internalType: 'ModeCode' },
{ name: '', type: 'bytes', internalType: 'bytes' },
{ name: '', type: 'bytes32', internalType: 'bytes32' },
{ name: '', type: 'address', internalType: 'address' },
{ name: '', type: 'address', internalType: 'address' },
],
outputs: [],
stateMutability: 'nonpayable',
},
{
type: 'function',
name: 'beforeAllHook',
inputs: [
{ name: '', type: 'bytes', internalType: 'bytes' },
{ name: '', type: 'bytes', internalType: 'bytes' },
{ name: '', type: 'bytes32', internalType: 'ModeCode' },
{ name: '', type: 'bytes', internalType: 'bytes' },
{ name: '', type: 'bytes32', internalType: 'bytes32' },
{ name: '', type: 'address', internalType: 'address' },
{ name: '', type: 'address', internalType: 'address' },
],
outputs: [],
stateMutability: 'nonpayable',
},
{
type: 'function',
name: 'beforeHook',
inputs: [
{ name: '', type: 'bytes', internalType: 'bytes' },
{ name: '', type: 'bytes', internalType: 'bytes' },
{ name: '', type: 'bytes32', internalType: 'ModeCode' },
{ name: '', type: 'bytes', internalType: 'bytes' },
{ name: '', type: 'bytes32', internalType: 'bytes32' },
{ name: '', type: 'address', internalType: 'address' },
{ name: '', type: 'address', internalType: 'address' },
],
outputs: [],
stateMutability: 'nonpayable',
},
] as const;
2 changes: 2 additions & 0 deletions packages/universal-data/src/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ export const universalDeployments = {
RedeemerEnforcer: '0xbD848E5861825754c809F5b813A72B50691A86A6',
TimestampEnforcer: '0x675b4844E388e329354185cAcBC30E3F93456559',
ValueLteEnforcer: '0x1f7EE6330cc76Bb26Cc490166CcAbf451234A578',
ERC20BalanceGteAfterAllEnforcer: '0x2AB6fed8C074D4Bf4668E46f78a97cCD1FC23686',
ExternalHookEnforcer: '0xF1dF0e6d2d6D307814A086cCdeBdcD1250C53df3',
} as const;

0 comments on commit 56797dd

Please sign in to comment.