167 lines
4.0 KiB
TypeScript
167 lines
4.0 KiB
TypeScript
import { tEthereumAddress, tStringTokenSmallUnits } from '@aave/deploy-v3';
|
|
import { BigNumberish, ethers } from 'ethers';
|
|
import { signTypedData_v4 } from 'eth-sig-util';
|
|
import { fromRpcSig, ECDSASignature } from 'ethereumjs-util';
|
|
|
|
export const buildParaSwapLiquiditySwapParams = (
|
|
assetToSwapTo: tEthereumAddress,
|
|
minAmountToReceive: BigNumberish,
|
|
swapAllBalanceOffset: BigNumberish,
|
|
swapCalldata: string | Buffer,
|
|
augustus: tEthereumAddress,
|
|
permitAmount: BigNumberish,
|
|
deadline: BigNumberish,
|
|
v: BigNumberish,
|
|
r: string | Buffer,
|
|
s: string | Buffer
|
|
) => {
|
|
return ethers.utils.defaultAbiCoder.encode(
|
|
[
|
|
'address',
|
|
'uint256',
|
|
'uint256',
|
|
'bytes',
|
|
'address',
|
|
'tuple(uint256,uint256,uint8,bytes32,bytes32)',
|
|
],
|
|
[
|
|
assetToSwapTo,
|
|
minAmountToReceive,
|
|
swapAllBalanceOffset,
|
|
swapCalldata,
|
|
augustus,
|
|
[permitAmount, deadline, v, r, s],
|
|
]
|
|
);
|
|
};
|
|
|
|
export const buildPermitParams = (
|
|
chainId: number,
|
|
token: tEthereumAddress,
|
|
revision: string,
|
|
tokenName: string,
|
|
owner: tEthereumAddress,
|
|
spender: tEthereumAddress,
|
|
nonce: number,
|
|
deadline: string,
|
|
value: tStringTokenSmallUnits
|
|
) => ({
|
|
types: {
|
|
EIP712Domain: [
|
|
{ name: 'name', type: 'string' },
|
|
{ name: 'version', type: 'string' },
|
|
{ name: 'chainId', type: 'uint256' },
|
|
{ name: 'verifyingContract', type: 'address' },
|
|
],
|
|
Permit: [
|
|
{ name: 'owner', type: 'address' },
|
|
{ name: 'spender', type: 'address' },
|
|
{ name: 'value', type: 'uint256' },
|
|
{ name: 'nonce', type: 'uint256' },
|
|
{ name: 'deadline', type: 'uint256' },
|
|
],
|
|
},
|
|
primaryType: 'Permit' as const,
|
|
domain: {
|
|
name: tokenName,
|
|
version: revision,
|
|
chainId: chainId,
|
|
verifyingContract: token,
|
|
},
|
|
message: {
|
|
owner,
|
|
spender,
|
|
value,
|
|
nonce,
|
|
deadline,
|
|
},
|
|
});
|
|
|
|
export const getSignatureFromTypedData = (
|
|
privateKey: string,
|
|
typedData: any // TODO: should be TypedData, from eth-sig-utils, but TS doesn't accept it
|
|
): ECDSASignature => {
|
|
const signature = signTypedData_v4(Buffer.from(privateKey.substring(2, 66), 'hex'), {
|
|
data: typedData,
|
|
});
|
|
return fromRpcSig(signature);
|
|
};
|
|
|
|
export const buildLiquiditySwapParams = (
|
|
assetToSwapToList: tEthereumAddress[],
|
|
minAmountsToReceive: BigNumberish[],
|
|
swapAllBalances: BigNumberish[],
|
|
permitAmounts: BigNumberish[],
|
|
deadlines: BigNumberish[],
|
|
v: BigNumberish[],
|
|
r: (string | Buffer)[],
|
|
s: (string | Buffer)[],
|
|
useEthPath: boolean[]
|
|
) => {
|
|
return ethers.utils.defaultAbiCoder.encode(
|
|
[
|
|
'address[]',
|
|
'uint256[]',
|
|
'bool[]',
|
|
'uint256[]',
|
|
'uint256[]',
|
|
'uint8[]',
|
|
'bytes32[]',
|
|
'bytes32[]',
|
|
'bool[]',
|
|
],
|
|
[
|
|
assetToSwapToList,
|
|
minAmountsToReceive,
|
|
swapAllBalances,
|
|
permitAmounts,
|
|
deadlines,
|
|
v,
|
|
r,
|
|
s,
|
|
useEthPath,
|
|
]
|
|
);
|
|
};
|
|
|
|
export const buildParaswapBuyParams = (
|
|
buyCalldata: string | Buffer,
|
|
augustus: tEthereumAddress
|
|
) => {
|
|
return ethers.utils.defaultAbiCoder.encode(['bytes', 'address'], [buyCalldata, augustus]);
|
|
};
|
|
|
|
export const buildParaSwapRepayParams = (
|
|
collateralAsset: tEthereumAddress,
|
|
collateralAmount: BigNumberish,
|
|
buyAllBalanceOffset: BigNumberish,
|
|
debtRateMode: BigNumberish,
|
|
buyCalldata: string | Buffer,
|
|
augustus: tEthereumAddress,
|
|
permitAmount: BigNumberish,
|
|
deadline: BigNumberish,
|
|
v: BigNumberish,
|
|
r: string | Buffer,
|
|
s: string | Buffer
|
|
) => {
|
|
const paraswapData = buildParaswapBuyParams(buyCalldata, augustus);
|
|
return ethers.utils.defaultAbiCoder.encode(
|
|
[
|
|
'address',
|
|
'uint256',
|
|
'uint256',
|
|
'uint256',
|
|
'bytes',
|
|
'tuple(uint256,uint256,uint8,bytes32,bytes32)',
|
|
],
|
|
[
|
|
collateralAsset,
|
|
collateralAmount,
|
|
buyAllBalanceOffset,
|
|
debtRateMode,
|
|
paraswapData,
|
|
[permitAmount, deadline, v, r, s],
|
|
]
|
|
);
|
|
};
|