Last active
September 4, 2021 16:18
-
-
Save ezynda3/f1f2e5789d3b0798a992f6fd3cb36e4b to your computer and use it in GitHub Desktop.
Connext Test
This file contains hidden or 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
import 'dotenv/config' | |
import { node_url } from '../utils/network' | |
import { providers, Wallet, utils } from 'ethers' | |
import { NxtpSdk, NxtpSdkEvents } from '@connext/nxtp-sdk' | |
import { NXTPFacet__factory, ERC20__factory } from '../typechain' | |
const tidy = (str: string): string => | |
`${str.replace(/\n/g, '').replace(/ +/g, ' ')}` | |
async function main() { | |
// Set up provider to chain mappings | |
const chainConfig = { | |
4: { | |
provider: new providers.FallbackProvider([ | |
new providers.JsonRpcProvider(node_url('rinkeby')), | |
]), | |
}, | |
5: { | |
provider: new providers.FallbackProvider([ | |
new providers.JsonRpcProvider(node_url('goerli')), | |
]), | |
}, | |
} | |
// Get signer | |
let wallet = Wallet.fromMnemonic(<string>process.env.MNEMONIC) | |
const provider = new providers.JsonRpcProvider(node_url('rinkeby')) | |
wallet = wallet.connect(provider) | |
// Instantiate SDK | |
const sdk = new NxtpSdk(chainConfig, wallet) | |
const quote = await sdk.getTransferQuote({ | |
sendingAssetId: '0x9aC2c46d7AcC21c881154D57c0Dc1c55a3139198', | |
sendingChainId: 4, | |
receivingAssetId: '0x8a1Cad3703E0beAe0e0237369B4fcD04228d1682', | |
receivingChainId: 5, | |
receivingAddress: await wallet.getAddress(), | |
amount: utils.parseEther('1').toString(), | |
}) | |
const AuctionBidEncoding = tidy(`tuple( | |
address user, | |
address router, | |
uint24 sendingChainId, | |
address sendingAssetId, | |
uint256 amount, | |
uint24 receivingChainId, | |
address receivingAssetId, | |
uint256 amountReceived, | |
address receivingAddress, | |
bytes32 transactionId, | |
uint256 expiry, | |
bytes32 callDataHash, | |
address callTo, | |
bytes encryptedCallData, | |
address sendingChainTxManagerAddress, | |
address receivingChainTxManagerAddress, | |
uint256 bidExpiry | |
)`) | |
const nxtpData = { | |
...quote.bid, | |
sendingChainFallback: await wallet.getAddress(), | |
encodedBid: utils.defaultAbiCoder.encode([AuctionBidEncoding], [quote.bid]), | |
bidSignature: quote.bidSignature || '', | |
amount: utils.parseEther('1'), | |
expiry: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 3, | |
} | |
// console.log(nxtpData) | |
const token = ERC20__factory.connect( | |
'0x9aC2c46d7AcC21c881154D57c0Dc1c55a3139198', | |
wallet | |
) | |
const lifi = NXTPFacet__factory.connect( | |
'0x1d92aC0Ae6e33bAc0C7e8De5B22fD3cCeaD2b2bE', | |
wallet | |
) | |
await token.approve(lifi.address, utils.parseEther('1')) | |
const tx = await lifi.initBridgeTokensViaNXTP(nxtpData, { gasLimit: 500000 }) | |
const receipt = await tx.wait() | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |
let transfer: any | |
for (const e of receipt?.events || []) { | |
if (e.eventSignature?.includes('NXTPTransactionInitiated')) { | |
transfer = lifi.interface.parseLog(e) | |
} | |
} | |
console.log(transfer) | |
// wait for receiver prepared event | |
const prepared = await sdk.waitFor( | |
NxtpSdkEvents.ReceiverTransactionPrepared, | |
100_000, | |
(data) => data.txData.transactionId === transfer?.transactionId // filter function | |
) | |
console.log('Prepared!') | |
console.log(prepared) | |
} | |
main() | |
.then(() => process.exit(0)) | |
.catch((error) => { | |
console.error(error) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment