uint256 value from Polygon PoS to Ethereum. The same approach applies to any data type: encode the data as bytes, emit it from the child contract, then decode it in the root contract after checkpoint verification. For background on the checkpoint and predicate mechanism, see the state sync architecture docs.
Implementation
Create the root chain and child chain contracts. The function that performs the state change must also emit an event whose parameters include the data to transfer.Child contract
0x1470E07a6dD1D11eAE439Acaa6971C941C9EF48f as the value for _predicate in the root contract constructor.
Root contract
onlyPredicate modifier. It is recommended to use this modifier always because it ensures that only the predicate contract makes the state change on the root contract. The predicate contract is a special contract that triggers the root contract only when the transaction that happened on the Polygon PoS chain is verified by the RootChainManager on Ethereum chain. This ensures secure change of state on the root contract.
For testing the above implementation, we can create a transaction on the Polygon chain by calling the setData function of the child contract. We need to wait at this point for the checkpoint to be completed. The checkpoint inclusion can be checked using this script. Once checkpoint is completed, call the exit function of the RootChainManager using matic.js SDK.
txHash is the transaction hash of the transaction that happened on the child contract deployed on Polygon chain.
The logEventSignature is the keccack-256 hash of the Data event. This is the same hash that we have included in the predicate contract. All the contract code used for this tutorial and the exit script can be found here
Once the exit script is completed, the root contract on Ethereum chain can be queried to verify if the value of the variable data that was set in child contract has also been reflected in the data variable of the root contract.