Update contracts; fix evm approve and swap
This commit is contained in:
parent
5629f2fae8
commit
3154c74c73
|
@ -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,8 +386,16 @@ contract CrossChainSwapV2 {
|
||||||
swapParams.deadline
|
swapParams.deadline
|
||||||
);
|
);
|
||||||
|
|
||||||
// encode payload for second swap
|
// create payload variable
|
||||||
bytes memory payload = abi.encodePacked(
|
bytes memory payload;
|
||||||
|
|
||||||
|
// UST is native to Terra - no need for swap instructions
|
||||||
|
if (targetChainId == terraChainId) {
|
||||||
|
payload = abi.encodePacked(
|
||||||
|
swapParams.targetChainRecipient
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
payload = abi.encodePacked(
|
||||||
swapParams.targetAmountOutMinimum,
|
swapParams.targetAmountOutMinimum,
|
||||||
swapParams.targetChainRecipient,
|
swapParams.targetChainRecipient,
|
||||||
path[2],
|
path[2],
|
||||||
|
@ -396,6 +405,7 @@ contract CrossChainSwapV2 {
|
||||||
typeExactIn,
|
typeExactIn,
|
||||||
typeNativeSwap
|
typeNativeSwap
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// approve token bridge to spend feeTokens (UST)
|
// approve token bridge to spend feeTokens (UST)
|
||||||
TransferHelper.safeApprove(
|
TransferHelper.safeApprove(
|
||||||
|
@ -761,8 +771,16 @@ contract CrossChainSwapV2 {
|
||||||
typeNativeSwap
|
typeNativeSwap
|
||||||
);
|
);
|
||||||
|
|
||||||
// encode payload for second swap
|
// create payload variable
|
||||||
bytes memory payload = abi.encodePacked(
|
bytes memory payload;
|
||||||
|
|
||||||
|
// UST is native to Terra - no need for swap instructions
|
||||||
|
if (targetChainId == terraChainId) {
|
||||||
|
payload = abi.encodePacked(
|
||||||
|
swapParams.targetChainRecipient
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
payload = abi.encodePacked(
|
||||||
swapParams.targetAmountOut,
|
swapParams.targetAmountOut,
|
||||||
swapParams.targetChainRecipient,
|
swapParams.targetChainRecipient,
|
||||||
path[2],
|
path[2],
|
||||||
|
@ -772,6 +790,7 @@ contract CrossChainSwapV2 {
|
||||||
typeExactOut,
|
typeExactOut,
|
||||||
typeNativeSwap
|
typeNativeSwap
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// approve token bridge to spend feeTokens (UST)
|
// approve token bridge to spend feeTokens (UST)
|
||||||
TransferHelper.safeApprove(
|
TransferHelper.safeApprove(
|
||||||
|
|
|
@ -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,8 +404,16 @@ contract CrossChainSwapV3 {
|
||||||
typeNativeSwap
|
typeNativeSwap
|
||||||
);
|
);
|
||||||
|
|
||||||
// encode payload for second swap
|
// create payload variable
|
||||||
bytes memory payload = abi.encodePacked(
|
bytes memory payload;
|
||||||
|
|
||||||
|
// UST is native to Terra - no need for swap instructions
|
||||||
|
if (targetChainId == terraChainId) {
|
||||||
|
payload = abi.encodePacked(
|
||||||
|
swapParams.targetChainRecipient
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
payload = abi.encodePacked(
|
||||||
swapParams.targetAmountOutMinimum,
|
swapParams.targetAmountOutMinimum,
|
||||||
swapParams.targetChainRecipient,
|
swapParams.targetChainRecipient,
|
||||||
path[2],
|
path[2],
|
||||||
|
@ -414,6 +423,7 @@ contract CrossChainSwapV3 {
|
||||||
typeExactIn,
|
typeExactIn,
|
||||||
typeNativeSwap
|
typeNativeSwap
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// approve token bridge to spend feeTokens (UST)
|
// approve token bridge to spend feeTokens (UST)
|
||||||
TransferHelper.safeApprove(
|
TransferHelper.safeApprove(
|
||||||
|
@ -788,8 +798,16 @@ contract CrossChainSwapV3 {
|
||||||
typeNativeSwap
|
typeNativeSwap
|
||||||
);
|
);
|
||||||
|
|
||||||
// encode payload for second swap
|
// create payload variable
|
||||||
bytes memory payload = abi.encodePacked(
|
bytes memory payload;
|
||||||
|
|
||||||
|
// UST is native to Terra - no need for swap instructions
|
||||||
|
if (targetChainId == terraChainId) {
|
||||||
|
payload = abi.encodePacked(
|
||||||
|
swapParams.targetChainRecipient
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
payload = abi.encodePacked(
|
||||||
swapParams.targetAmountOut,
|
swapParams.targetAmountOut,
|
||||||
swapParams.targetChainRecipient,
|
swapParams.targetChainRecipient,
|
||||||
path[2],
|
path[2],
|
||||||
|
@ -799,6 +817,7 @@ contract CrossChainSwapV3 {
|
||||||
typeExactOut,
|
typeExactOut,
|
||||||
typeNativeSwap
|
typeNativeSwap
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// approve token bridge to spend feeTokens (UST)
|
// approve token bridge to spend feeTokens (UST)
|
||||||
TransferHelper.safeApprove(
|
TransferHelper.safeApprove(
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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,
|
||||||
|
|
Loading…
Reference in New Issue