3

我在 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();
4

1 回答 1

2

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

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();
于 2022-03-03T17:18:46.783 回答