Update contracts; fix evm approve and swap

This commit is contained in:
Karl Kempe 2022-01-27 18:28:36 +00:00
parent 5629f2fae8
commit 3154c74c73
4 changed files with 103 additions and 64 deletions

View File

@ -44,7 +44,8 @@ contract CrossChainSwapV2 {
uint8 public immutable typeExactOut = 2; uint8 public immutable typeExactOut = 2;
uint8 public immutable typeNativeSwap = 1; uint8 public immutable typeNativeSwap = 1;
uint8 public immutable typeTokenSwap = 2; uint8 public immutable typeTokenSwap = 2;
uint16 public immutable expectedVaaLength = 262; uint16 public immutable expectedVaaLength = 274;
uint8 public immutable terraChainId = 3;
IUniswapV2Router02 public immutable swapRouter; IUniswapV2Router02 public immutable swapRouter;
address public immutable feeTokenAddress; address public immutable feeTokenAddress;
address public immutable tokenBridgeAddress; address public immutable tokenBridgeAddress;
@ -385,17 +386,26 @@ contract CrossChainSwapV2 {
swapParams.deadline swapParams.deadline
); );
// encode payload for second swap // create payload variable
bytes memory payload = abi.encodePacked( bytes memory payload;
swapParams.targetAmountOutMinimum,
swapParams.targetChainRecipient, // UST is native to Terra - no need for swap instructions
path[2], if (targetChainId == terraChainId) {
path[3], payload = abi.encodePacked(
swapParams.deadline, swapParams.targetChainRecipient
swapParams.poolFee, );
typeExactIn, } else {
typeNativeSwap payload = abi.encodePacked(
); swapParams.targetAmountOutMinimum,
swapParams.targetChainRecipient,
path[2],
path[3],
swapParams.deadline,
swapParams.poolFee,
typeExactIn,
typeNativeSwap
);
}
// approve token bridge to spend feeTokens (UST) // approve token bridge to spend feeTokens (UST)
TransferHelper.safeApprove( TransferHelper.safeApprove(
@ -761,17 +771,26 @@ contract CrossChainSwapV2 {
typeNativeSwap typeNativeSwap
); );
// encode payload for second swap // create payload variable
bytes memory payload = abi.encodePacked( bytes memory payload;
swapParams.targetAmountOut,
swapParams.targetChainRecipient, // UST is native to Terra - no need for swap instructions
path[2], if (targetChainId == terraChainId) {
path[3], payload = abi.encodePacked(
swapParams.deadline, swapParams.targetChainRecipient
swapParams.poolFee, );
typeExactOut, } else {
typeNativeSwap payload = abi.encodePacked(
); swapParams.targetAmountOut,
swapParams.targetChainRecipient,
path[2],
path[3],
swapParams.deadline,
swapParams.poolFee,
typeExactOut,
typeNativeSwap
);
}
// approve token bridge to spend feeTokens (UST) // approve token bridge to spend feeTokens (UST)
TransferHelper.safeApprove( TransferHelper.safeApprove(

View File

@ -48,7 +48,8 @@ contract CrossChainSwapV3 {
uint8 public immutable typeExactOut = 2; uint8 public immutable typeExactOut = 2;
uint8 public immutable typeNativeSwap = 1; uint8 public immutable typeNativeSwap = 1;
uint8 public immutable typeTokenSwap = 2; uint8 public immutable typeTokenSwap = 2;
uint16 public immutable expectedVaaLength = 262; uint16 public immutable expectedVaaLength = 274;
uint8 public immutable terraChainId = 3;
IUniswapRouter public immutable swapRouter; IUniswapRouter public immutable swapRouter;
address public immutable feeTokenAddress; address public immutable feeTokenAddress;
address public immutable tokenBridgeAddress; address public immutable tokenBridgeAddress;
@ -403,17 +404,26 @@ contract CrossChainSwapV3 {
typeNativeSwap typeNativeSwap
); );
// encode payload for second swap // create payload variable
bytes memory payload = abi.encodePacked( bytes memory payload;
swapParams.targetAmountOutMinimum,
swapParams.targetChainRecipient, // UST is native to Terra - no need for swap instructions
path[2], if (targetChainId == terraChainId) {
path[3], payload = abi.encodePacked(
swapParams.deadline, swapParams.targetChainRecipient
swapParams.poolFee, );
typeExactIn, } else {
typeNativeSwap payload = abi.encodePacked(
); swapParams.targetAmountOutMinimum,
swapParams.targetChainRecipient,
path[2],
path[3],
swapParams.deadline,
swapParams.poolFee,
typeExactIn,
typeNativeSwap
);
}
// approve token bridge to spend feeTokens (UST) // approve token bridge to spend feeTokens (UST)
TransferHelper.safeApprove( TransferHelper.safeApprove(
@ -788,17 +798,26 @@ contract CrossChainSwapV3 {
typeNativeSwap typeNativeSwap
); );
// encode payload for second swap // create payload variable
bytes memory payload = abi.encodePacked( bytes memory payload;
swapParams.targetAmountOut,
swapParams.targetChainRecipient, // UST is native to Terra - no need for swap instructions
path[2], if (targetChainId == terraChainId) {
path[3], payload = abi.encodePacked(
swapParams.deadline, swapParams.targetChainRecipient
swapParams.poolFee, );
typeExactOut, } else {
typeNativeSwap payload = abi.encodePacked(
); swapParams.targetAmountOut,
swapParams.targetChainRecipient,
path[2],
path[3],
swapParams.deadline,
swapParams.poolFee,
typeExactOut,
typeNativeSwap
);
}
// approve token bridge to spend feeTokens (UST) // approve token bridge to spend feeTokens (UST)
TransferHelper.safeApprove( TransferHelper.safeApprove(

View File

@ -18,7 +18,7 @@ library SwapHelper {
uint256 amountIn; uint256 amountIn;
uint256 amountOutMinimum; uint256 amountOutMinimum;
uint256 targetAmountOutMinimum; uint256 targetAmountOutMinimum;
address targetChainRecipient; bytes32 targetChainRecipient;
uint256 deadline; uint256 deadline;
uint24 poolFee; uint24 poolFee;
} }
@ -28,7 +28,7 @@ library SwapHelper {
uint256 amountOut; uint256 amountOut;
uint256 amountInMaximum; uint256 amountInMaximum;
uint256 targetAmountOut; uint256 targetAmountOut;
address targetChainRecipient; bytes32 targetChainRecipient;
uint256 deadline; uint256 deadline;
uint24 poolFee; uint24 poolFee;
} }
@ -75,7 +75,7 @@ library SwapHelper {
index += 32; index += 32;
decoded.estimatedAmount = encodedVm.payload.toUint256(index); decoded.estimatedAmount = encodedVm.payload.toUint256(index);
index += 32; index += 44;
decoded.recipientAddress = encodedVm.payload.toAddress(index); decoded.recipientAddress = encodedVm.payload.toAddress(index);
index += 20; index += 20;

View File

@ -13,7 +13,6 @@ import {
getSignedVAAWithRetry, getSignedVAAWithRetry,
} from "@certusone/wormhole-sdk"; } from "@certusone/wormhole-sdk";
import { grpc } from "@improbable-eng/grpc-web"; import { grpc } from "@improbable-eng/grpc-web";
import { UniEvmToken } from "../route/uniswap-core";
import { import {
PROTOCOL_UNISWAP_V2, PROTOCOL_UNISWAP_V2,
// PROTOCOL_UNISWAP_V3, // PROTOCOL_UNISWAP_V3,
@ -120,11 +119,12 @@ function makeExecutionParameters(chainId: ChainId): ExecutionParameters {
async function evmApproveContractTokenSpend( async function evmApproveContractTokenSpend(
provider: ethers.providers.Provider, provider: ethers.providers.Provider,
signer: ethers.Signer, signer: ethers.Signer,
tokenContract: ethers.Contract, tokenAddress: string, //ethers.Contract,
swapContractAddress: string, swapContractAddress: string,
amount: ethers.BigNumber amount: ethers.BigNumber
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
// build transaction for token spending // build transaction for token spending
const tokenContract = makeEvmToken(provider, tokenAddress).getContract();
const unsignedTx = await tokenContract.populateTransaction.approve( const unsignedTx = await tokenContract.populateTransaction.approve(
swapContractAddress, swapContractAddress,
amount amount
@ -189,7 +189,7 @@ function addressToBytes32(
async function evmApproveAndSwapExactIn( async function evmApproveAndSwapExactIn(
srcProvider: ethers.providers.Provider, srcProvider: ethers.providers.Provider,
srcWallet: ethers.Signer, srcWallet: ethers.Signer,
srcTokenIn: UniEvmToken, tokenInAddress: string,
quoteParams: ExactInCrossParameters, quoteParams: ExactInCrossParameters,
srcExecutionParams: ExecutionParameters, srcExecutionParams: ExecutionParameters,
dstExecutionParams: ExecutionParameters, dstExecutionParams: ExecutionParameters,
@ -254,7 +254,7 @@ async function evmApproveAndSwapExactIn(
await evmApproveContractTokenSpend( await evmApproveContractTokenSpend(
srcProvider, srcProvider,
srcWallet, srcWallet,
srcTokenIn.getContract(), tokenInAddress,
swapContract.address, swapContract.address,
amountIn amountIn
); );
@ -276,7 +276,7 @@ async function evmApproveAndSwapExactIn(
async function evmApproveAndSwapExactOut( async function evmApproveAndSwapExactOut(
srcProvider: ethers.providers.Provider, srcProvider: ethers.providers.Provider,
srcWallet: ethers.Signer, srcWallet: ethers.Signer,
srcTokenIn: UniEvmToken, tokenInAddress: string,
quoteParams: ExactOutCrossParameters, quoteParams: ExactOutCrossParameters,
srcExecutionParams: ExecutionParameters, srcExecutionParams: ExecutionParameters,
dstExecutionParams: ExecutionParameters, dstExecutionParams: ExecutionParameters,
@ -341,7 +341,7 @@ async function evmApproveAndSwapExactOut(
await evmApproveContractTokenSpend( await evmApproveContractTokenSpend(
srcProvider, srcProvider,
srcWallet, srcWallet,
srcTokenIn.getContract(), tokenInAddress,
swapContract.address, swapContract.address,
maxAmountIn maxAmountIn
); );
@ -412,13 +412,6 @@ async function swapExactOutFromVaa(
} }
} }
interface CrossChainSwapTokens {
srcIn: UniEvmToken;
srcOut: UniEvmToken;
dstIn: UniEvmToken;
dstOut: UniEvmToken;
}
interface VaaSearchParams { interface VaaSearchParams {
sequence: string; sequence: string;
emitterAddress: string; emitterAddress: string;
@ -583,13 +576,21 @@ export class UniswapToUniswapExecutor {
return this.quoter.getDstEvmProvider(); return this.quoter.getDstEvmProvider();
} }
getTokenInAddress(): string {
return this.quoter.tokenInAddress;
}
getTokenOutAddress(): string {
return this.quoter.tokenOutAddress;
}
async evmApproveAndSwapExactIn( async evmApproveAndSwapExactIn(
wallet: ethers.Signer wallet: ethers.Signer
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
return evmApproveAndSwapExactIn( return evmApproveAndSwapExactIn(
this.getSrcEvmProvider(), this.getSrcEvmProvider(),
wallet, wallet,
this.tokens.srcIn, this.getTokenInAddress(),
this.cachedExactInParams, this.cachedExactInParams,
this.srcExecutionParams, this.srcExecutionParams,
this.dstExecutionParams, this.dstExecutionParams,
@ -603,7 +604,7 @@ export class UniswapToUniswapExecutor {
return evmApproveAndSwapExactOut( return evmApproveAndSwapExactOut(
this.getSrcEvmProvider(), this.getSrcEvmProvider(),
wallet, wallet,
this.tokens.srcIn, this.getTokenInAddress(),
this.cachedExactOutParams, this.cachedExactOutParams,
this.srcExecutionParams, this.srcExecutionParams,
this.dstExecutionParams, this.dstExecutionParams,