0

我正在尝试通过 API 更新我在 OpenSea 上进行 NFT 测试的一些艺术,现在他更新了,但问题是他正在重复数字,是否可以选择一个范围内的数字但从不重复?

我的实际代码:

    const opensea = require("opensea-js");
const OpenSeaPort = opensea.OpenSeaPort;
const Network = opensea.Network;
const MnemonicWalletSubprovider = require("@0x/subproviders")
  .MnemonicWalletSubprovider;
const RPCSubprovider = require("web3-provider-engine/subproviders/rpc");
const Web3ProviderEngine = require("web3-provider-engine");

const MNEMONIC = 'SECRET';
const NODE_API_KEY = 'MyKEY';
const isInfura = true;
const FACTORY_CONTRACT_ADDRESS = '0x745e6b0CAd1eDc72647B9fFec5C69e4608f73ab2';
const NFT_CONTRACT_ADDRESS = '0x745e6b0CAd1eDc72647B9fFec5C69e4608f73ab2';
const OWNER_ADDRESS = '0xaEBB892210eB23C47b1e710561c7BC4CFA63A62e';
const NETWORK = 'rinkeby';
const API_KEY = ""; // API key is optional but useful if you're doing a high volume of requests.


if (!MNEMONIC || !NODE_API_KEY || !NETWORK || !OWNER_ADDRESS) {
  console.error(
    "Please set a mnemonic, Alchemy/Infura key, owner, network, API key, nft contract, and factory contract address."
  );
  return;
}

if (!FACTORY_CONTRACT_ADDRESS && !NFT_CONTRACT_ADDRESS) {
  console.error("Please either set a factory or NFT contract address.");
  return;
}

const BASE_DERIVATION_PATH = `44'/60'/0'/0`;

const mnemonicWalletSubprovider = new MnemonicWalletSubprovider({
  mnemonic: MNEMONIC,
  baseDerivationPath: BASE_DERIVATION_PATH,
});
const network =
  NETWORK === "mainnet" || NETWORK === "live" ? "mainnet" : "rinkeby";
const infuraRpcSubprovider = new RPCSubprovider({
  rpcUrl: isInfura
    ? "https://" + network + ".infura.io/v3/" + NODE_API_KEY
    : "https://eth-" + network + ".alchemyapi.io/v2/" + NODE_API_KEY,
});

const providerEngine = new Web3ProviderEngine();
providerEngine.addProvider(mnemonicWalletSubprovider);
providerEngine.addProvider(infuraRpcSubprovider);
providerEngine.start();

const seaport = new OpenSeaPort(
  providerEngine,
  {
    networkName:
      NETWORK === "mainnet" || NETWORK === "live"
        ? Network.Main
        : Network.Rinkeby,
    apiKey: API_KEY,
  },
  (arg) => console.log(arg)
);

async function sellTheItems() {
  // Example: simple fixed-price sale of an item owned by a user.
  console.log("Auctioning an item for a fixed price...");

  //Get a number in a range and return as a string
    function getRandomInt(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
    }   


  const fixedPriceSellOrder = await seaport.createSellOrder({
    asset: {
        tokenId: getRandomInt(0, 500).toString(),
        //tokenId: "0",
        tokenAddress: NFT_CONTRACT_ADDRESS,
    },
    startAmount: 0.36,
    expirationTime: 0,
    accountAddress: OWNER_ADDRESS,
  });
  console.log(
    `Successfully created a fixed-price sell order! ${fixedPriceSellOrder.asset.openseaLink}\n`
  );

}

async function doItAgain() {
    sellTheItems();
}
//Repeat doItAgain() every 5 seconds
setInterval(doItAgain, 5000);

我想要做的是得到一个 0 到 500 之间的数字,但永远不要重复我以前使用过的数字,我的意图是每次都获得一个数字 'tokenId'。

4

1 回答 1

0

恕我直言,使用 3 位“随机”标记是个坏主意。部分是因为你得到了重复,你需要某种存储来避免这种情况。部分原因是它们很容易暴力破解。

使用uuid Universal Unique IDentifier 包生成 128 位 uuidv4 令牌,其中 60 位是随机的,使用密码安全的伪随机数生成器。所有的 uuid,不仅仅是 uuidv4 随机的,都是为了避免重复而构建的。但是 uuidv4 令牌的优势在于它们很难让网络爬虫猜测。

const { v4: uuidv4 } = require('uuid');

...
   tokenId: uuidv4(), // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
于 2021-10-02T14:46:15.660 回答