Skip to content

Commit

Permalink
fix: limit order redemption
Browse files Browse the repository at this point in the history
  • Loading branch information
marthendalnunes committed Dec 12, 2024
1 parent 634059c commit 1c3c14a
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export function insertDelegationDb({
}: InsertDelegationDbParams) {
return db.transaction(async (tx) => {
// Insert the delegation
await tx.insert(delegationsDb).values(delegation).returning();
await tx
.insert(delegationsDb)
.values(delegation)
.returning()
.onConflictDoNothing();

if (caveats.length > 0) {
const caveatsWithDelegationHash = caveats.map((caveat) => ({
Expand All @@ -25,7 +29,11 @@ export function insertDelegationDb({
}));

// Insert the caveats
await tx.insert(caveatsDb).values(caveatsWithDelegationHash).returning();
await tx
.insert(caveatsDb)
.values(caveatsWithDelegationHash)
.returning()
.onConflictDoNothing();
}
});
}
13 changes: 8 additions & 5 deletions apps/api-delegations/src/processing/limit-order/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Delegation, DelegationExecution } from 'universal-types';
import {
decodeEnforcerERC20TransferAmount,
encodeDelegation,
encodeExternalCallEnforcerArgs,
encodeSingleExecution,
getErc20TransferAmountEnforcerFromDelegation,
getExternalHookEnforcerFromDelegation,
Expand All @@ -26,7 +27,6 @@ function getDepositAaveV3HookData({
}: { amount: bigint; delegator: Address; token: Address }): Hex {
return encodeFunctionData({
abi: multicallAbi,

functionName: 'multicall',
args: [
[
Expand Down Expand Up @@ -71,10 +71,13 @@ export async function processLimitOrderDelegation({
delegation.caveats[externalHookEnforcerIndex] = {
...externalHookEnforcer,
// Update the args of the external hook enforcer to include the data for the Aave V3 deposit
args: getDepositAaveV3HookData({
amount,
token,
delegator: delegation.delegator,
args: encodeExternalCallEnforcerArgs({
target: universalDeployments.Multicall,
callData: getDepositAaveV3HookData({
amount,
token,
delegator: delegation.delegator,
}),
}),
};

Expand Down
12 changes: 12 additions & 0 deletions apps/api-delegations/src/processing/limit-order/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,15 @@ export function encodeDelegation(delegation: Delegation): Hex {
[[delegation]],
);
}

export type EncodeExternalHookArgsParams = {
target: Address;
callData: Hex;
};

export function encodeExternalCallEnforcerArgs({
target,
callData,
}: EncodeExternalHookArgsParams) {
return encodePacked(['address', 'bytes'], [target, callData]);
}
5 changes: 4 additions & 1 deletion apps/api-delegations/src/routes/delegations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ const delegationsRouter = new Hono()
try {
await Promise.all([
// Process any necessary delegation side effects
processDelegation(delegation),
processDelegation({
chainId: delegation.chainId,
delegation,
}),
// Save the delegation to the database
insertDelegationDb(delegation),
]);
Expand Down
5 changes: 1 addition & 4 deletions apps/wallet/src/components/forms/form-erc20-swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { useSignErc20SwapDelegation } from 'universal-delegations-sdk';
import type { TokenItem } from 'universal-types';
import type { Address } from 'viem';
import { useAccount, useSwitchChain } from 'wagmi';
import { useWriteContracts } from 'wagmi/experimental';
import { z } from 'zod';

const formSchema = z.object({
Expand Down Expand Up @@ -40,9 +39,7 @@ type FormErc20SwapProps = {
function FormErc20Swap({ defaultValues }: FormErc20SwapProps) {
const { address } = useAccount();
const { switchChain, isPending: isPendingSwitchChain } = useSwitchChain();
const { signAndSaveDelegationAsync, isSuccess } =
useSignErc20SwapDelegation();
const { writeContracts } = useWriteContracts();
const { signAndSaveDelegationAsync } = useSignErc20SwapDelegation();

const { isValidChain, chainId, defaultChain } = useIsValidChain();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useInsertDelegation } from '../api/actions/insert-delegation.js';
import { eip712DelegationTypes } from '../delegation/eip712-delegation-type.js';
import { getDelegationHash } from '../delegation/get-delegation-hash.js';
import { encodeEnforcerERC20TransferAmount } from '../enforcers/enforcer-erc20-transfer-amount.js';
import { encodeERC20BalanceGteWrapEnforcerTerms } from '../enforcers/erc20-balance-gte-wrap-enforcer.js';
// import { encodeERC20BalanceGteWrapEnforcerTerms } from '../enforcers/erc20-balance-gte-wrap-enforcer.js';

type SignDelegationParams = {
chainId: number;
Expand Down Expand Up @@ -37,9 +37,9 @@ export function useSignErc20SwapDelegation() {
tokenOut = zeroAddress,
decimalsOut = 18,
amountOut = '0',
tokenIn = zeroAddress,
decimalsIn = 18,
amountIn = '0',
// tokenIn = zeroAddress,
// decimalsIn = 18,
// amountIn = '0',
}: SignDelegationParams) {
const signature = await signTypedDataAsync({
types: eip712DelegationTypes,
Expand All @@ -56,14 +56,13 @@ export function useSignErc20SwapDelegation() {
authority: ROOT_AUTHORITY,
salt: salt,
caveats: [
{
enforcer: universalDeployments.ERC20BalanceGteWrapEnforcer,
terms: encodeERC20BalanceGteWrapEnforcerTerms({
token: tokenIn,
amount: amountIn,
decimals: decimalsIn,
}),
},
// {
// enforcer: universalDeployments.ERC20BalanceGteWrapEnforcer,
// terms: encodeERC20BalanceGteWrapEnforcerTerms({
// token: tokenIn,
// amount: parseUnits(amountIn, decimalsIn),
// }),
// },
{
enforcer: universalDeployments.ERC20TransferAmountEnforcer,
terms: encodeEnforcerERC20TransferAmount({
Expand All @@ -87,15 +86,14 @@ export function useSignErc20SwapDelegation() {
salt,
signature,
caveats: [
{
enforcer: universalDeployments.ERC20BalanceGteWrapEnforcer,
terms: encodeERC20BalanceGteWrapEnforcerTerms({
token: tokenIn,
amount: amountIn,
decimals: decimalsIn,
}),
args: '0x',
},
// {
// enforcer: universalDeployments.ERC20BalanceGteWrapEnforcer,
// terms: encodeERC20BalanceGteWrapEnforcerTerms({
// token: tokenIn,
// amount: parseUnits(amountIn, decimalsIn),
// }),
// args: '0x',
// },
{
enforcer: universalDeployments.ERC20TransferAmountEnforcer,
terms: encodeEnforcerERC20TransferAmount({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type EncodeExternalHookArgsParams = {
callData: Hex;
};

export function encodeExternalHookArgs({
export function encodeExternalCallEnforcerArgs({
target,
callData,
}: EncodeExternalHookArgsParams) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ import {
type Hex,
encodePacked,
hexToBigInt,
parseUnits,
sliceHex,
} from 'viem';

export function encodeERC20BalanceGteWrapEnforcerTerms(data: {
export function encodeERC20BalanceGteWrapEnforcerTerms({
amount,
token,
}: {
amount: bigint;
token: Address;
amount: string;
decimals: number;
}) {
return encodePacked(
['address', 'uint256'],
[data.token, parseUnits(data.amount, data.decimals)],
);
return encodePacked(['address', 'uint256'], [token, amount]);
}

export function decodeERC20BalanceGteWrapEnforcerTerms(data: Hex) {
Expand Down

This file was deleted.

3 changes: 1 addition & 2 deletions packages/universal-delegations-sdk/src/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export {
encodeEnforcerERC20TransferAmount,
getErc20TransferAmountEnforcerFromDelegation,
} from '../enforcers/enforcer-erc20-transfer-amount.js';
export { encodeExternalHookArgs } from '../enforcers/enforcer-external-hook.js';
export { encodeExternalCallEnforcerArgs } from '../enforcers/enforcer-external-call.js';
export { eip712DelegationTypes } from '../delegation/eip712-delegation-type.js';
export { encodeDelegation } from '../delegation/encode-delegation.js';
export {
Expand All @@ -41,6 +41,5 @@ export {
decodeERC20BalanceGteWrapEnforcerTerms,
encodeERC20BalanceGteWrapEnforcerTerms,
} from '../enforcers/erc20-balance-gte-wrap-enforcer.js';
export { encodeExternalHookEnforcerArgs } from '../enforcers/external-call-enforcer.js';
export { encodeBatchExecution } from '../execution/encode-batch-execution.js';
export { encodeSingleExecution } from '../execution/encode-single-execution.js';

0 comments on commit 1c3c14a

Please sign in to comment.