Pnuttaste/Interchain-message/test/RubicFallback.spec.ts

369 lines
14 KiB
TypeScript
Raw Permalink Normal View History

new file: Interchain-message/.env.example new file: Interchain-message/.eslintrc.js new file: Interchain-message/.gitignore new file: Interchain-message/.prettierrc new file: Interchain-message/.solhint.json new file: Interchain-message/.solhintignore new file: Interchain-message/contracts/interfaces/IBridge.sol new file: Interchain-message/contracts/interfaces/IMessageBus.sol new file: Interchain-message/contracts/interfaces/IMessageReceiverApp.sol new file: Interchain-message/contracts/interfaces/IUniswapRouterV3.sol new file: Interchain-message/contracts/interfaces/IWETH.sol new file: Interchain-message/contracts/message/apps/BridgeSwap.sol new file: Interchain-message/contracts/message/apps/RubicRouterV2.sol new file: Interchain-message/contracts/message/apps/RubicRouterV2ETH.sol new file: Interchain-message/contracts/message/apps/SwapBase.sol new file: Interchain-message/contracts/message/apps/TransferSwapInch.sol new file: Interchain-message/contracts/message/apps/TransferSwapV2.sol new file: Interchain-message/contracts/message/apps/TransferSwapV3.sol new file: Interchain-message/contracts/message/framework/MessageReceiverApp.sol new file: Interchain-message/contracts/message/framework/MessageSenderApp.sol new file: Interchain-message/contracts/message/libraries/MessageSenderLib.sol new file: Interchain-message/contracts/message/libraries/MsgDataTypes.sol new file: Interchain-message/contracts/test/MessageBusSender.sol new file: Interchain-message/contracts/test/TestERC20.sol new file: Interchain-message/contracts/test/TestMessages.sol new file: Interchain-message/contracts/test/WETH9.sol new file: Interchain-message/deployments/Readme.md new file: Interchain-message/executor/config/cbridge.toml new file: Interchain-message/executor/config/executor.toml new file: Interchain-message/executor/eth-ks/signer.json new file: Interchain-message/hardhat.config.ts new file: Interchain-message/package-lock.json new file: Interchain-message/package.json new file: Interchain-message/reports/contract_sizes.txt new file: Interchain-message/reports/gas_usage/summary.txt new file: Interchain-message/scripts/deploy/deploy.js new file: Interchain-message/scripts/deploy/deployAVAX.ts new file: Interchain-message/scripts/deploy/deployArbitrum.ts new file: Interchain-message/scripts/deploy/deployAurora.ts new file: Interchain-message/scripts/deploy/deployBSC.ts new file: Interchain-message/scripts/deploy/deployEth.ts new file: Interchain-message/scripts/deploy/deployFantom.ts new file: Interchain-message/scripts/deploy/deployPoly.ts new file: Interchain-message/scripts/privateKey.js new file: Interchain-message/scripts/sendTx/avaxToFantomBridge.js new file: Interchain-message/scripts/sendTx/avaxToFantomNativeV2.js new file: Interchain-message/test/RubicCrossChainBridge.spec.ts new file: Interchain-message/test/RubicCrossChainV2.spec.ts new file: Interchain-message/test/RubicCrossChainV3.spec.ts new file: Interchain-message/test/RubicFallback.spec.ts new file: Interchain-message/test/RubicSettings.spec.ts new file: Interchain-message/test/shared/consts.ts new file: Interchain-message/test/shared/fixtures.ts new file: Interchain-message/test/shared/utils.ts new file: Interchain-message/tsconfig.json deleted: Rubic-Inter-chain-Message-develop.zip deleted: proxy-instant-trades-master.zip deleted: rubic-app-master.zip deleted: rubic-sdk-master.zip
2024-07-09 21:32:00 +00:00
import { ethers, network, waffle } from 'hardhat';
import { swapContractFixtureInFork } from './shared/fixtures';
import { Wallet } from '@ethersproject/wallet';
import { RubicRouterV2, TestERC20, TestMessages, WETH9 } from '../typechain';
import { expect } from 'chai';
import {
DEADLINE,
DST_CHAIN_ID,
VERSION,
ZERO_ADDRESS,
DEFAULT_AMOUNT_OUT_MIN,
EXECUTOR_ADDRESS,
INTEGRATOR,
DEFAULT_AMOUNT_IN_USDC
} from './shared/consts';
import { BigNumber as BN, BigNumberish, ContractTransaction } from 'ethers';
const hre = require('hardhat');
const createFixtureLoader = waffle.createFixtureLoader;
const envConfig = require('dotenv').config();
const {
ROUTERS_POLYGON: TEST_ROUTERS,
NATIVE_POLYGON: TEST_NATIVE,
BUS_POLYGON_MAIN: TEST_BUS
} = envConfig.parsed || {};
describe('RubicFallback', () => {
let wallet: Wallet, other: Wallet;
let swapToken: TestERC20;
let transitToken: TestERC20;
let swapMain: RubicRouterV2;
let router: string;
let routerV3: string;
let wnative: WETH9;
let chainId: number;
let testMessagesContract: TestMessages;
let loadFixture: ReturnType<typeof createFixtureLoader>;
async function getMessage(
messagesContract: TestMessages,
_nonce: BigNumberish,
dstChainId: BigNumberish,
{
dex = router,
receiverEOA = other.address,
integrator = INTEGRATOR,
version = VERSION,
path = [wnative.address, transitToken.address],
pathV3 = '0x',
deadline = DEADLINE,
amountOutMinimum = DEFAULT_AMOUNT_OUT_MIN,
_receiver = wallet.address,
nativeOut = true
} = {}
): Promise<string> {
return messagesContract.getMessage(
{
dex,
nativeOut,
receiverEOA,
integrator,
version,
path,
pathV3,
deadline,
amountOutMinimum
},
_nonce,
dstChainId
);
}
async function encodePath(tokens): Promise<string> {
return tokens[0] + '000bb8' + tokens[1].slice(2);
}
async function getID(
messagesContract: TestMessages,
_nonce: BigNumberish,
{
dex = router,
receiverEOA = other.address,
integrator = ZERO_ADDRESS,
version = VERSION,
path = [wnative.address, transitToken.address],
pathV3 = '0x',
deadline = DEADLINE,
amountOutMinimum = DEFAULT_AMOUNT_OUT_MIN,
_receiver = wallet.address,
nativeOut = false,
_srcChainId = chainId,
_dstChainId = DST_CHAIN_ID
} = {}
): Promise<string> {
return messagesContract.getID(
_srcChainId,
_dstChainId,
{
dex,
nativeOut,
receiverEOA,
integrator,
version,
path,
pathV3,
deadline,
amountOutMinimum
},
_nonce
);
}
async function callExecuteMessageWithTransferFallback({
sender = ZERO_ADDRESS,
tokenIn = transitToken.address,
amountIn = DEFAULT_AMOUNT_IN_USDC,
srcChainID = chainId,
message = '0x',
executor = EXECUTOR_ADDRESS
} = {}): Promise<ContractTransaction> {
await hre.network.provider.request({
method: 'hardhat_impersonateAccount',
params: [TEST_BUS]
});
const bus = await ethers.getSigner(TEST_BUS);
await network.provider.send('hardhat_setBalance', [
bus.address,
'0x152D02C7E14AF6800000' // 100000 eth
]);
const _swapMain = swapMain.connect(bus);
return _swapMain.executeMessageWithTransferFallback(
sender,
tokenIn,
amountIn,
srcChainID,
message,
executor
// { value: nativeIn === null ? cryptoFee.add(ethers.utils.parseEther('0.01')) : nativeIn }
);
}
async function callExecuteMessageWithTransferRefund({
tokenIn = transitToken.address,
amountIn = DEFAULT_AMOUNT_IN_USDC,
message = '0x',
executor = EXECUTOR_ADDRESS
} = {}): Promise<ContractTransaction> {
await hre.network.provider.request({
method: 'hardhat_impersonateAccount',
params: [TEST_BUS]
});
const bus = await ethers.getSigner(TEST_BUS);
await network.provider.send('hardhat_setBalance', [
bus.address,
'0x152D02C7E14AF6800000' // 100000 eth
]);
const _swapMain = swapMain.connect(bus);
return _swapMain.executeMessageWithTransferRefund(tokenIn, amountIn, message, executor);
}
before('create fixture loader', async () => {
[wallet, other] = await (ethers as any).getSigners();
loadFixture = createFixtureLoader([wallet, other]);
chainId = (await ethers.provider.getNetwork()).chainId;
});
beforeEach('deploy fixture', async () => {
({ swapMain, swapToken, transitToken, wnative, router, routerV3, testMessagesContract } =
await loadFixture(swapContractFixtureInFork));
});
it('constructor initializes', async () => {
expect(await swapMain.nativeWrap()).to.eq(TEST_NATIVE);
expect(await swapMain.messageBus()).to.eq(TEST_BUS);
const routers = TEST_ROUTERS.split(',');
expect(await swapMain.getAvailableRouters()).to.deep.eq(routers);
});
describe('#Fallback and refund tests', () => {
describe('#executeMessageWithTransferFallback', () => {
beforeEach('Setup for target executions', async () => {
// transfer 1000 USDC
await transitToken.transfer(swapMain.address, ethers.BigNumber.from('1000000000'));
});
describe('Fallback should emit correct event in dst chain', async () => {
let nonce: BN;
let message: string;
beforeEach('setup before fallback', async () => {
nonce = (await swapMain.nonce()).add('1');
message = await getMessage(testMessagesContract, nonce, DST_CHAIN_ID, {
dex: ZERO_ADDRESS,
version: 2,
path: [transitToken.address],
amountOutMinimum: ethers.BigNumber.from('0')
});
});
it('Should successfully fallback token with failed bridge', async () => {
// const ID = await getID(
// testMessagesContract,
// (await swapMain.nonce()).add('1'),
// {
// dex: ZERO_ADDRESS,
// version: 2,
// path: [transitToken.address],
// amountOutMinimum: ethers.BigNumber.from('0')
// }
// );
const balanceBefore = await transitToken.balanceOf(wallet.address);
await expect(
await callExecuteMessageWithTransferFallback({
message: message
})
).to.emit(swapMain, 'CrossChainProcessed');
//.withArgs(ID, DEFAULT_AMOUNT_IN_USDC, '3');
const balanceAfter = await transitToken.balanceOf(wallet.address);
await expect(balanceBefore).to.be.eq(balanceAfter);
});
it('Should successfully fallback token with failed V3', async () => {
nonce = (await swapMain.nonce()).add('1');
const path = await encodePath([transitToken.address, swapToken.address]);
message = await getMessage(testMessagesContract, nonce, DST_CHAIN_ID, {
dex: routerV3,
version: 1,
pathV3: path
});
const balanceBefore = await transitToken.balanceOf(wallet.address);
await expect(
await callExecuteMessageWithTransferFallback({
message: message
})
).to.emit(swapMain, 'CrossChainProcessed');
const balanceAfter = await transitToken.balanceOf(wallet.address);
await expect(balanceBefore).to.be.eq(balanceAfter);
});
it('Should successfully fallback token with failed V2', async () => {
nonce = (await swapMain.nonce()).add('1');
message = await getMessage(testMessagesContract, nonce, DST_CHAIN_ID, {
dex: router,
version: 0,
path: [transitToken.address, swapToken.address]
});
const balanceBefore = await transitToken.balanceOf(wallet.address);
await expect(
await callExecuteMessageWithTransferFallback({
message: message
})
).to.emit(swapMain, 'CrossChainProcessed');
const balanceAfter = await transitToken.balanceOf(wallet.address);
await expect(balanceBefore).to.be.eq(balanceAfter);
});
});
describe('#executeMessageWithTransferRefund', () => {
beforeEach('Setup for target executions', async () => {
// transfer 1000 USDC
await transitToken.transfer(swapMain.address, '1000000000');
});
describe('Refund should emit correct event in src chain', async () => {
let nonce: BN;
let message: string;
it('Should successfully refund token with failed bridge', async () => {
nonce = (await swapMain.nonce()).add('1');
message = await getMessage(testMessagesContract, nonce, DST_CHAIN_ID, {
dex: ZERO_ADDRESS,
version: 2,
path: [transitToken.address],
amountOutMinimum: ethers.BigNumber.from('0')
});
// const ID = await getID(
// testMessagesContract,
// (await swapMain.nonce()).add('1'),
// {
// dex: ZERO_ADDRESS,
// version: 2,
// path: [transitToken.address],
// amountOutMinimum: ethers.BigNumber.from('0')
// }
// );
const balanceBefore = await transitToken.balanceOf(wallet.address);
await expect(
await callExecuteMessageWithTransferRefund({
message: message
})
).to.emit(swapMain, 'CrossChainProcessed');
// .withArgs(ID, DEFAULT_AMOUNT_IN_USDC, '3');
const balanceAfter = await transitToken.balanceOf(wallet.address);
await expect(balanceBefore.add(DEFAULT_AMOUNT_IN_USDC)).to.be.eq(
balanceAfter
);
});
it('Should successfully refund token with failed V3', async () => {
nonce = (await swapMain.nonce()).add('1');
const path = await encodePath([transitToken.address, swapToken.address]);
message = await getMessage(testMessagesContract, nonce, DST_CHAIN_ID, {
dex: routerV3,
version: 1,
pathV3: path
});
const balanceBefore = await transitToken.balanceOf(wallet.address);
await expect(
await callExecuteMessageWithTransferRefund({
message: message
})
).to.emit(swapMain, 'CrossChainProcessed');
const balanceAfter = await transitToken.balanceOf(wallet.address);
await expect(balanceBefore.add(DEFAULT_AMOUNT_IN_USDC)).to.be.eq(
balanceAfter
);
});
it('Should successfully refund token with failed V2', async () => {
nonce = (await swapMain.nonce()).add('1');
message = await getMessage(testMessagesContract, nonce, DST_CHAIN_ID, {
dex: router,
version: 0,
path: [transitToken.address, swapToken.address]
});
const balanceBefore = await transitToken.balanceOf(wallet.address);
await expect(
await callExecuteMessageWithTransferRefund({
message: message
})
).to.emit(swapMain, 'CrossChainProcessed');
const balanceAfter = await transitToken.balanceOf(wallet.address);
await expect(balanceBefore.add(DEFAULT_AMOUNT_IN_USDC)).to.be.eq(
balanceAfter
);
});
});
});
});
});
});