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

Update SDK examples to use buildCallWithPermit2 #409

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
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
65 changes: 49 additions & 16 deletions examples/swaps/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,21 @@ import {
Swap,
SwapBuildOutputExactIn,
SwapBuildOutputExactOut,
Permit2Helper,
CHAINS,
} from '../../src';

import {
// createWalletClient,
// createPublicClient,
http,
createTestClient,
publicActions,
walletActions,
} from 'viem';
// import { privateKeyToAccount } from 'viem/accounts';
// import { polygon } from 'viem/chains';

const swap = async () => {
// User defined
const rpcUrl = process.env.POLYGON_RPC_URL;
Expand Down Expand Up @@ -77,30 +90,50 @@ const swap = async () => {
// Get up to date swap result by querying onchain
const queryOutput = await swap.query(rpcUrl);

// const client = createPublicClient({
// chain: polygon,
// transport: http(),
// });

const buildCallInput = {
slippage,
deadline,
queryOutput,
sender,
recipient,
wethIsEth,
};

const client = createTestClient({
mode: 'anvil',
chain: CHAINS[chainId],
transport: http(rpcUrl),
})
.extend(publicActions)
.extend(walletActions);

const permit2 = await Permit2Helper.signSwapApproval({
...buildCallInput,
client,
owner: sender,
});
MattPereira marked this conversation as resolved.
Show resolved Hide resolved

// Construct transaction to make swap
if (queryOutput.swapKind === SwapKind.GivenIn) {
console.log(`Updated amount: ${queryOutput.expectedAmountOut.amount}`);
const callData = swap.buildCall({
slippage,
deadline,
queryOutput,
sender,
recipient,
wethIsEth,
}) as SwapBuildOutputExactIn;
const callData = swap.buildCallWithPermit2(
buildCallInput,
permit2,
) as SwapBuildOutputExactIn;
console.log(
`Min Amount Out: ${callData.minAmountOut.amount}\n\nTx Data:\nTo: ${callData.to}\nCallData: ${callData.callData}\nValue: ${callData.value}`,
);
} else {
console.log(`Updated amount: ${queryOutput.expectedAmountIn.amount}`);
const callData = swap.buildCall({
slippage,
deadline,
queryOutput,
sender,
recipient,
wethIsEth,
}) as SwapBuildOutputExactOut;
const callData = swap.buildCallWithPermit2(
buildCallInput,
permit2,
) as SwapBuildOutputExactOut;
console.log(
`Max Amount In: ${callData.maxAmountIn.amount}\n\nTx Data:\nTo: ${callData.to}\nCallData: ${callData.callData}\nValue: ${callData.value}`,
);
Expand Down
Loading