web3p / web3.php

A php interface for interacting with the Ethereum blockchain and ecosystem. Native ABI parsing and smart contract interactions.
MIT License
1.17k stars 555 forks source link

Issue with constructor->send() function #351

Open aerory opened 8 months ago

aerory commented 8 months ago

This is my solidity code: // SPDX-License-Identifier: MIT pragma solidity >=0.4.22 <0.9.0;

contract CompanyData { address public admin; struct Data { uint256 amount; string amount_type; string cid; }

mapping(address => Data) public dataStore;

constructor() public {
    admin = msg.sender;
}

modifier onlyAdmin() {
    require(msg.sender == admin, "Only admin can call this function.");
    _;
}

function storeData(uint256 _amount, string memory _amount_type, string memory _cid) public onlyAdmin {
    Data storage data = dataStore[msg.sender];
    data.amount = _amount;
    data.amount_type = _amount_type;
    data.cid = _cid;
}

function getData() public view returns (uint256, string memory, string memory) {
    Data storage data = dataStore[msg.sender];
    return (data.amount, data.amount_type, data.cid);
}

}

//I will be calling the storeData() function in my Laravel Project function storeData(Request $req) { $response = $this->addStatement($req); $contractAddress = '0x998A16edcb330DA3bf9B00065dc9091870B20dc3'; $cid = $response->getData()->cid; $amount = intval($req->input('amount')); $amount_type = $req->input('amount_type'); $callback = function ($err, $result) { if ($err !== null) { print('failed to interact with the smart contract function: ' . $err); return; } };

try {
  $store = $this->contract->at($contractAddress)->send('storeData', $amount, $amount_type, $cid, $callback);
} catch (Exception $e) {
  print('Error interacting with the smart contract: ' . $e->getMessage());
  return;
}
return redirect('/');

} function addStatement(Request $request) { $data = $request->all(); // Retrieve all input data $description = $request->input('description'); $key = env('ENCRYPTION_KEY');

// **Encryption Step:**
if ($description && $key) {
  $encryptedDescription = $this->encryptDataWithOpenSSL($description, $key);
} else {
  // Handle missing data or encryption key
  return response()->json(['error' => 'Description or encryption key missing'], 400);
}

// Check if encryption was successful
if (!isset($encryptedDescription)) {
  // Handle encryption failure
  return response()->json(['error' => 'Encryption failed'], 500);
}

try {

  $ipfs = new IPFS();
  //print("Connected to IPFS\n");

  // Attempt to add content to IPFS
  $result = $ipfs->add($encryptedDescription);
  if (!empty($result)) {
    $cid = $result;
    //print("Stored encrypted data on IPFS with CID: " . $cid . "\n");
    // dd($encryptedDescription, $description, $data['amount'], $data['amount_type'], $cid);
    // Return the CID as a response
    return response()->json(['cid' => $cid], 200);
  } else {
    // Print or log the error message
    print("Error storing data on IPFS: Unable to retrieve CID\n");
    print_r($result); // Print the result for further debugging

    // Handle failure to retrieve CID
    return response()->json(['error' => 'Unable to retrieve CID from IPFS'], 500);
  }
} catch (Exception $e) {
  // Print or log the error message
  print("Error connecting to IPFS: " . $e->getMessage() . "\n");

  // Handle connection error
  return response()->json(['error' => 'Error connecting to IPFS'], 500);
}

} I wanted to store some data such as the amount, amount_type and CID onto the Ganache network by interacting with the smart contract which i have deployed onto the Ganache Blockchain, however, it kept prompting me "Wrong type of eth_sendTransaction method argument 0." but i think the way i call the contract->send() looks just find, Github Copilot suggest me to write my param like this "[$amount, $amount_type, $cid]", but if i do that, i will get this error stating "Please make sure you have put all function params and callback."