-
Notifications
You must be signed in to change notification settings - Fork 1
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
Connext integration #3
base: main
Are you sure you want to change the base?
Changes from all commits
dad6850
fe6370a
4e377c8
62a6fd7
8939e94
8a8f32d
dd67042
428a3f7
69cf19f
74c9b07
ff1b7e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { ethers } from 'ethers'; | ||
import { PROTOCOL_TOKEN_ADDRESS } from '@common/mocks/tokens'; | ||
import { DestinationCallDataParams } from '@connext/chain-abstraction/dist/types'; | ||
|
||
interface Permissions { | ||
operator: string; | ||
permissions: number[]; | ||
} | ||
|
||
export const getSwapAndXcallParams = ( | ||
originDomain: string, | ||
destinationDomain: string, | ||
fromAsset: string, | ||
toAsset: string, | ||
amountIn: string, | ||
to: string, // mean target address. | ||
callData: string, | ||
relayerFeeInNativeAsset: string | ||
) => { | ||
const fromAssetOrigin = fromAsset === PROTOCOL_TOKEN_ADDRESS ? ethers.constants.AddressZero : fromAsset; | ||
const swapAndXCallParams = { | ||
originDomain, | ||
destinationDomain, | ||
fromAsset: fromAssetOrigin, // BNB | ||
toAsset, // USDC | ||
amountIn: amountIn.toString(), | ||
to, | ||
relayerFeeInNativeAsset, // 0.001 BNB | ||
callData, | ||
}; | ||
return swapAndXCallParams; | ||
}; | ||
|
||
export const getForwardFunctionCallHelper = ( | ||
from: string, | ||
to: string, | ||
amountOfSwaps: ethers.BigNumber, | ||
swapInterval: ethers.BigNumber, | ||
owner: string, | ||
permissions: Permissions[] | ||
) => { | ||
const { defaultAbiCoder } = ethers.utils; | ||
const encodedData = defaultAbiCoder.encode( | ||
['address', 'address', 'uint256', 'uint32', 'address', 'tuple(address operator,uint256[] permissions)[]'], | ||
[from, to, amountOfSwaps, swapInterval, owner, permissions] | ||
); | ||
return encodedData; | ||
}; | ||
|
||
export const getDestinationCallDataParams = (signerAddress: string, token: string, poolFee: string) => { | ||
const params: DestinationCallDataParams = { | ||
fallback: signerAddress, | ||
swapForwarderData: { | ||
toAsset: token, | ||
swapData: { | ||
amountOutMin: '0', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be 0 or should the value change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine |
||
poolFee, | ||
}, | ||
}, | ||
}; | ||
return params; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { getPoolFeeForUniV3, getXCallCallData, prepareSwapAndXCall } from '@connext/chain-abstraction'; | ||
import { DestinationCallDataParams, Swapper, SwapAndXCallParams } from '@connext/chain-abstraction/dist/types'; | ||
import { SdkConfig, create } from '@connext/sdk'; | ||
import { NETWORKS, SUPPORTED_CHAINS_BY_CONNEXT } from '@constants'; | ||
import { find } from 'lodash'; | ||
import WalletService from './walletService'; | ||
|
||
interface DomainID { | ||
[key: number]: string; | ||
} | ||
|
||
export default class ConnextService { | ||
walletService: WalletService; | ||
|
||
sdkConfig: SdkConfig; | ||
|
||
constructor(walletService: WalletService) { | ||
this.walletService = walletService; | ||
} | ||
|
||
sdkInit() { | ||
const domainConfig: { [domainId: string]: { providers: string[] } } = {}; | ||
|
||
const domainChainIds = Object.entries(SUPPORTED_CHAINS_BY_CONNEXT) | ||
.filter(([key]) => typeof key === 'number') | ||
.map(([key, value]) => ({ domainId: value.domainId, chainId: key })); | ||
|
||
domainChainIds.forEach((obj) => { | ||
domainConfig[obj.domainId] = { providers: [this.getRPCURL(parseInt(obj.chainId, 10))] }; | ||
}); | ||
|
||
const sdkConfig: SdkConfig = { | ||
signerAddress: this.walletService.getAccount(), | ||
network: 'mainnet', // can change it to testnet as well | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this parameter for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For initing the connext core SDK There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right but I mean, what does it imply if it is "mainnet"? or "testnet"? shouldn't it work the same regardless of what type of network it is? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So for initialising the SDK we have to define if this is for
Comment on lines
+22
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I woudl move this all into its own function, since walletService.getAccount() will return the current account and at the moment of the constructor the wallet is not yet connected, so its always going to be empty. The idea would be that on the |
||
chains: domainConfig, | ||
}; | ||
this.sdkConfig = sdkConfig; | ||
return sdkConfig; | ||
} | ||
|
||
getRPCURL(chainID: number) { | ||
const network = find(NETWORKS, { chainId: chainID }); | ||
if (network) { | ||
return network.rpc[0]; | ||
} | ||
throw Error('Network not supported'); | ||
} | ||
|
||
async getCalculatedRelayerFees(originDomain: string, destinationDomain: string) { | ||
const { sdkBase } = await create(this.sdkConfig); | ||
const relayerFees = await sdkBase.estimateRelayerFee({ | ||
originDomain, | ||
destinationDomain, | ||
isHighPriority: true, | ||
}); | ||
return relayerFees.toString(); | ||
} | ||
|
||
async getPoolFeeForUniV3Helper(domainID: string, token0: string, token1: string, rpcURL: string) { | ||
try { | ||
const poolFee = await getPoolFeeForUniV3(domainID, rpcURL, token0, token1); | ||
return poolFee; | ||
} catch (err) { | ||
throw Error('Failed to fetch Pool Fees'); | ||
} | ||
} | ||
|
||
async getXCallCallDataHelper(domainID: string, forwardCallData: string, params: DestinationCallDataParams) { | ||
const swapper = Swapper.UniV3; | ||
return getXCallCallData(domainID, swapper, forwardCallData, params); | ||
} | ||
|
||
async prepareSwapAndXCallHelper(swapAndXCallParams: SwapAndXCallParams, signerAddress: string) { | ||
return prepareSwapAndXCall(swapAndXCallParams, signerAddress); | ||
} | ||
|
||
async getEstimateAmountReceived( | ||
originDomain: string, | ||
destinationDomain: string, | ||
originToken: string, | ||
amount: number | ||
) { | ||
const { sdkBase } = await create(this.sdkConfig); | ||
const estimateReceived = await sdkBase.calculateAmountReceived( | ||
originDomain, | ||
destinationDomain, | ||
originToken, | ||
amount | ||
); | ||
return estimateReceived; | ||
} | ||
|
||
getNativeUSDCAddress(networkName: number) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will connext only work with USDC? |
||
const USDC_ADDRESS: DomainID = { | ||
1: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', | ||
137: '0x2791bca1f2de4661ed88a30c99a7a9449aa84174', | ||
10: '0x7F5c764cBc14f9669B88837ca1490cCa17c31607', | ||
42161: '0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8', | ||
100: '0xDDAfbb505ad214D7b80b1f830fcCc89B60fb7A83', | ||
56: '0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d', | ||
}; | ||
return USDC_ADDRESS[networkName]; | ||
} | ||
|
||
async getTransferStatus(transactionHash: string) { | ||
try { | ||
const { sdkUtils } = await create(this.sdkConfig); | ||
const params: { transactionHash: string } = { | ||
transactionHash, | ||
}; | ||
const transferStatus = await sdkUtils.getTransfers(params); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how can it know in which chain is it bridging if it only has the transactionHash? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So it is the internal function we use for connextscan, it can fetch the whole transaction from origin transactionHash itself, it will give you chains and other details as well |
||
if (!transferStatus) { | ||
throw Error('Failed to fetch transfer status'); | ||
} | ||
return transferStatus; | ||
} catch (err) { | ||
throw Error(err); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are all of these new dependencies needed?