Hello people, I am new here and I am currently working on my final year project, however I have faced some programming issue while trying to interact with smart contract that I have deployed onto the Ganache Blockchain, the objective is simple, i wanted to store some data such as amount, amount_type and description onto the smart contract storage and I could be able to retrieve the data from it. The code below is my smart contract function using Solidity. And i have successfully deployed onto Ganache Network.
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
pragma experimental ABIEncoderV2;
contract CompanyData {
struct Data {
uint256 amount;
string amount_type;
string cid;
}
address public admin;
Data[] public dataList;
uint256 public dataCount = 0;
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 memory localData = Data(_amount, _amount_type, _cid);
dataList.push(localData);
dataCount++;
}
function getAllData() public view returns (Data[] memory) {
return dataList;
}
function getDataCount() public view returns (uint){
return dataCount;
}
}
In my Laravel project, I have installed the Web3p/web3.php from Github, based on my understanding, for my scenario, i probably going to be using the contract->send() and contract->call() functions only right? I did specified all the requirements needed like sender address, contract address, arguments and callback functions. The function below is used to interact with the smart contract storedata function.
function storeData(Request $req)
{
$response = $this->addStatement($req);
$contractAddress = '0x972b3c22070e2786039bAf93a1C90727D683DD7c';
$adminAddress = "0x6c0d19425c4c226f1c107ff3606eb1b21ca44370";
$cid = $response->getData()->cid;
$amount = intval($req->input('amount'));
$amount_type = $req->input('amount_type');
($this->contract)->at($contractAddress)->send('storeData', $amount, $amount_type, $cid, function ($err, $transaction) {
if ($err !== null) {
// Handle error
echo 'Error: ' . $err;
return;
}
// Handle successful transaction
echo 'Transaction hash: ' . $transaction;
});
return redirect('/');
}
The function below is to call the getdata function in smart contract
function retrieveData()
{
$contractAddress = '0x972b3c22070e2786039bAf93a1C90727D683DD7c';
$this->contract->at($contractAddress)->call('getAllData', function ($err, $transaction) {
if ($err !== null) {
// Handle error
echo 'Error: ' . $err;
return;
}
// Handle successful transaction
dd($transaction);
});
}
Basically, i could create a transaction and the transaction will be mined into a block in the Ganache network, however, I do not think the data is being stored into the smart contract storage.
I might need brothers and sisters help for this because I have been stucked for 2 weeks :(
Top comments (0)