maticnetwork / matic.js

Javascript developer library to interact with Matic Network
https://wiki.polygon.technology/docs/tools/matic-js/get-started/
MIT License
543 stars 259 forks source link

posClient.exitUtil.buildPayloadForExit Invalid Payload for Goerli/Mumbai Transaction #315

Closed c194 closed 2 years ago

c194 commented 2 years ago

Describe the bug

The maticjs library returns a different, invalid burnproof payload than when I access the API directly for Mumbai transactions.

For example:

I submit this withdrawal transaction on Mumbai: https://mumbai.polygonscan.com/tx/0xc2668907d53f85d1ca1808622e51dc8645c8e9e7cf1393347a156e015309160a

When I access the API directly using https://apis.matic.network/api/v1/mumbai/exit-payload/0xc2668907d53f85d1ca1808622e51dc8645c8e9e7cf1393347a156e015309160a?eventSignature=0x8c5261668696ce22758910d05bab8f186d6eb247ceac2af2e82c7dc17669b036

I get a payload starting with 0xf90da58428db4a20b90100a...

When I submit this payload on Goerli, everything works as intended: https://goerli.etherscan.io/tx/0x4b39adaace7a9645067a247bf74b5da54cc6e2dcdf0bb83660339e5a09b3eb7f

HOWEVER

When I use the maticjs library to generate the burnproof, it returns a different payload starting with 0xf90ca38428db4a20808401... - trying to use this burnproof results in a Fail with Error: Leaf Index is too Big for the Goerli transaction.

Requested Resolution

Investigate why the library returns a different payload than the Web API call and resolve the discrepancy

My Configuration to Reproduce

generate_burnproof.js

const secrets = require("../secrets.json")
const { POSClient, use} = require('@maticnetwork/maticjs')
const { Web3ClientPlugin } = require("@maticnetwork/maticjs-web3");
const HDWalletProvider = require('@truffle/hdwallet-provider')

const withdraw_transaction = "0xc2668907d53f85d1ca1808622e51dc8645c8e9e7cf1393347a156e015309160a";

// install web3 plugin
use(Web3ClientPlugin);

const execute = async () => {
    const posClient = new POSClient();
    await posClient.init({
        log: true,
        network: 'testnet',
        version: 'mumbai',
        parent: {
            provider: new HDWalletProvider(secrets["privatekey"], secrets["goerlinode"]),
            defaultConfig: {
                from : secrets["useraddress"]
            }
        },
        child: {
            provider: new HDWalletProvider(secrets["privatekey"], secrets["mumbainode"]),
            defaultConfig: {
                from : secrets["useraddress"]
            }
        }
    });

    const proof = await posClient.exitUtil.buildPayloadForExit(
        withdraw_transaction, // Withdraw transaction hash
        "0x8c5261668696ce22758910d05bab8f186d6eb247ceac2af2e82c7dc17669b036"  // MESSAGE_SENT_EVENT_SIG: Do not change this
    )
    console.log(" ----- BURN PROOF -----")
    console.log(proof)
}

execute().then(() => {
}).catch(err => {
console.error("err", err);
}).finally(_ => {
process.exit(0);
})

package.json

{
  "name": "blockchain",
  "version": "0.0.0",
  "private": true,
  "devDependencies": {
    "@chainlink/contracts": "^0.2.2",
    "@maticnetwork/fx-portal": "fx-portal/contracts",
    "@maticnetwork/maticjs-web3": "^1.0.0",
    "@nomiclabs/hardhat-ethers": "^2.0.2",
    "@nomiclabs/hardhat-etherscan": "^2.1.7",
    "@nomiclabs/hardhat-waffle": "^2.0.1",
    "@openzeppelin/contracts": "^4.3.3",
    "@openzeppelin/contracts-upgradeable": "^4.4.0",
    "@truffle/hdwallet-provider": "^2.0.0",
    "chai": "^4.3.4",
    "ethereum-waffle": "^3.2.0",
    "ethers": "^5.4.7",
    "etherscan": "^0.2.2",
    "fx-portal": "^1.0.3"
  },
  "dependencies": {
    "@maticnetwork/maticjs": "^3.2.0",
    "@openzeppelin/test-helpers": "^0.5.15",
    "dotenv": "^14.3.2",
    "hardhat": "^2.8.2",
    "test-helpers": "^0.1.4"
  }
}
ujjwalguptaofficial commented 2 years ago

so i tried using the buildPayloadForExit and i received same result -

const client = new POSClient();

  await client.init({
    log: true,
    network: 'testnet',
    version: 'mumbai',
    parent: {
      provider: new HDWalletProvider(privateKey, rpc.parent),
      defaultConfig: {
        from
      }
    },
    child: {
      provider: new HDWalletProvider(privateKey, rpc.child),
      defaultConfig: {
        from
      }
    }
  });

  // client.rootChainManager.processReceivedMessage()
  console.log("init called");

  const mumbaiERC20Token = client.erc20(mumbaiERC20);
  const goerliERC20Token = client.erc20(goerliERC20, true);
  const goerliERC721Token = client.erc721(pos.parent.erc721, true);
  const mumbaiERC721Token = client.erc721(pos.child.erc721);
  const goerliERC1155Token = client.erc1155(pos.parent.erc1155, true);
  const mumbaiERC1155Token = client.erc1155(pos.child.erc1155);

  // setProofApi("https://apis.matic.network");

  var result = await client.exitUtil.buildPayloadForExit('0xc2668907d53f85d1ca1808622e51dc8645c8e9e7cf1393347a156e015309160a','0x8c5261668696ce22758910d05bab8f186d6eb247ceac2af2e82c7dc17669b036');

  return console.log('result', result);

sharing the screenshot for ref - Screenshot from 2022-01-28 20-36-03

ujjwalguptaofficial commented 2 years ago

I am not sure what's happening in your case, but i will ask you to try changing the RPC.

also you could try with proof-api using -

setProofApi("https://apis.matic.network");

  var result = await client.exitUtil.buildPayloadForExit('0xc2668907d53f85d1ca1808622e51dc8645c8e9e7cf1393347a156e015309160a','0x8c5261668696ce22758910d05bab8f186d6eb247ceac2af2e82c7dc17669b036', true);

  return console.log('result', result);

just so you know - proof api also internally uses this code to return the proof.

c194 commented 2 years ago

Thanks for the quick response @ujjwalguptaofficial ! It turns out this was RPC related. I was using a Speedy Node for Mumbai through Moralis, but switching my RPC to "https://matic-mumbai.chainstacklabs.com" resolved the issue. Thanks for your help!