forked from paraswap/paraswap-dex-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request paraswap#657 from paraswap/feat/BACK-1528-etherfi
Feat/back 1528 etherfi
- Loading branch information
Showing
5 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[ | ||
{ | ||
"inputs": [], | ||
"name": "deposit", | ||
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], | ||
"stateMutability": "payable", | ||
"type": "function" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[ | ||
{ | ||
"inputs": [ | ||
{ "internalType": "uint256", "name": "_eETHAmount", "type": "uint256" } | ||
], | ||
"name": "wrap", | ||
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ "internalType": "uint256", "name": "_weETHAmount", "type": "uint256" } | ||
], | ||
"name": "unwrap", | ||
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import { Interface, JsonFragment } from '@ethersproject/abi'; | ||
import { ETHER_ADDRESS, NULL_ADDRESS, SwapSide } from '../constants'; | ||
import { AdapterExchangeParam, Address, SimpleExchangeParam } from '../types'; | ||
import { IDexTxBuilder } from './idex'; | ||
import { SimpleExchange } from './simple-exchange'; | ||
import eETHPoolABI from '../abi/etherfi/eETHPool.json'; | ||
import weETHABI from '../abi/etherfi/weETH.json'; | ||
import { IDexHelper } from '../dex-helper'; | ||
import { isETHAddress } from '../utils'; | ||
import { assert } from 'ts-essentials'; | ||
import { WethFunctions } from './weth/types'; | ||
|
||
type EtherFiData = void; | ||
|
||
type eETHPoolDepositParam = []; | ||
type weETHWrap = [_eETHAmount: string]; | ||
type weETHUnwrap = [_weETHAmount: string]; | ||
|
||
type EtherFiParam = eETHPoolDepositParam | weETHWrap | weETHUnwrap; | ||
|
||
enum EtherFiFunctions { | ||
deposit = 'deposit', | ||
wrap = 'wrap', | ||
unwrap = 'unwrap', | ||
} | ||
|
||
const EtherFiConfig: Record< | ||
number, | ||
{ eETH: string; eETHPool: string; weETH: string } | ||
> = { | ||
1: { | ||
eETH: '0x35fa164735182de50811e8e2e824cfb9b6118ac2', | ||
eETHPool: '0x308861a430be4cce5502d0a12724771fc6daf216', | ||
weETH: '0xcd5fe23c85820f7b72d0926fc9b05b43e359b7ee', | ||
}, | ||
}; | ||
|
||
export class EtherFi | ||
extends SimpleExchange | ||
implements IDexTxBuilder<EtherFiData, EtherFiParam> | ||
{ | ||
static dexKeys = ['etherfi']; | ||
|
||
eETHPoolInterface: Interface; | ||
weETHInterface: Interface; | ||
needWrapNative = false; | ||
|
||
eETH: string; | ||
eETHPool: string; | ||
weETH: string; | ||
|
||
constructor(dexHelper: IDexHelper) { | ||
super(dexHelper, 'etherfi'); | ||
|
||
this.eETHPoolInterface = new Interface(eETHPoolABI as JsonFragment[]); | ||
this.weETHInterface = new Interface(weETHABI as JsonFragment[]); | ||
|
||
this.eETH = EtherFiConfig[this.network].eETH.toLowerCase(); | ||
this.eETHPool = EtherFiConfig[this.network].eETHPool.toLowerCase(); | ||
this.weETH = EtherFiConfig[this.network].weETH.toLowerCase(); | ||
} | ||
|
||
is_eETH = (token: string) => token.toLowerCase() === this.eETH; | ||
is_weETH = (token: string) => token.toLowerCase() === this.weETH; | ||
|
||
async getSimpleParam( | ||
srcToken: string, | ||
destToken: string, | ||
srcAmount: string, | ||
destAmount: string, | ||
data: EtherFiData, | ||
side: SwapSide, | ||
): Promise<SimpleExchangeParam> { | ||
const [Interface, swapCallee, swapFunction, swapFunctionParams] = ((): [ | ||
Interface, | ||
Address, | ||
EtherFiFunctions, | ||
EtherFiParam, | ||
] => { | ||
if (this.is_weETH(destToken)) { | ||
assert(this.is_eETH(srcToken), 'srcToken should be eETH'); | ||
return [ | ||
this.weETHInterface, | ||
this.weETH, | ||
EtherFiFunctions.wrap, | ||
[srcAmount], | ||
]; | ||
} | ||
|
||
if (this.is_weETH(srcToken)) { | ||
assert(this.is_eETH(destToken), 'destToken should be eETH'); | ||
return [ | ||
this.weETHInterface, | ||
this.weETH, | ||
EtherFiFunctions.unwrap, | ||
[srcAmount], | ||
]; | ||
} | ||
|
||
if (this.is_eETH(destToken)) { | ||
assert( | ||
this.isWETH(srcToken) || isETHAddress(srcToken), | ||
'srcToken should be (w)eth', | ||
); | ||
return [ | ||
this.eETHPoolInterface, | ||
this.eETHPool, | ||
EtherFiFunctions.deposit, | ||
[], | ||
]; | ||
} | ||
|
||
throw new Error('LOGIC ERROR'); | ||
})(); | ||
|
||
const swapData = Interface.encodeFunctionData( | ||
swapFunction, | ||
swapFunctionParams, | ||
); | ||
|
||
// if src token is WETH, we need to withdraw from weth and pass eth value on call. For other cases, we need to approve and perform call | ||
const isSrcTokenWeth = this.isWETH(srcToken); | ||
|
||
return this.buildSimpleParamWithoutWETHConversion( | ||
isSrcTokenWeth ? ETHER_ADDRESS : srcToken, | ||
srcAmount, | ||
destToken, | ||
destAmount, | ||
swapData, | ||
swapCallee, | ||
swapCallee, | ||
'0', | ||
isSrcTokenWeth | ||
? { | ||
callees: [this.dexHelper.config.data.wrappedNativeTokenAddress], | ||
calldata: [ | ||
this.erc20Interface.encodeFunctionData(WethFunctions.withdraw, [ | ||
srcAmount, | ||
]), | ||
], | ||
values: ['0'], | ||
} | ||
: undefined, | ||
); | ||
} | ||
|
||
getAdapterParam( | ||
srcToken: string, | ||
destToken: string, | ||
srcAmount: string, | ||
destAmount: string, | ||
data: EtherFiData, | ||
side: SwapSide, | ||
): AdapterExchangeParam { | ||
return { | ||
targetExchange: NULL_ADDRESS, | ||
payload: '0x', | ||
networkFee: '0', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters