Skip to content

Commit 4dcd169

Browse files
Merge pull request #353 from LIT-Protocol/feat/multistep-uniswap-ability
Feat/multistep uniswap ability
2 parents 82a6921 + 83e4a0c commit 4dcd169

30 files changed

+1781
-866
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
ability-sdk: minor
3+
---
4+
5+
Export new methods: sponsoredGasContractCall and sponsoredGasRawTransaction to allow for Alchemy gas sponsorship of broadcast transaction from a Vincent Ability
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
ability-uniswap-swap: major
3+
---
4+
5+
Adds new Ability parameters that dictate what the Ability does (e.g. ERC20 allowance approvals, Uniswap Swaps, or both). Additionally, adds support for Alchemy gas sponsorship of both ERC20 approval and Uniswap swap txs. The Ability now returns failure reponses for known errors like insufficient balances or allowances

packages/apps/abilities-e2e/test-e2e/swap.spec.ts

Lines changed: 591 additions & 350 deletions
Large diffs are not rendered by default.

packages/apps/ability-uniswap-swap/src/generated/lit-action-prepare.js

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

packages/apps/ability-uniswap-swap/src/generated/lit-action.js

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"ipfsCid": "QmTvPwj165nh8ps6Zxfi6yhS4qpb5aQQGV5vj7i4Zfp5cV"
2+
"ipfsCid": "QmS5hyaSvdcjgMDeEfybzTESHpRTj4aJsagqfu6VLkVVcB"
33
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"ipfsCid": "QmTHxA1szqumjfndvB8D59qRPhrhMsJWr6jTq81RFeaf1D",
3-
"pkpTokenId": "44317236434387694874531930309258310489704884861516325543415539434780397033801",
4-
"pkpPublicKey": "0x04c2aed3bbbe57674ec7a0b2e094d8e365415508dd9725bf1b7c8a526e58a75c2e7f2a830a50ac7fc5d26610ba23ad81be1eb8a032f3f813db67d2324df6ecd650",
5-
"pkpEthAddress": "0xa7A476c03a04C1aa86Ad2319594376a124E7f598"
2+
"ipfsCid": "QmcvRUyTiRwaw5CzuYhFwrJbUkfegMS9gnFh8Z5ciBgg1u",
3+
"pkpTokenId": "111900264250323532308709640357423227654246808641396976251148600630865853586661",
4+
"pkpPublicKey": "0x04e68f03f0dd111c94f4dd890c20f06edf4256050452256fea1a214c1a7f6acfaaf25f5f152caaf5d1774e1f2fd9bac4da33fcb75a6ce2acef3e8eb34d7ef0eabc",
5+
"pkpEthAddress": "0xbe37409C68283fAA2eC57e6c1EC0dD96421A391B"
66
}

packages/apps/ability-uniswap-swap/src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,15 @@ export { bundledVincentAbility } from './generated/vincent-bundled-ability';
22
export { getSignedUniswapQuote } from './lib/prepare/get-signed-uniswap-quote';
33
export { validateSignedUniswapQuote } from './lib/prepare/validate-signed-uniswap-quote';
44
export type * from './lib/prepare/types';
5+
export type {
6+
CheckNativeTokenBalanceResult,
7+
CheckNativeTokenBalanceResultSuccess,
8+
CheckNativeTokenBalanceResultFailure,
9+
CheckErc20BalanceResult,
10+
CheckErc20BalanceResultSuccess,
11+
CheckErc20BalanceResultFailure,
12+
CheckErc20AllowanceResult,
13+
CheckErc20AllowanceResultSuccess,
14+
CheckErc20AllowanceResultFailure,
15+
} from './lib/types';
16+
export { AbilityAction } from './lib/types';
Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,44 @@
11
import { ethers } from 'ethers';
2-
import { getErc20Contract } from './getErc20Contract';
2+
3+
import { getErc20Allowance } from '../ability-helpers/get-erc20-allowance';
4+
import { CheckErc20AllowanceResult } from '../types';
35

46
export const checkErc20Allowance = async ({
57
provider,
68
tokenAddress,
79
owner,
810
spender,
9-
tokenAmount,
11+
requiredAllowance,
1012
}: {
1113
provider: ethers.providers.StaticJsonRpcProvider;
1214
tokenAddress: string;
1315
owner: string;
1416
spender: string;
15-
tokenAmount: bigint;
16-
}): Promise<ethers.BigNumber> => {
17-
const contract = getErc20Contract(tokenAddress, provider);
18-
19-
const currentAllowance = await contract.allowance(owner, spender);
20-
21-
// Convert bigint to BigNumber for comparison
22-
const tokenAmountBN = ethers.BigNumber.from(tokenAmount.toString());
17+
requiredAllowance: ethers.BigNumber;
18+
}): Promise<CheckErc20AllowanceResult> => {
19+
const currentAllowance = await getErc20Allowance({
20+
provider,
21+
tokenAddress,
22+
owner,
23+
spender,
24+
});
2325

24-
if (currentAllowance.lt(tokenAmountBN)) {
25-
throw new Error(
26-
`Address ${owner} has insufficient ERC20 allowance for spender ${spender} for token ${tokenAddress} (checkErc20Allowance)`,
27-
);
26+
if (currentAllowance.lt(requiredAllowance)) {
27+
return {
28+
success: false,
29+
reason: `[checkErc20Allowance] Address ${owner} has insufficient ERC20 allowance for spender ${spender} for token ${tokenAddress}`,
30+
spenderAddress: spender,
31+
tokenAddress: tokenAddress,
32+
requiredAllowance,
33+
currentAllowance,
34+
};
2835
}
2936

30-
return currentAllowance;
37+
return {
38+
success: true,
39+
spenderAddress: spender,
40+
tokenAddress: tokenAddress,
41+
requiredAllowance,
42+
currentAllowance,
43+
};
3144
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { ethers } from 'ethers';
2+
3+
import { getErc20Contract } from '../ability-helpers/get-erc20-contract';
4+
import { CheckErc20BalanceResult } from '../types';
5+
6+
export const checkErc20Balance = async ({
7+
provider,
8+
pkpEthAddress,
9+
tokenAddress,
10+
requiredTokenAmount,
11+
}: {
12+
provider: ethers.providers.StaticJsonRpcProvider;
13+
pkpEthAddress: string;
14+
tokenAddress: string;
15+
requiredTokenAmount: ethers.BigNumber;
16+
}): Promise<CheckErc20BalanceResult> => {
17+
const contract = getErc20Contract(tokenAddress, provider);
18+
19+
const tokenBalance: ethers.BigNumber = await contract.balanceOf(pkpEthAddress);
20+
21+
if (tokenBalance.lt(requiredTokenAmount)) {
22+
return {
23+
success: false,
24+
reason: `pkpEthAddress (${pkpEthAddress}) has insufficient balance of tokenIn (${tokenAddress}). Wanted ${requiredTokenAmount}, but only have ${tokenBalance}`,
25+
tokenAddress,
26+
requiredTokenAmount,
27+
tokenBalance,
28+
};
29+
}
30+
31+
return {
32+
success: true,
33+
tokenAddress,
34+
requiredTokenAmount,
35+
tokenBalance,
36+
};
37+
};

0 commit comments

Comments
 (0)