Skip to content

Commit

Permalink
feat: merge encode decode into CrossChainCall (#329)
Browse files Browse the repository at this point in the history
Co-authored-by: todd <[email protected]>
  • Loading branch information
zakir-code and todd-woko authored Apr 10, 2024
1 parent 8d1a6d8 commit a7b95db
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 170 deletions.
2 changes: 1 addition & 1 deletion contract/FIP20Upgradable.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion contract/WFXUpgradable.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions contract/contract.go

Large diffs are not rendered by default.

158 changes: 154 additions & 4 deletions solidity/contracts/crosschain/CrossChainCall.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,162 @@
// SPDX-License-Identifier: Apache-2.0

/* solhint-disable one-contract-per-file */
pragma solidity ^0.8.0;

/* solhint-disable no-global-import */
import "./Encode.sol";
import "./Decode.sol";
library Decode {
function crossChain(bytes memory data) internal pure returns (bool) {
bool result = abi.decode(data, (bool));
return result;
}

// Deprecated: for fip20 only
function fip20CrossChain(bytes memory data) internal pure returns (bool) {
bool result = abi.decode(data, (bool));
return result;
}

function cancelSendToExternal(
bytes memory data
) internal pure returns (bool) {
bool result = abi.decode(data, (bool));
return result;
}

function increaseBridgeFee(bytes memory data) internal pure returns (bool) {
bool result = abi.decode(data, (bool));
return result;
}

function bridgeCoinAmount(
bytes memory data
) internal pure returns (uint256) {
uint256 amount = abi.decode(data, (uint256));
return amount;
}

function bridgeCall(bytes memory data) internal pure returns (bool) {
return abi.decode(data, (bool));
}

function ok(
bool _result,
bytes memory _data,
string memory _msg
) internal pure {
if (!_result) {
string memory errMsg = abi.decode(_data, (string));
if (bytes(_msg).length < 1) {
revert(errMsg);
}
revert(string(abi.encodePacked(_msg, ": ", errMsg)));
}
}
}

library Encode {
function crossChain(
address _token,
string memory _receipt,
uint256 _amount,
uint256 _fee,
bytes32 _target,
string memory _memo
) internal pure returns (bytes memory) {
return
abi.encodeWithSignature(
"crossChain(address,string,uint256,uint256,bytes32,string)",
_token,
_receipt,
_amount,
_fee,
_target,
_memo
);
}

/* solhint-enable no-global-import */
// Deprecated: for fip20 only
function fip20CrossChain(
address _sender,
string memory _receipt,
uint256 _amount,
uint256 _fee,
bytes32 _target,
string memory _memo
) internal pure returns (bytes memory) {
return
abi.encodeWithSignature(
"fip20CrossChain(address,string,uint256,uint256,bytes32,string)",
_sender,
_receipt,
_amount,
_fee,
_target,
_memo
);
}

function cancelSendToExternal(
string memory _chain,
uint256 _txid
) internal pure returns (bytes memory) {
return
abi.encodeWithSignature(
"cancelSendToExternal(string,uin256)",
_chain,
_txid
);
}

function increaseBridgeFee(
string memory _chain,
uint256 _txid,
address _token,
uint256 _fee
) internal pure returns (bytes memory) {
return
abi.encodeWithSignature(
"increaseBridgeFee(string,uin256,address,uint256)",
_chain,
_txid,
_token,
_fee
);
}

function bridgeCoinAmount(
address _token,
bytes32 _target
) internal pure returns (bytes memory) {
return
abi.encodeWithSignature(
"bridgeCoinAmount(address,bytes32)",
_token,
_target
);
}

function bridgeCall(
string memory _dstChainId,
uint256 _gasLimit,
address _receiver,
address _to,
bytes calldata _message,
uint256 _value,
bytes memory _asset
) internal pure returns (bytes memory) {
return
abi.encodeWithSignature(
"bridgeCall(string,uint256,address,address,bytes,uint256,bytes)",
_dstChainId,
_gasLimit,
_receiver,
_to,
_message,
_value,
_asset
);
}
}

library CrossChainCall {
address public constant CROSS_CHAIN_ADDRESS =
Expand Down
53 changes: 0 additions & 53 deletions solidity/contracts/crosschain/Decode.sol

This file was deleted.

108 changes: 0 additions & 108 deletions solidity/contracts/crosschain/Encode.sol

This file was deleted.

Loading

0 comments on commit a7b95db

Please sign in to comment.