Notice: update your Vizing Package as least @vizing v1.1.6
First, merge the Vizing core contract as well as the IExpertHook interface contract into your Omni-DApp
import {VizingOmniUpgradeable} from "@vizing/contracts/VizingOmni-upgradeable.sol";
import {VizingERC20HandlerUpgradeable} from "@vizing/contracts/extensions/VizingERC20Handler-upgradeable.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
contract SimultaneousTokenTransfer is
Ownable,
VizingOmniUpgradeable,
VizingERC20HandlerUpgradeable
{
constructor(address _vizingPad) Ownable(msg.sender) {
__VizingOmniInit(_vizingPad);
__VizingERC20HandlerInit(_vizingPad);
}
}
Next, the user request is formatted on the source chain Omni-DApp and sent to the Vzing core contract. The following code demonstrates the transfer of ERC20 tokens at the same time as the destination contract interaction.
Please wait, the gas fee will be charged for vizing to send full chain messages. You can call the _estimateVizingGasFee method before starting the message. When you inherit VizingOmni contract, this method is already free to use.
In our example, many parameters are written in the function for the convenience of testing. Please develop them according to the actual situation in the specific business. Developers can get Vizing GasFee fetchTransferFee calling this method on-chain or off-chain
Receives messages on the destination chain. Note that on the target chain DApp, you need to grant the Vzing LandingPad permission to call the function.
Just 1 setp to receive messages from the source chain
// _receiveMessage is Inheritance from VizingOmni
function _receiveMessage(
uint64,
/*srcChainId*/ uint256,
/*srcContract*/ bytes calldata message
) internal virtual override {
string memory m = abi.decode(message, (string));
console.logString(m);
}
After receiving the message, the LandingHook is triggered to execute. The hook obtains the current Landing status. The correct status triggers the ERC20 Transfer.
Please note that at present, the LandingHook event can only be triggered after receiving the message, that is, the token transfer can be completed at this moment.