How to communicate with metamask within app

Hi,

I’m working on a crypto game which needs to communicate with Metamask to interact with the blockchain.

I have a ready code to do that from a website (not from cocos creator), I’m using “npm ethers” for that.

When I’m adding my code in Cocoscreator, all modules can load on the simulator.
But it doesn’t like when this line is called

const blockchainProvider = new ethers.providers.Web3Provider(window.ethereum);

window.ethereum is not detected.

Has someone already manage to make metamask communicate with cocosCreator directly from the app?
If so, how?

Is webview the only solution?

Any help is appreciated. thank you

BTW: I’m using cocos creator v3.4.1.

The game will be played on Android, not the web. So Metamask mobile app should be used on the phone.

I have a mid-solution to make smart contracts communicate with my game:

1/ For all calls to contract readable functions I will use an infuraIO apiKey, so no need to access metamask to read the blockchain. It’ll be done internally in the app.

2/ For calls which write something in the contract, I will create a page on my site for each function. I’ll then open the page with a Web!View. The url to that page will be a metamask compatible link, so metamask mobile will be directly open. (see metamask docs for these kind of urls).

I think it’s the best compromise , for me, to be able to create an Android/iOS application which can read & write into a contract

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.