【问题标题】:How to send already minted NFT using alchemy如何使用炼金术发送已经铸造的 NFT
【发布时间】:2022-06-25 03:41:12
【问题描述】:

我在 opensea 上铸造了一些 NFT。这些在 Polygon Mumbai 网络上。现在我想使用 alchemy web3 将这些令牌转移到其他地址。这是我正在使用的代码。

注意:这应该在 nodejs RESTful API 中运行,所以没有可用的钱包,这就是我手动签署交易的原因。

async function main() {
  require('dotenv').config();
  const { API_URL,API_URL_TEST, PRIVATE_KEY } = process.env;
  const { createAlchemyWeb3 } = require("@alch/alchemy-web3");
  const web3 = createAlchemyWeb3(API_URL_TEST);
  const myAddress = '*************************'
  const nonce = await web3.eth.getTransactionCount(myAddress, 'latest');
  const transaction = { //I believe transaction object is not correct, and I dont know what to put here
      'asset': {
        'tokenId': '******************************',//NFT token id in opensea
      },
      'gas': 53000,
      'to': '***********************', //metamask address of the user which I want to send the NFT
      'quantity': 1,
      'nonce': nonce,

    }
 
  const signedTx = await web3.eth.accounts.signTransaction(transaction, PRIVATE_KEY);
  web3.eth.sendSignedTransaction(signedTx.rawTransaction, function(error, hash) {
  if (!error) {
    console.log("???? The hash of your transaction is: ", hash, "\n Check Alchemy's Mempool to view the status of your transaction!");
  } else {
    console.log("❗Something went wrong while submitting your transaction:", error)
  }
 });
}
main();

【问题讨论】:

    标签: node.js smartcontracts web3js nft opensea


    【解决方案1】:

    假设您在浏览器中安装了 Metamask,并且 NFT 智能合约遵循ERC721 Standard

    const { API_URL,API_URL_TEST, PRIVATE_KEY } = process.env;
    const { createAlchemyWeb3 } = require("@alch/alchemy-web3");
    const {abi} = YOUR_CONTRACT_ABI
    
    const contract_address = CONTRACT ADDRESS
    require('dotenv').config();
    
    
    async function main() {
      const web3 = createAlchemyWeb3(API_URL_TEST);  
     web3.eth.getAccounts().then(accounts => {
        const account = account[0]
        const nameContract = web3.eth.Contract(abi, contract_address);
        nameContract.methods.transfer(account, ADDRESS_OF_WALLET_YOU_WANT_TO_SEND_TO, TOKEN_ID).send();
     })
    .catch(e => console.log(e));
    }
    main();
    

    【讨论】:

    • 这个可以在后端工作吗,没有浏览器。只是 nodejs
    • 它在nodejs后端不起作用,它需要连接钱包,这就是为什么我用私钥签署交易。
    【解决方案2】:

    遇到同样的问题,因为没有转移 NFT 代币的例子。 有一个很好解释的 3 部分示例 on ethereum website 来铸造 NFT。

    在第一部分step 10,它解释了如何编写合约,并提到了扩展合约对象中的现有方法:

    在我们的 import 语句之后,我们有了自定义的 NFT 智能合约,它非常短——它只包含一个计数器、一个构造函数和一个函数!这要归功于我们继承的 OpenZeppelin 合约,它实现了我们创建 NFT 所需的大部分方法,例如 ownerOf 会返回 NFT 的所有者,和 transferFrom 会转移 NFT 的所有权NFT 从一个帐户到另一个帐户。

    因此,有了这些信息,我使用我的 metamask 移动应用程序在两个地址之间进行了 NFT 转账交易。然后我通过etherscan API搜索了这个交易的JSON。

    通过这种方式,我能够通过以下脚本使用 alchemy web3 将代币转移到其他地址:

    require("dotenv").config()
    const API_URL = process.env.API_URL; //the alchemy app url
    const PUBLIC_KEY = process.env.PUBLIC_KEY; //my metamask public key
    const PRIVATE_KEY = process.env.PRIVATE_KEY;//my metamask private key
    const {createAlchemyWeb3} = require("@alch/alchemy-web3")
    const web3 = createAlchemyWeb3(API_URL)
    
    const contract = require("../artifacts/contracts/MyNFT.sol/MyNFT.json")//this is the contract created from ethereum example site
    
    const contractAddress = "" // put here the contract address
    
    const nftContract = new web3.eth.Contract(contract.abi, contractAddress)
    
    
    /**
     * 
     * @param tokenID the token id we want to exchange
     * @param to the metamask address will own the NFT
     * @returns {Promise<void>}
     */
    async function exchange(tokenID, to) {
        const nonce = await web3.eth.getTransactionCount(PUBLIC_KEY,         'latest');
    //the transaction
    const tx = {
        'from': PUBLIC_KEY,
        'to': contractAddress,
        'nonce': nonce,
        'gas': 500000,
        'input': nftContract.methods.safeTransferFrom(PUBLIC_KEY, to, tokenID).encodeABI() //I could use also transferFrom
    };
    const signPromise = web3.eth.accounts.signTransaction(tx, PRIVATE_KEY)
    
    signPromise
    
        .then((signedTx) => {
    
            web3.eth.sendSignedTransaction(
                signedTx.rawTransaction,
    
                function (err, hash) {
    
                    if (!err) {
    
                        console.log(
                            "The hash of your transaction is: ",
    
                            hash,
    
                            "\nCheck Alchemy's Mempool to view the status of your transaction!"
                        )
    
                    } else {
    
                        console.log(
                            "Something went wrong when submitting your transaction:",
    
                            err
                        )
    
                    }
    
                }
            )
    
        })
    
        .catch((err) => {
    
            console.log(" Promise failed:", err)
    
        })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2016-09-27
      • 2022-07-15
      • 2022-08-12
      • 2022-08-12
      • 2022-01-17
      • 2016-10-02
      相关资源
      最近更新 更多