3

Is there a way to replace geth with just using metamask via some type of api via the command line? I want to interface with metamask to send and eth and erc20 tokens.

Right now geth works, but it has to be in "light mode" for me to operate it without the full node requirements. It would be great if I can somehow tap into metamask to do these transactions instead.

4 Answers 4

3
+25

Metamask is a browser extension, thus its scope is only within a browser. You can write automation scripts but that's going to be too cumbersome and unstable.

A better alternative would be to use something like hardhat that can connect to a JSON-RPC node to send your transactions. It's javascript so you can easily create custom scripts for different functions.

3
  • Hardhat is the answer here. Jan 25, 2022 at 19:31
  • how is hardhat compared to eth-cli ? Jan 27, 2022 at 16:27
  • It really depends on the usecase. Hardhat let's you write any kind of actions you want to perform, in javascript. For example you can listen for an event and trigger something based on that, check for a value and such. You can also use ethers.js and run it as a nodejs application.
    – pbsh
    Jan 27, 2022 at 16:40
3

Metamask is simply a signer(provider + wallet) that interacts with other nodes. You can simply use the ethers.js library using a third party API providers like Alchemy/Infura and write automated scripts to interact with those API nodes. ERC20/ERC721 transfers are just contract interactions and can be done via the same ethers.js scripts.

You can also use your own node with ethers.js library. Instead of a third party API node, you can also specify your local JSON-RPC node and the PORT NUMBER on your system for ethers to submit the transactions.

2

You should be using a frontend facing library like ethers.js which may or may not require a connection to a node provider.

1

From your question it seems that you want to control someone's account to perform operations (e.g. send tokens, ETH..).

About "controlling" MetaMask. It is not possible. As Pranay said, MetaMask is a signer and it is not supposed to be interacted to, if not from the user.

What you can do instead is:

  • Have the user give you their private key. I am saying this only for academic reasons, but with the user private key you can do everything! Of course it's very difficult that a user will trust you so much to give you the private key, so I would say to DO NOT CONSIDER THIS OPTION.

Pros: god mode on user's wallet

Cons: Too risky, no one will give you their private key

  • Have a smart contract do these operations for the user. The user would have to approve the ERC20 tokens they want to move and then the contract would "get" them from the user's wallet and move them somewhere else. The transaction that moves the funds can be called by the same user or by anyone else.

Pros: easy to do

Cons: cannot move ETH, only ERC20

  • Have the user send their funds to a wallet you control. Same as option 1, you'd have total control over that wallet and the user would send you only the amount of funds they are willing to entrust you with.

Pros: god mode on your own wallet

Cons: User has to send you the funds (or have a contract doing so). I have never seen this happening but, who knows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.