From 64eadfbc2e446f9d299b1164222ee02ccb1506df Mon Sep 17 00:00:00 2001 From: welliton gervickas Date: Thu, 30 Nov 2023 15:51:05 -0300 Subject: [PATCH] feat: new test --- config/chains.ts | 5 +- contracts-left.md | 2 + tasks/deploy-test-nft-contract.ts | 24 ++++--- tasks/index.ts | 1 + tasks/test-nft-contracts.ts | 113 ++++++++++++++++++++++++++++++ 5 files changed, 131 insertions(+), 14 deletions(-) create mode 100644 tasks/test-nft-contracts.ts diff --git a/config/chains.ts b/config/chains.ts index 462c0c3..4fc3df0 100644 --- a/config/chains.ts +++ b/config/chains.ts @@ -44,7 +44,7 @@ export const polygonMumbai: Chain = { nativeCurrency: { name: 'MATIC', symbol: 'MATIC', decimals: 18 }, rpcUrls: { protocol: { - http: [process.env.PUBLIC_NETWORK_80001_HTTP_RPC!] + http: ['https://rpc.ankr.com/polygon_mumbai'] }, infura: { http: ['https://polygon-mumbai.infura.io/v3'] @@ -76,8 +76,7 @@ export const polygonMumbai: Chain = { blockCreated: 0 } }, - testnet: true, - gasPrice: 210000 + testnet: true } export const optimismGoerli: Chain = { diff --git a/contracts-left.md b/contracts-left.md index 3d6fe69..cf046fd 100644 --- a/contracts-left.md +++ b/contracts-left.md @@ -24,3 +24,5 @@ pnpm hardhat setup-bridge-adapter --network 80001 --bridge-address 0x96D103BCb67 ========================= pnpm hardhat deploy-test-nft-contract --network 80001 --token-name "hello" --token-symbol "world" --bridge-address 0x96D103BCb675945DE9C51D9dCa57a14593a54558 --adapter-address 0xBd770416a3345F91E4B34576cb804a576fa48EB1 --target-network 43113 + +pnpm hardhat deploy-test-nft-contract --network 80001 --token-name "hello" --token-symbol "world" --bridge-address 0x96D103BCb675945DE9C51D9dCa57a14593a54558 --adapter-address 0xBd770416a3345F91E4B34576cb804a576fa48EB1 --target-network 43113 --nft-address 0x7634f8DF9d00C1A445062D908aB129BE65DF7AB9 --token-id 1 diff --git a/tasks/deploy-test-nft-contract.ts b/tasks/deploy-test-nft-contract.ts index b124656..062455b 100644 --- a/tasks/deploy-test-nft-contract.ts +++ b/tasks/deploy-test-nft-contract.ts @@ -39,23 +39,25 @@ task('deploy-test-nft-contract', 'deploy nft contract') `ℹ️ Deploying new NFT ${tokenName} with symbol ${tokenSymbol} to ${chainConfig.id}` ) - const tokenId = 1 + try { + const tokenId = 1 - const nft = await hre.ethers.deployContract('MockNFT', [ - tokenName, - tokenSymbol - ]) + const nft = await hre.ethers.deployContract('MockNFT', [ + tokenName, + tokenSymbol + ]) - const [deployer] = await hre.ethers.getSigners() + const [deployer] = await hre.ethers.getSigners() - await nft.waitForDeployment() - const tx = await nft.mint(tokenId) + await nft.waitForDeployment() + const tx = await nft.mint(tokenId) - await tx.wait() + await tx.wait() - const nftAddress = await nft.getAddress() + const nftAddress = await nft.getAddress() + + console.log('ℹ️ NFT deployed', nftAddress) - try { const bridge = await hre.ethers.getContractAt('Bridge', bridgeAddress) const adapter = await hre.ethers.getContractAt( diff --git a/tasks/index.ts b/tasks/index.ts index f8ff22a..8492a82 100644 --- a/tasks/index.ts +++ b/tasks/index.ts @@ -3,3 +3,4 @@ import './deploy-adapter-contract' import './set-chain-settings' import './setup-bridge-adapter' import './deploy-test-nft-contract' +import './test-nft-contracts' diff --git a/tasks/test-nft-contracts.ts b/tasks/test-nft-contracts.ts new file mode 100644 index 0000000..e544139 --- /dev/null +++ b/tasks/test-nft-contracts.ts @@ -0,0 +1,113 @@ +import { task } from 'hardhat/config' +import { Spinner } from '../scripts/spinner' +import cliSpinner from 'cli-spinners' +import { allowedChainsConfig } from '@/config/config' +import { RampType } from './set-chain-settings' + +const spinner: Spinner = new Spinner(cliSpinner.triangle) + +export type DeployTestNFTContractTask = { + tokenName: string + tokenSymbol: string + bridgeAddress: string + adapterAddress: string + targetNetwork: number + nftAddress: string + tokenId: number +} + +task('test-nft-contract', 'deploy nft contract') + .addParam('tokenName', 'token name') + .addParam('tokenSymbol', 'token symbol') + .addParam('bridgeAddress', 'bridge address') + .addParam('adapterAddress', 'adapter address') + .addParam('targetNetwork', 'target network') + .addParam('nftAddress', 'nft address') + .addParam('tokenId', 'token id') + .setAction( + async ( + { + nftAddress, + tokenName, + tokenSymbol, + bridgeAddress, + targetNetwork, + adapterAddress, + tokenId + }: DeployTestNFTContractTask, + hre + ) => { + spinner.start() + const chainConfig = allowedChainsConfig[+hre.network.name] + if (!chainConfig) throw new Error('Chain config not found') + + console.log( + `ℹ️ Deploying new NFT ${tokenName} with symbol ${tokenSymbol} to ${chainConfig.id}` + ) + + try { + const nft = await hre.ethers.getContractAt('MockNFT', nftAddress) + const [deployer] = await hre.ethers.getSigners() + const bridge = await hre.ethers.getContractAt('Bridge', bridgeAddress) + + const adapter = await hre.ethers.getContractAt( + 'CCIPAdapter', + adapterAddress + ) + + const abiCoder = hre.ethers.AbiCoder.defaultAbiCoder() + + const targetChainSettings = await bridge.getChainSettings( + targetNetwork, + RampType.OnRamp + ) + + const payload = { + toChain: targetChainSettings.nonEvmChainId, + receiver: targetChainSettings.adapter, + gasLimit: targetChainSettings.gasLimit, + data: abiCoder.encode( + ['address', 'bytes', 'bytes'], + [ + deployer.address, + abiCoder.encode( + ['uint256', 'address', 'uint256'], + [chainConfig.id, nftAddress, tokenId] + ), + abiCoder.encode( + ['string', 'string', 'string'], + [tokenName, tokenSymbol, await nft.tokenURI(tokenId)] + ) + ] + ) + } + + console.log('ℹ️ Getting required fee') + const fee = await adapter.getFee(payload) + console.log('ℹ️ Feee', fee) + + console.log('ℹ️ Approving') + const tx2 = await nft.approve(bridgeAddress, tokenId) + await tx2.wait() + console.log('ℹ️ Approved') + + await bridge.sendERC721( + targetChainSettings.evmChainId, + nftAddress, + tokenId, + { + value: fee + 2560000n + } + ) + + const nftOwner = await nft.ownerOf(tokenId) + + spinner.stop() + console.log(`✅ NFT deployed and transfered to ${nftOwner}`) + } catch (error) { + spinner.stop() + console.log(`❌ NFT deploy failed`) + console.log(error) + } + } + )