Skip to content

Commit

Permalink
Merge branch 'main' into github-action/todd/test-script
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Feb 19, 2024
2 parents 9ad55cc + a03d435 commit 3300fe4
Show file tree
Hide file tree
Showing 3 changed files with 4,226 additions and 6,315 deletions.
106 changes: 106 additions & 0 deletions solidity/contracts/bridge/FxBridgeLogicETH.sol
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,36 @@ contract FxBridgeLogicETH is
);
}

function bridgeCall(
string memory _dstChainId,
uint256 _gasLimit,
address _receiver,
address _to,
bytes calldata _message,
uint256 _value,
bytes memory _asset
) external {
// decode and transfer asset
(string memory assetType, bytes memory assetData) = decodeType(_asset);
transferAsset(msg.sender, assetType, assetData);

// last event nonce +1
state_lastEventNonce = state_lastEventNonce.add(1);

// bridge call event
emit BridgeCallEvent(
_msgSender(),
_receiver,
_to,
state_lastEventNonce,
_dstChainId,
_gasLimit,
_value,
_message,
_asset
);
}

function submitBatch(
address[] memory _currentOracles,
uint256[] memory _currentPowers,
Expand Down Expand Up @@ -538,6 +568,70 @@ contract FxBridgeLogicETH is
return result;
}

function decodeType(
bytes memory _asset
) public pure returns (string memory, bytes memory) {
return abi.decode(_asset, (string, bytes));
}

function transferAsset(
address _sender,
string memory _assetType,
bytes memory _assetData
) internal {
if (
keccak256(bytes(_assetType)) ==
bytes32(
0x8ae85d849167ff996c04040c44924fd364217285e4cad818292c7ac37c0a345b
)
) {
transferERC20(_sender, _assetData);
} else {
revert("Asset type not support");
}
}

function transferERC20(address _from, bytes memory _asset) internal {
(address[] memory token, uint256[] memory amount) = decodeERC20(_asset);
for (uint256 i = 0; i < token.length; i++) {
require(amount[i] > 0, "amount should be greater than zero");
TokenStatus memory _tokenStatus = tokenStatus[token[i]];
require(_tokenStatus.isExist, "Unsupported token address");
require(_tokenStatus.isActive, "token was paused");

IERC20MetadataUpgradeable(token[i]).safeTransferFrom(
_from,
address(this),
amount[i]
);
if (_tokenStatus.isOriginated == true) {
IERC20ExtensionsUpgradeable(token[i]).burn(amount[i]);
}
}
}

function decodeERC20(
bytes memory _data
) internal pure returns (address[] memory, uint256[] memory) {
(bytes memory tokenBytes, uint256[] memory amounts) = abi.decode(
_data,
(bytes, uint256[])
);
uint256 tokenCount = amounts.length;
require(tokenBytes.length == tokenCount * 20, "Token not match amount");
address[] memory tokens = new address[](tokenCount);
for (uint256 i = 0; i < tokenCount; i++) {
uint256 currentTokenStartingByte = 20 + i * 20;
address currentToken;
// solhint-disable-next-line no-inline-assembly
assembly {
currentToken := mload(add(tokenBytes, currentTokenStartingByte))
}
tokens[i] = currentToken;
}
return (tokens, amounts);
}

/* ============== UPGRADE FUNCTIONS =============== */

function migrate() public onlyOwner {
Expand Down Expand Up @@ -592,4 +686,16 @@ contract FxBridgeLogicETH is
uint256[] _powers
);
event TransferOwnerEvent(address _token, address _newOwner);

event BridgeCallEvent(
address indexed _sender,
address indexed _receiver,
address indexed _to,
uint256 _eventNonce,
string _dstChainId,
uint256 _gasLimit,
uint256 _value,
bytes _message,
bytes _asset
);
}
45 changes: 19 additions & 26 deletions solidity/test/fx_bridge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {ethers, network} from "hardhat";
import {TransactionRequest} from "@ethersproject/abstract-provider/src.ts";
import {ethers} from "hardhat";
import {expect} from "chai";
import {FxBridgeLogicETH} from "../typechain-types";
import {AbiCoder, TransactionRequest} from "ethers"

describe("fork mainnet fx bridge test", function () {
it.skip("update bridge contract", async function () {
Expand All @@ -9,37 +10,25 @@ describe("fork mainnet fx bridge test", function () {
const ownerAddress = "0xE77A7EA2F1DC25968b5941a456d99D37b80E98B5"
const bridgeContractAddress = "0x6f1D09Fed11115d65E1071CD2109eDb300D80A27"

await network.provider.request({
method: "hardhat_impersonateAccount",
params: [gasAddress],
});
const gasSigner = ethers.provider.getSigner(gasAddress)
const gasSigner = await ethers.getImpersonatedSigner(gasAddress)

await gasSigner.sendTransaction({
to: adminAddress,
value: ethers.utils.parseEther("100")
value: ethers.parseEther("100")
})

await gasSigner.sendTransaction({
to: ownerAddress,
value: ethers.utils.parseEther("100")
value: ethers.parseEther("100")
})

await network.provider.request({
method: "hardhat_impersonateAccount",
params: [adminAddress],
});
const adminSigner = ethers.provider.getSigner(adminAddress)
const adminSigner = await ethers.getImpersonatedSigner(adminAddress)

await network.provider.request({
method: "hardhat_impersonateAccount",
params: [ownerAddress],
});
const ownerSigner = await ethers.getImpersonatedSigner(ownerAddress)

const ownerSigner = ethers.provider.getSigner(ownerAddress)
const bridgeFactory = await ethers.getContractFactory("FxBridgeLogicETH")

const bridgeContractV1 = bridgeFactory.attach(bridgeContractAddress)
const bridgeContractV1 = bridgeFactory.attach(bridgeContractAddress) as FxBridgeLogicETH
const oldFxBridgeId = await bridgeContractV1.state_fxBridgeId()
const oldPowerThreshold = await bridgeContractV1.state_powerThreshold()
const fxAddress = await bridgeContractV1.state_fxOriginatedToken()
Expand All @@ -56,12 +45,16 @@ describe("fork mainnet fx bridge test", function () {
}

const bridgeContract = await bridgeFactory.deploy()
await bridgeContract.deployed()
await bridgeContract.waitForDeployment()

const bridgeLogicContractAddress = await bridgeContract.getAddress()

// 0x3659cfe6 is the signature of the upgradeTo(address) function
const data = ethers.utils.hexConcat([
const abiCode = new AbiCoder;

const data = ethers.concat([
'0x3659cfe6',
ethers.utils.defaultAbiCoder.encode(['address'], [bridgeContract.address])
abiCode.encode(['address'], [bridgeLogicContractAddress])
])

const transaction: TransactionRequest = {
Expand All @@ -72,8 +65,8 @@ describe("fork mainnet fx bridge test", function () {
const upgradeTx = await adminSigner.sendTransaction(transaction)
await upgradeTx.wait()

const migrateTx = await bridgeContractV1.connect(ownerSigner).migrate()
await migrateTx.wait()
// const migrateTx = await bridgeContractV1.connect(ownerSigner).migrate()
// await migrateTx.wait()

const fxBridgeId = await bridgeContractV1.state_fxBridgeId()
const powerThreshold = await bridgeContractV1.state_powerThreshold()
Expand All @@ -100,5 +93,5 @@ describe("fork mainnet fx bridge test", function () {
const batchNonce = await bridgeContractV1.state_lastBatchNonces(bridgeToken.addr)
expect(batchNonce.toString()).to.equal(oldBatchNonce.get(bridgeToken.addr).toString())
}
});
}).timeout(100000);
});
Loading

0 comments on commit 3300fe4

Please sign in to comment.