web3 / web3.js

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.
https://web3js.org/
Other
19.24k stars 4.93k forks source link

How to use web3 to transfer an ERC1155 Matic token using Node.js? #4423

Closed imkane closed 2 years ago

imkane commented 3 years ago

I'm trying to write a standalone Node script that will transfer an ERC1155 token on the Matic network from my address to someone else's address.

Here's an example token: https://opensea.io/assets/matic/0x2953399124f0cbb46d2cbacd8a89cf0599974963/72846082262473004479256831974008120683339732829716081255357396830225176237696

I thought I could just use https://github.com/maticnetwork/matic.js, but it seems to only work for ERC20/ERC721 tokens.

I then played around with https://docs.moralis.io/moralis-server/sending-assets#transferring-erc1155-tokens-semi-fungible, but I can't figure out how to authenticate it using web3 or truffle + my wallet mnemonic. Moralis seems like it's meant for frontend authentication (ie MetaMask Chrome Extension), but I need this to work with backend code.

I was reading https://github.com/ChainSafe/web3.js/blob/1.x/docs/web3-eth-contract.rst but the code isn't complete .. for example, what's jsonInterface?

Does anyone know how to do this? Thanks in advance! :bow:

nazarhussain commented 3 years ago

@imkane The jsonInterface is the ABI interface of the contract. You need to have a proper ERC1155 compatible ABI for a contract that you want to interact with. Try generating ABI for this contract and then use with your example.

For the question related to my wallet mnemonic, that's not the scope of the web3 library. There must be a wallet provider connected to web3 for that purpose, which normally is MetaMask or WalletConnect. For testing purpose you can use Truffle HD Wallet Provider but I would not recommend to use for production purpose.

If you need any further help in this regard, please provide your sample code with reproducible error related to web3 and we will be happy to assist.

imkane commented 2 years ago

@nazarhussain I don't know what "try generating ABI for this contract" means :thinking:

How exactly would I generate ABI for the contract for this token? https://opensea.io/assets/matic/0x2953399124f0cbb46d2cbacd8a89cf0599974963/72846082262473004479256831974008120683339732829716081255357396830225176237696

nazarhussain commented 2 years ago

If you look at the details of that NFT, you will notice the contract address and token standard. So you actually need an ABI for ERC-1155 standard. On the above link you will find the ABI for the contract https://polygonscan.com/address/0x2953399124f0cbb46d2cbacd8a89cf0599974963#code

image

imkane commented 2 years ago

@nazarhussain I appreciate your help with this. It's a bit odd that there's no guide anywhere with a complete example - isn't this a fairly common thing I'm trying to do? :thinking: I read through a bunch of StackOverflow pages, but still couldn't find the answer.

So from the Polygonscan link you provided, I copied the ABI code: Selection_964

And saved it into a file called abi.json.

Here's my code now:

const fs = require("fs");
const abiFile = fs.readFileSync("./abi.json", {
  encoding: "utf8",
});
const abi = JSON.parse(abiFile);

var Contract = require("web3-eth-contract");

var yargs = require("yargs");
const { hideBin } = require("yargs/helpers");
const argv = yargs(hideBin(process.argv)).argv;

// Get wallet mnemonic from command line
const walletMnemonic = argv.wallet_mnemonic;
console.log("walletMnemonic = " + walletMnemonic);

// Define vars
const contractAddress = "0x2953399124f0cbb46d2cbacd8a89cf0599974963";
const tokenId =
  "72846082262473004479256831974008120683339732829716081255357396830225176237696";

// Set provider for all later instances to use
Contract.setProvider("ws://localhost:8546");

// Get contract
var contract = new Contract(abi, contractAddress);

// Transfer token
...

But I'm not sure what to do next ...

The sample code I've seen for contract.methods.transfer() never has a parameter for tokenId, so how can I specify the exact token to be transferred?

nazarhussain commented 2 years ago

@imkane

There are huge variety of contracts in Ethereum and EVM compatible ecosystem. As a library maintainers we can't document or provide examples for the usage of each contract due to limitation of resources and also to focus our effort on the right direction which is to provide you a stable and easy to use library.

For your case you should read the ERC1155 documentation. There is no transfer interface for the purpose you are trying to achieve rather use safeTransferFrom. Which is very well documented on the following link.

I am closing this issue as its not related to the usage of library rather usage of the eth contracts. If you need any further help in this regard, please feel free to join our Discord channel or ask wonderful community on the Stackoverflow.