【问题标题】:Get past transactions on a contract using web3.php使用 web3.php 获取合约的过去交易
【发布时间】:2023-01-19 17:31:24
【问题描述】:

我正在使用 web3.php 脚本,需要从合同中获取铸造的 NFT ID。

我想到的解决方案是读取过去的合同“转移”事件并从其数据中获取 tokenId。 但找不到任何关于如何使用 WEB3 PHP 执行此操作的手册,或者是否有其他方法可以做到这一点?

PS:由于开发上的一些限制,我们不得不使用php

PS2:使用 Moralis 可以在几秒钟内完成,但我们在本地使用自定义链,因此不能使用 moralis web3 api

【问题讨论】:

    标签: php erc721 web3php


    【解决方案1】:

    只是为那些看到这个未来的人添加更多关于解决方案的细节。

    通过使用 Moralis API,您可以通过将合约地址、事件名称和事件 ABI 作为查询参数传递来获取合约过去的所有 transfer 事件。下面是示例代码。

    <?php
    // Dependencies to install:
    // $ composer require guzzlehttp/guzzle
    
    require_once('vendor/autoload.php');
    
    $client = new GuzzleHttpClient();
    
    $response = $client->request('POST', 'https://deep-index.moralis.io/api/v2/0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984/events?chain=eth&topic=0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', [
      'body' => '{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Transfer","type":"event"}',
      'headers' => [
        'Accept' => 'application/json',
        'X-API-Key' => 'Moralis_API_Key',
        'Content-Type' => 'application/json',
      ],
    ]);
    
    echo $response->getBody();
    

    在以下位置查找更多信息:https://docs.moralis.io/web3-data-api/reference/get-contract-events

    但是,如果您没有合约 ABI,您仍然可以仅使用合约地址获取所有 NFT/NFT Id。这是一个示例代码。

    <?php
    // Dependencies to install:
    // $ composer require guzzlehttp/guzzle
    
    require_once('vendor/autoload.php');
    
    $client = new GuzzleHttpClient();
    
    $response = $client->request('GET', 'https://deep-index.moralis.io/api/v2/nft/0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB?chain=eth&format=decimal', [
      'headers' => [
        'Accept' => 'application/json',
        'X-API-Key' => 'Moralis_API_Key',
      ],
    ]);
    
    echo $response->getBody();
    

    在以下位置查找更多信息:https://docs.moralis.io/web3-data-api/reference/get-contract-nfts

    希望这可以帮助。

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 2022-10-06
      • 1970-01-01
      • 2021-10-07
      • 2020-08-25
      • 2022-07-19
      • 2018-08-02
      • 2021-10-06
      • 2018-11-30
      相关资源
      最近更新 更多