Simple-Web3-Php
Simple-Web3-Php copied to clipboard
how to customize batch ids?
I want to make a script to batch sweep user wallet addresses(batch transfer to target address), First, batch check the balance of the user's wallet address and whether the user's address has enough gas fee, If the gas fee is insufficient, the gas fee will be sent from the centralized gas fee wallet to the specified user wallet address, but an error occurred when I executed the following script.
stdClass Object ( [jsonrpc] => 2.0 [error] => stdClass Object ( [code] => 0 [message] => found duplicated id in the batch ) [id] => )
how to customize batch ids?
$extra_curl_params = [
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
];
$sweb3 = new SWeb3('https://rpc.ankr.com/bsc', $extra_curl_params);
$sweb3->chainId = '0x38'; // Chain ID for BSC Mainnet
// Enable batching
$sweb3->batch(true);
$usdt_contract = '0x55d398326f99059ff775485246999027b3197955'; // usdt contract
$stream_opts = [ "ssl" => [ "verify_peer"=>false, "verify_peer_name"=>false, ] ];
$bep20_abi = file_get_contents("https://raw.githubusercontent.com/bnb-chain/token-bind-tool/master/contracts/bep20/bep20.abi", false, stream_context_create($stream_opts));
$contract = new SWeb3_Contract($sweb3, $usdt_contract, $bep20_abi);
$addresses = [
'0x1cFDBd2dFf70C6e2e30df5012726F87731F38164', // Example address
'0xF98728141c00D76D8568805B838619E6646E199b',
// Add other addresses as needed
];
$calls = [];
$callIndex = 0; // Track index for id generation
foreach ($addresses as $address) {
// Assign a unique ID for each call
$balanceCallId = uniqid('balance_', true);
$contract->call('balanceOf', [$address]);
$calls[$balanceCallId] = ['type' => 'balance', 'address' => $address];
// Assign a unique ID for nonce call
$nonceCallId = uniqid('nonce_', true);
$sweb3->call('eth_getTransactionCount', [$address], 'pending');
$calls[$nonceCallId] = ['type' => 'nonce', 'address' => $address];
}
// Also prepare gas price call
$gasPriceCallId = uniqid('gasPrice_', true);
$sweb3->call('eth_gasPrice', []);
$calls[$gasPriceCallId] = ['type' => 'gasPrice'];
// Execute batch
$results = $sweb3->executeBatch();
print_r($results);