Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Files for Walkthrough of Transferring data to Matic chain (https://docs.matic.network/docs/develop/advanced/transfer-data)
// receiver.sol
pragma solidity ^0.5.11;
// IStateReceiver represents interface to receive state
interface IStateReceiver {
function onStateReceive(uint256 stateId, bytes calldata data) external;
}
contract receiver {
uint public lastStateId;
bytes public lastChildData;
function onStateReceive(uint256 stateId, bytes calldata data) external {
lastStateId = stateId;
lastChildData = data;
}
}
[
{
"constant": false,
"inputs": [
{
"internalType": "uint256",
"name": "stateId",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "data",
"type": "bytes"
}
],
"name": "onStateReceive",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "lastChildData",
"outputs": [
{
"internalType": "bytes",
"name": "",
"type": "bytes"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "lastStateId",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
// sender.sol
pragma solidity ^0.5.11;
contract IStateSender {
function syncState(address receiver, bytes calldata data) external;
function register(address sender, address receiver) public;
}
contract sender {
address public stateSenderContract = 0x22E1f5aa1BA9e60527250FFeb35e30Aa2913727f;
address public receiver = 0x83bB46B64b311c89bEF813A534291e155459579e;
uint public states = 0;
function sendState(bytes calldata data) external {
states = states + 1 ;
IStateSender(stateSenderContract).syncState(receiver, data);
}
}
[
{
"constant": false,
"inputs": [
{
"internalType": "bytes",
"name": "data",
"type": "bytes"
}
],
"name": "sendState",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "receiver",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "states",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "stateSenderContract",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
// test.js
const Web3 = require('web3')
const Network = require("@maticnetwork/meta/network")
const network = new Network ('testnet', 'v3')
const main = new Web3(network.Main.RPC)
const matic = new Web3 (network.Matic.RPC)
let privateKey = `0x...`
matic.eth.accounts.wallet.add(privateKey)
main.eth.accounts.wallet.add(privateKey)
let receiverAddress = `0x83bB46B64b311c89bEF813A534291e155459579e`
let receiverABI = ``
let senderAddress = `0xf428d353aBdb6fdFf9E5E713f1Ec403cD15d0975`
let senderABI = ``
let sender = new main.eth.Contract(JSON.parse(senderABI), senderAddress)
let receiver = new matic.eth.Contract(JSON.parse(receiverABI), receiverAddress)
// data to sync
function getData(string) {
let data = matic.utils.asciiToHex(string);
return data
}
function getString(data) {
let string = matic.utils.hexToAscii(data);
return string
}
// console.log(getData('Sending a state sync! :) '))
async function sendData (data) {
let r = await sender.methods
.sendState (getData(data))
.send({
from: main.eth.accounts.wallet[0].address,
gas: 8000000
})
console.log('sent data from root, ', r.transactionHash)
}
async function checkSender () {
let r = await sender.methods
.states()
.call()
console.log('number of states sent from sender: ', r)
}
async function checkReceiver () {
let r = await receiver.methods
.lastStateId()
.call()
let s = await receiver.methods
.lastChildData()
.call()
console.log('last state id: ', r, 'and last data: ', s)
console.log('interpreted data: ', getString(s))
}
async function test() {
await sendData ('Hello World !')
await checkSender ()
// add a timeout here to allow time gap for the state to sync
await checkReceiver ()
}
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.