Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/Delta support #183

Merged
merged 186 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 177 commits
Commits
Show all changes
186 commits
Select commit Hold shift + click to select a range
cad44c9
update build dependencies
Velenir Sep 12, 2024
974a802
update tsconfig
Velenir Sep 12, 2024
14294e3
actions/remove Node18
Velenir Sep 12, 2024
93b7826
fix types
Velenir Sep 12, 2024
2109de9
extra gitignore
Velenir Sep 12, 2024
da9d495
update typedoc deps
Velenir Sep 12, 2024
eac78e6
update snapshots
Velenir Sep 12, 2024
feb6631
abstract away providers/findPrimaryType
Velenir Sep 17, 2024
8a36c72
add TransactionParams.maxFeeGas params
Velenir Sep 17, 2024
35f45dd
providers/viem
Velenir Sep 17, 2024
ab08a0e
export from providers/viem
Velenir Sep 17, 2024
5930b5c
DEFAULT_VERSION better type
Velenir Sep 17, 2024
ee465f9
add wagmi & viem deps
Velenir Sep 17, 2024
2120841
SimpleSDK/viem for contractCaller
Velenir Sep 17, 2024
dc7b9a8
examples/viem
Velenir Sep 17, 2024
b9216f4
cleanup
Velenir Sep 17, 2024
ed18b71
examples/wagmi
Velenir Sep 17, 2024
c785a04
fix dependencies
Velenir Sep 17, 2024
b463812
Merge branch 'feat/build_upgrade' into feat/viem_support
Velenir Sep 17, 2024
12644b9
tests/fix types
Velenir Sep 18, 2024
1740831
examples/fix types
Velenir Sep 18, 2024
582eefc
tests/fix types
Velenir Sep 18, 2024
9e99ee1
viem/fix account usage
Velenir Sep 19, 2024
dfa0e2b
examples/viem/fix account usage
Velenir Sep 19, 2024
0e8a916
types/TxHash=Hex
Velenir Sep 19, 2024
1d6ed0f
add hardhat
Velenir Sep 19, 2024
33fc15e
legacy/fix types
Velenir Sep 19, 2024
bd73372
ignore cache files
Velenir Sep 19, 2024
071dc3d
some tests
Velenir Sep 19, 2024
c57d043
tsconfig/allow BigInt short notation
Velenir Sep 19, 2024
dc8c3c2
providers/viem/reuse account
Velenir Sep 19, 2024
a0e80e9
viem/fix tests
Velenir Sep 19, 2024
c3b72ac
tests/viem/remove extra
Velenir Sep 19, 2024
84382f8
tests/viem/Order signing
Velenir Sep 19, 2024
d8f82fc
rename stuff
Velenir Sep 19, 2024
7f22798
cleanup
Velenir Sep 19, 2024
7327226
reenable tests
Velenir Sep 19, 2024
b068165
tests/viem/const acc
Velenir Sep 19, 2024
ab1ded2
tests/viem/update snapshots
Velenir Sep 19, 2024
ff055fb
reexport txParamsToViemTxParams
Velenir Sep 19, 2024
9d1fdb3
tests/viem/update snapshots
Velenir Sep 19, 2024
c9de532
more comments
Velenir Sep 19, 2024
c94a386
cleanup
Velenir Sep 19, 2024
536efb9
remove extra deps
Velenir Sep 19, 2024
4f3df54
moved bignumber.js to devDeps
Velenir Sep 19, 2024
8399183
cleanup
Velenir Sep 19, 2024
45f72d4
update tests jpeg
Velenir Sep 19, 2024
fc3976f
update README
Velenir Sep 19, 2024
5c35317
update snapshots
Velenir Oct 22, 2024
b637c47
FetcherError/fix types
Velenir Oct 22, 2024
b368e93
viem test/market swap
Velenir Oct 22, 2024
4719ce0
fix deps
Velenir Oct 22, 2024
64f3216
update deps
Velenir Oct 22, 2024
e56cf76
update snapshots
Velenir Oct 22, 2024
085823b
remove dummy test
Velenir Oct 22, 2024
332d98b
change default version
Velenir Oct 22, 2024
5d392d7
Merge branch 'master' into feat/build_upgrade
Velenir Oct 23, 2024
24e868c
Merge branch 'feat/build_upgrade' into feat/viem_support
Velenir Oct 23, 2024
e11a89c
update some deps
Velenir Oct 23, 2024
57a9f24
replace ganache with hardhat
Velenir Oct 23, 2024
ed68254
remove ganache
Velenir Oct 23, 2024
15887c1
fix chainId in tests mismatch
Velenir Oct 23, 2024
cc2d0a3
try with tevm
Velenir Oct 30, 2024
27adcce
tevm test
Velenir Oct 30, 2024
0d59580
add hardhat-switch-network plugin
Velenir Oct 31, 2024
641005c
simplify hardhat helpers
Velenir Oct 31, 2024
292c084
make tests work with hardhat
Velenir Oct 31, 2024
3201dc4
fix method name
Velenir Oct 31, 2024
dfe8d85
disable web3 tests that break with hardhat
Velenir Oct 31, 2024
1f3543d
fix NFT tests
Velenir Oct 31, 2024
24b2f6b
remove tevm
Velenir Oct 31, 2024
0ac1d30
hardhat config/explicit gasPrice
Velenir Oct 31, 2024
8a5d211
NFT tests/adjust amounts
Velenir Oct 31, 2024
f29ad59
cleanup
Velenir Oct 31, 2024
04ef27f
remove temp tests
Velenir Oct 31, 2024
6360f0f
update some deps
Velenir Oct 31, 2024
167f353
NFT tests/workaround some errors
Velenir Oct 31, 2024
82c6c7f
update ethers dep
Velenir Oct 31, 2024
0383da6
update web3 dep(slightly)
Velenir Oct 31, 2024
929b018
update deps
Velenir Oct 31, 2024
570622f
override some deps
Velenir Oct 31, 2024
2f807f7
update some deps
Velenir Nov 1, 2024
2ea62c9
hadrhat config/smaller default baseFee
Velenir Nov 1, 2024
edd167b
NFT tests/workaround edge case
Velenir Nov 1, 2024
b414a35
update web3 dep
Velenir Nov 2, 2024
bd3afc4
update Web3 types
Velenir Nov 2, 2024
f4fdaf5
update web3/constructContractCaller
Velenir Nov 2, 2024
0c72307
legacy/update types
Velenir Nov 2, 2024
98b9e7c
hardhat config/fixed accounts
Velenir Nov 2, 2024
dc20224
LOrder tests/reenable sign with web3
Velenir Nov 2, 2024
e66c42d
NFT Order tests/reenable sign with web3
Velenir Nov 2, 2024
dda380c
getBalance tests workaround
Velenir Nov 2, 2024
73e5fab
cleanup
Velenir Nov 2, 2024
23812f6
update some deps
Velenir Nov 2, 2024
55d43a3
update snapshots
Velenir Nov 2, 2024
eab3190
update perrDeps
Velenir Nov 2, 2024
1410e67
update required Node v
Velenir Nov 2, 2024
4fb50ba
NFT tests/account for dust
Velenir Nov 2, 2024
3978301
hadrhat config/lower initialBaseFeePerGas
Velenir Nov 5, 2024
c6007a0
install ethersV6 as alias
Velenir Nov 5, 2024
850c61a
providers/ethersV6
Velenir Nov 5, 2024
2e1a118
distinct ethersV5 exports
Velenir Nov 5, 2024
a522f47
legacy/support ethersV6
Velenir Nov 5, 2024
d402eb9
simple SDK/support ethersV6
Velenir Nov 5, 2024
b567f64
simpleSDK.tests/add ethersV6
Velenir Nov 5, 2024
0bcb538
partialSDK.tests/add ethersV6
Velenir Nov 5, 2024
648a5f6
LOrders.tests/add ethersV6
Velenir Nov 5, 2024
e00a65a
NFT_Orders.tests/add ethersV6
Velenir Nov 5, 2024
9639ea7
update snapshots
Velenir Nov 5, 2024
e94a5e1
examples/ add ethersV6
Velenir Nov 5, 2024
92e57a4
perrDeps/update ethers versions
Velenir Nov 5, 2024
5a89ef9
update snapshots
Velenir Nov 5, 2024
0df1bf4
cleanup
Velenir Nov 5, 2024
f09cce5
delta/approve
Velenir Nov 7, 2024
9885605
delta/getContract
Velenir Nov 7, 2024
b92dccd
delta/getOrders
Velenir Nov 7, 2024
5c3bf86
delta/some types
Velenir Nov 7, 2024
43ef400
delta/getPrice
Velenir Nov 7, 2024
0721865
delta/getPartnerFee
Velenir Nov 7, 2024
5b357fe
delta/sanitizeOrderData
Velenir Nov 7, 2024
c92f9b9
delta/composePermit
Velenir Nov 7, 2024
3fbd1f7
delta/buildOrderData
Velenir Nov 7, 2024
95db6c3
delta/buildData
Velenir Nov 7, 2024
05946bc
delta/signOrder
Velenir Nov 7, 2024
c405936
extend AdaptersContractsResult
Velenir Nov 7, 2024
4c24256
delta/postOrder
Velenir Nov 7, 2024
4e990e6
delta/submitOrder & allHandlers
Velenir Nov 7, 2024
2dc0233
delta/buildOrder/explicit BuildDeltaOrderDataParams
Velenir Nov 7, 2024
dbf3804
add PartnerFeeResponse.takeSurplus
Velenir Nov 8, 2024
e26d523
reexport Delta methods & types
Velenir Nov 8, 2024
5b32abd
BuildDeltaOrderDataParams.partner?
Velenir Nov 8, 2024
87ef000
DeltaPriceParams.*decimals: required
Velenir Nov 8, 2024
a8f8c29
examples/delta
Velenir Nov 8, 2024
b7c77a0
fullSDK/delta methods
Velenir Nov 8, 2024
cd268a8
simpleSDK/delta methods
Velenir Nov 8, 2024
797c847
producePartnerAndFee |= (takeSuplus << 8)
Velenir Nov 8, 2024
ce9a82a
examples/delta/manualDeltaFlow
Velenir Nov 8, 2024
61885f2
cleanup
Velenir Nov 8, 2024
48e619e
move ethers types to provider/ethers
Velenir Nov 8, 2024
1098429
move web3 types to provider/web3
Velenir Nov 8, 2024
7f12f34
untie FetchError type from AxiosError
Velenir Nov 8, 2024
e20b6ad
cleanup
Velenir Nov 8, 2024
e887e6c
reexport DeltaPriceParams
Velenir Nov 8, 2024
9953b66
ethers -> ethersV5, ethersV6 -> ethers to fix types when used as lib
Velenir Nov 9, 2024
387b427
deriveCompactSignature
Velenir Nov 9, 2024
2633abc
signDeltaOrder/deriveCompactSignature
Velenir Nov 9, 2024
1eee8ae
move ethers types to provider/ethers
Velenir Nov 8, 2024
4e9e56a
move web3 types to provider/web3
Velenir Nov 8, 2024
e10b1bc
untie FetchError type from AxiosError
Velenir Nov 8, 2024
c165dd4
ethers -> ethersV5, ethersV6 -> ethers to fix types when used as lib
Velenir Nov 9, 2024
f550265
cleanup
Velenir Nov 8, 2024
6369f4a
Merge branch 'feat/etherV6_support' into feat/delta
Velenir Nov 9, 2024
3455fe1
fetch/remove unnecessary headers
Velenir Nov 9, 2024
cbb1e68
delta/examples/fix slippage
Velenir Nov 11, 2024
432b6f4
reexport more types
Velenir Nov 11, 2024
f4269c3
cleanup
Velenir Nov 11, 2024
6f8f565
delta/getPrices/fix query params
Velenir Nov 11, 2024
5d066e7
type DeltaPrice/add fields
Velenir Nov 11, 2024
9eec5ef
Partial/delta/approve/fix tx type
Velenir Nov 11, 2024
e1720be
ethersV6/more explicit Contract method calling
Velenir Nov 11, 2024
3b03b05
delta/add tests
Velenir Nov 11, 2024
df4e822
delta/getPartnerFee/temp workaround for API response
Velenir Nov 11, 2024
36fb501
README/update version
Velenir Nov 11, 2024
1ba0be8
README/update version
Velenir Nov 11, 2024
2a47009
less dependency on ethers types
Velenir Nov 11, 2024
9103ded
update README
Velenir Nov 11, 2024
ace4561
Merge branch 'feat/etherV6_support' into feat/delta
Velenir Nov 11, 2024
6535106
cleanup
Velenir Nov 11, 2024
68cb246
remove default partner
Velenir Nov 12, 2024
b2df52c
Merge branch 'master' into feat/delta
Velenir Nov 15, 2024
08b8a8d
remove temp tests
Velenir Nov 16, 2024
eb39af5
ParaswapDeltaAuction.orderHash: string | null
Velenir Nov 16, 2024
9671e2a
cleanup
Velenir Nov 16, 2024
8828039
fix typos
Velenir Nov 16, 2024
55780f3
cleanup
Velenir Nov 16, 2024
a07084d
replace deprecated method
Velenir Nov 16, 2024
7a881f1
Delta/signDeltaOrder/skip compact sig when incompatible
Velenir Nov 16, 2024
63668af
update Delta Order types
Velenir Nov 28, 2024
da9351e
update tests
Velenir Nov 28, 2024
30cf0de
fix typo
Velenir Dec 3, 2024
68467fe
Delta/update types
Velenir Dec 3, 2024
b8abf85
tests/Delta/update snapshots
Velenir Dec 3, 2024
59f66f7
Merge branch 'master' into feat/delta
Velenir Dec 3, 2024
2a0f9ae
Delta/annotate some types
Velenir Dec 4, 2024
e69c88c
Delta/getDEltaOrders/pagination options
Velenir Dec 5, 2024
d571daf
Feat/Quote support (#185)
Velenir Dec 9, 2024
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,4 @@ Refer to [SDK API documentation](docs/md/modules.md) for detailed documentation
To run `yarn test` it is necessary to provide `PROVIDER_URL=<mainnet_rpc_url>` environment variable.
If it is necessary to run tests against a different API endpoint, provide `API_URL=url_to_API` environment variable.

<img src="./docs/passed_tests.jpg" width=350 />
<img src="./docs/passed_tests.png" width=350 />
Binary file added docs/passed_tests.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
125 changes: 125 additions & 0 deletions src/examples/delta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import axios from 'axios';
import { ethers, Wallet } from 'ethersV5';
import {
constructPartialSDK,
constructEthersContractCaller,
constructAxiosFetcher,
constructAllDeltaOrdersHandlers,
} from '..';

const fetcher = constructAxiosFetcher(axios);

const provider = ethers.getDefaultProvider(1);
const signer = Wallet.createRandom().connect(provider);
const account = signer.address;
const contractCaller = constructEthersContractCaller({
ethersProviderOrSigner: provider,
EthersContract: ethers.Contract,
});

// type AdaptersFunctions & ApproveTokenFunctions<ethers.ContractTransaction>
const deltaSDK = constructPartialSDK(
{
chainId: 1,
fetcher,
contractCaller,
},
constructAllDeltaOrdersHandlers
);

const DAI_TOKEN = '0x6b175474e89094c44da98b954eedeac495271d0f';
const PSP_TOKEN = '0xcafe001067cdef266afb7eb5a286dcfd277f3de5';

async function simpleDeltaFlow() {
const amount = '1000000000000'; // wei

const deltaPrice = await deltaSDK.getDeltaPrice({
srcToken: DAI_TOKEN,
destToken: PSP_TOKEN,
amount: '1000000000000',
userAddress: account,
srcDecimals: 18,
destDecimals: 18,
// partner: "..." // if available
});

const DeltaContract = await deltaSDK.getDeltaContract();

// or sign a Permit1 or Permit2 TransferFrom for DeltaContract
await deltaSDK.approveTokenForDelta(amount, DAI_TOKEN);

const slippagePercent = 0.5;
const destAmountAfterSlippage = BigInt(
// get rid of exponential notation

+(+deltaPrice.destAmount * (1 - slippagePercent / 100)).toFixed(0)
// get rid of decimals
).toString(10);

const deltaAuction = await deltaSDK.submitDeltaOrder({
deltaPrice,
owner: account,
// beneficiary: anotherAccount, // if need to send destToken to another account
// permit: "0x1234...", // if signed a Permit1 or Permit2 TransferFrom for DeltaContract
srcToken: DAI_TOKEN,
destToken: PSP_TOKEN,
srcAmount: amount,
destAmount: destAmountAfterSlippage, // minimum acceptable destAmount
});

// poll if necessary
const auction = await deltaSDK.getDeltaOrderById(deltaAuction.id);
if (auction?.status === 'EXECUTED') {
console.log('Auction was executed');
}
}
async function manualDeltaFlow() {
const amount = '1000000000000'; // wei

const deltaPrice = await deltaSDK.getDeltaPrice({
srcToken: DAI_TOKEN,
destToken: PSP_TOKEN,
amount: '1000000000000',
userAddress: account,
srcDecimals: 18,
destDecimals: 18,
// partner: "..." // if available
});

const DeltaContract = await deltaSDK.getDeltaContract();

// or sign a Permit1 or Permit2 TransferFrom for DeltaContract
await deltaSDK.approveTokenForDelta(amount, DAI_TOKEN);

const slippagePercent = 0.5;
const destAmountAfterSlippage = (
+deltaPrice.destAmount *
(1 - slippagePercent / 100)
).toString(10);

const signableOrderData = await deltaSDK.buildDeltaOrder({
deltaPrice,
owner: account,
// beneficiary: anotherAccount, // if need to send destToken to another account
// permit: "0x1234...", // if signed a Permit1 or Permit2 TransferFrom for DeltaContract
srcToken: DAI_TOKEN,
destToken: PSP_TOKEN,
srcAmount: amount,
destAmount: destAmountAfterSlippage, // minimum acceptable destAmount
});

const signature = await deltaSDK.signDeltaOrder(signableOrderData);

const deltaAuction = await deltaSDK.postDeltaOrder({
// partner: "..." // if available
order: signableOrderData.data,
signature,
});

// poll if necessary
const auction = await deltaSDK.getDeltaOrderById(deltaAuction.id);
if (auction?.status === 'EXECUTED') {
console.log('Auction was executed');
}
}
2 changes: 1 addition & 1 deletion src/helpers/fetchers/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const constructFetcher =

// adding apiKey to headers if it's provided
const apiHeaders = extra?.apiKey
? { 'X-API-KEY': extra.apiKey, ...params.headers }
? { 'X-API-KEY': extra.apiKey }
: undefined;

// all headers combined
Expand Down
54 changes: 54 additions & 0 deletions src/helpers/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,57 @@ export function runOnceAndCache<T, Args extends any[]>(
return result ?? (result = func(...args));
};
}

export function deriveCompactSignature(signature: string): string {
// Remove "0x" prefix if present
if (signature.startsWith('0x')) {
signature = signature.slice(2);
}

// Convert the hex string to a byte array
const bytes = new Uint8Array(signature.length / 2);
for (let i = 0; i < signature.length; i += 2) {
bytes[i / 2] = parseInt(signature.slice(i, i + 2), 16);
}

// Validate the signature length (64 or 65 bytes)
if (bytes.length !== 64 && bytes.length !== 65) {
throw new Error('Invalid signature length: must be 64 or 65 bytes');
}

// Extract r and s components
const r = `0x${Array.from(bytes.slice(0, 32), (b) =>
b.toString(16).padStart(2, '0')
).join('')}`;
let v;

// Handle 64-byte (EIP-2098 compact) and 65-byte signatures
if (bytes.length === 64) {
// Extract v from the highest bit of s and clear the bit in s
v = 27 + (bytes[32]! >> 7);
bytes[32]! &= 0x7f; // Clear the highest bit
} else {
// Extract v directly for 65-byte signature
v = bytes[64]!;

// Normalize v to canonical form (27 or 28)
if (v < 27) {
v += 27;
}
}

// Compute yParityAndS (_vs) for the compact signature
const sBytes = Array.from(bytes.slice(32, 64));
if (v === 28) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
sBytes[0]! |= 0x80; // Set the highest bit if v is 28
}
const yParityAndS = `0x${sBytes
.map((b) => b.toString(16).padStart(2, '0'))
.join('')}`;

// Construct the compact signature by concatenating r and yParityAndS
const compactSignature = r + yParityAndS.slice(2);

return compactSignature;
}
6 changes: 3 additions & 3 deletions src/helpers/providers/ethersV6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export const constructContractCaller = (

const callableContractFunction = contract.getFunction(contractMethod);

// returns whatever the Contract.method returns: BigNumber, string, boolean
return callableContractFunction(...args, normalizedOverrides);
// returns whatever the Contract["method"].staticCall returns: BigNumber, string, boolean
return callableContractFunction.staticCall(...args, normalizedOverrides);
};

const transactCall: TransactionContractCallerFn<
Expand Down Expand Up @@ -77,7 +77,7 @@ export const constructContractCaller = (
// if no method for contractMethod, ethers will throw
const callableContractFunction = contract.getFunction(contractMethod);

const txResponse = await callableContractFunction(
const txResponse = await callableContractFunction.send(
...args,
normalizedOverrides
);
Expand Down
73 changes: 73 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,48 @@ import type {
ParaSwapVersionUnion as ParaSwapVersion,
} from './types';

import type {
DeltaAuctionOrder,
ParaswapDeltaAuction,
} from './methods/delta/helpers/types';
import {
BuildDeltaOrderDataParams,
BuildDeltaOrderFunctions,
constructBuildDeltaOrder,
SignableDeltaOrderData,
} from './methods/delta/buildDeltaOrder';
import {
constructPostDeltaOrder,
PostDeltaOrderFunctions,
PostDeltaOrderParams,
} from './methods/delta/postDeltaOrder';
import {
constructSignDeltaOrder,
SignDeltaOrderFunctions,
} from './methods/delta/signDeltaOrder';
import {
GetDeltaContractFunctions,
constructGetDeltaContract,
} from './methods/delta/getDeltaContract';
import {
constructGetDeltaPrice,
GetDeltaPriceFunctions,
DeltaPrice,
DeltaPriceParams,
} from './methods/delta/getDeltaPrice';
import {
constructGetDeltaOrders,
GetDeltaOrdersFunctions,
} from './methods/delta/getDeltaOrders';
import {
ApproveTokenForDeltaFunctions,
constructApproveTokenForDelta,
} from './methods/delta/approveForDelta';
import {
constructGetPartnerFee,
GetPartnerFeeFunctions,
} from './methods/delta/getPartnerFee';

export { constructSwapSDK, SwapSDKMethods } from './methods/swap';

export {
Expand All @@ -139,6 +181,12 @@ export {
LimitOrderHandlers,
} from './methods/limitOrders';

export {
constructAllDeltaOrdersHandlers,
constructSubmitDeltaOrder,
DeltaOrderHandlers,
} from './methods/delta';

export type {
TransactionParams,
BuildOptions,
Expand Down Expand Up @@ -198,7 +246,16 @@ export {
constructApproveTokenForNFTOrder,
constructGetNFTOrdersContract,
constructBuildNFTOrderTx,
// Delta methods
constructBuildDeltaOrder,
constructPostDeltaOrder,
constructSignDeltaOrder,
constructGetDeltaContract,
constructGetDeltaPrice,
constructGetDeltaOrders,
constructApproveTokenForDelta,
// different helpers
constructGetPartnerFee,
constructEthersContractCaller, // same as constructEthersV5ContractCaller for backwards compatibility
constructEthersV5ContractCaller,
constructEthersV6ContractCaller,
Expand Down Expand Up @@ -256,11 +313,27 @@ export type {
BuildNFTOrderInput,
BuildNFTOrderDataInput,
NFTOrdersUserParams,
//types for Delta methods
DeltaPrice,
DeltaPriceParams,
DeltaAuctionOrder,
ParaswapDeltaAuction,
BuildDeltaOrderDataParams,
BuildDeltaOrderFunctions,
SignableDeltaOrderData,
PostDeltaOrderFunctions,
PostDeltaOrderParams,
SignDeltaOrderFunctions,
GetDeltaContractFunctions,
GetDeltaPriceFunctions,
GetDeltaOrdersFunctions,
ApproveTokenForDeltaFunctions,
//common
ConstructFetchInput,
ContractCallerFunctions,
ConstructProviderFetchInput,
// other types
GetPartnerFeeFunctions,
Token,
Address,
AddressOrSymbol,
Expand Down
35 changes: 35 additions & 0 deletions src/methods/delta/approveForDelta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { ConstructProviderFetchInput } from '../../types';
import { ApproveToken, approveTokenMethodFactory } from '../../helpers/approve';
import { constructGetDeltaContract } from './getDeltaContract';

export type ApproveTokenForDeltaFunctions<T> = {
/** @description approving ParaswapDelta as spender for Token */
approveTokenForDelta: ApproveToken<T>;
};

// returns whatever `contractCaller` returns
// to allow for better versatility
export const constructApproveTokenForDelta = <T>(
options: ConstructProviderFetchInput<T, 'transactCall'>
): ApproveTokenForDeltaFunctions<T> => {
// getAugustusRFQ is cached internally for the same instance of SDK
// so should persist across same apiUrl & network
const { getDeltaContract } = constructGetDeltaContract(options);

const getParaswapDelta = async () => {
const deltaContract = await getDeltaContract();
if (!deltaContract) {
throw new Error(`Delta is not available on chain ${options.chainId}`);
}
return deltaContract;
};

const approveTokenForDelta: ApproveToken<T> = approveTokenMethodFactory<T>(
options.contractCaller,
getParaswapDelta
);

return {
approveTokenForDelta,
};
};
Loading
Loading