简体   繁体   中英

Get past transactions on a contract using web3.php

I am using web3.php script and need to get minted NFT IDs from a contract.

Solution I think about is to read past contract "transfer" events and get tokenId from its data. but couldn't find any manual on how to do this with WEB3 PHP or if there is another way to this?

PS: Because of some restrictions on development, we have to use php

PS2: It can be done in seconds using Moralis but we are using a custom chain on our local so cannot use moralis web3 api

Just adding more details on the solution, for those who view this future.

By using Moralis API you can get all the past transfer events of a contract by passing the contract address, event name and event ABI as query params. The below is the sample code.

<?php
// Dependencies to install:
// $ composer require guzzlehttp/guzzle

require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$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();

Find more at: https://docs.moralis.io/web3-data-api/reference/get-contract-events

However, if you don't have the contract ABI, you can still get all the NFT's/ NFT Id's using just the contract address. Here is an example code.

<?php
// Dependencies to install:
// $ composer require guzzlehttp/guzzle

require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$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();

Find more at: https://docs.moralis.io/web3-data-api/reference/get-contract-nfts

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM