web3p / web3.php

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

Unable to publish a transaction on blockchain. #275

Open deathemporer opened 2 years ago

deathemporer commented 2 years ago

I am using ganache for blockchain and have deployed my smart contract on remix ide. The methods which involve accessing data that has been published using remix work fine, but the methods which involve pushing data on the blockchain dont work.

Here is my smart contract` // SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

import "@openzeppelin/contracts/utils/Strings.sol"; contract primeShip{ struct Product{ address creator; string productName; string productBrand; uint256 productId; }

struct Transaction{
    uint256 productId;
    string txn_id;
    string sentTo;
    string location;
    string date;
}

mapping(uint => Product) allProducts;
mapping(string => Transaction) allTransactions;
mapping(uint => address) brands;
uint256 items = 0;

function concat(string memory _a, string memory _b) public pure returns (string memory){
    bytes memory bytes_a = bytes(_a);
    bytes memory bytes_b = bytes(_b);
    string memory length_ab = new string(bytes_a.length + bytes_b.length);
    bytes memory bytes_c = bytes(length_ab);
    uint k = 0;
    for (uint i = 0; i < bytes_a.length; i++) bytes_c[k++] = bytes_a[i];
    for (uint i = 0; i < bytes_b.length; i++) bytes_c[k++] = bytes_b[i];
    return string(bytes_c);
}

function newItem(string memory _name, uint _id, string memory _brand) public returns(bool){
    Product memory curItem = Product({creator: msg.sender, productName: _name, productBrand: _brand, productId: _id});
    //Product memory curItem = Product({creator: msg.sender, productName: _name, productBrand: _brand, productId: _id});
    allProducts[_id] = curItem;
    brands[_id] = msg.sender;
    items = items + 1;
    return true;
}

function addDetails(uint _id, string memory _txn_id, string memory _date, string memory _to, string memory _location) public onlyBrand(_id) returns(bool){
    //Product memory curItem = Product({creator: msg.sender, productName: _name, productBrand: _brand, sentTo: _to, productId: _id, date: _date, location: _location});
    Transaction memory curItem = Transaction({productId: _id, txn_id: _txn_id, date: _date, location: _location, sentTo: _to});
    allTransactions[_txn_id] = curItem;
    return true;
}

modifier onlyBrand(uint _id ) {
    require (msg.sender == brands[_id]);
    _;
}

function getProd(uint _id) public view returns(string memory){
    string memory output = "Product Id: ";
    output = concat(output, Strings.toString(allProducts[_id].productId));
    output = concat(output, " Name: ");
    output = concat(output, allProducts[_id].productName);
    output = concat(output, " Brand: ");
    output = concat(output, allProducts[_id].productBrand);
    return output;
}

function getItems(string memory _txn_id) public view returns(string memory){
    string memory output="Product Name: ";
    output=concat(output, allProducts[allTransactions[_txn_id].productId].productName);
    output=concat(output, ", Brand: "); 
    output=concat(output, allProducts[allTransactions[_txn_id].productId].productBrand);
    output=concat(output, ", Sent to: ");
    output=concat(output, allTransactions[_txn_id].sentTo);
    output=concat(output, ", Manufacture Date: ");
    output=concat(output, allTransactions[_txn_id].date);
    output=concat(output, ", Manufacture Location: ");
    output=concat(output, allTransactions[_txn_id].location);
    return output;
}

} ` Here is my php file

` <?php //include('../Code/vendor/web3-php/web3/src/'); require DIR.'/vendor/autoload.php'; use Web3\Web3; use Web3\Contract;

    $web3 = new Web3('http://127.0.0.1:7545');

    //$ans = $web3->eth->getBalance('0xCe5C289Cf7ECeBd1886e17238FD976aEAb77caE0')->toEth(); // 100

    //echo "<script>console.log(".$ans.");</script>";
    $abi = '[
{
    "inputs": [
        {
            "internalType": "uint256",
            "name": "_id",
            "type": "uint256"
        },
        {
            "internalType": "string",
            "name": "_txn_id",
            "type": "string"
        },
        {
            "internalType": "string",
            "name": "_date",
            "type": "string"
        },
        {
            "internalType": "string",
            "name": "_to",
            "type": "string"
        },
        {
            "internalType": "string",
            "name": "_location",
            "type": "string"
        }
    ],
    "name": "addDetails",
    "outputs": [
        {
            "internalType": "bool",
            "name": "",
            "type": "bool"
        }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
},
{
    "inputs": [
        {
            "internalType": "string",
            "name": "_name",
            "type": "string"
        },
        {
            "internalType": "uint256",
            "name": "_id",
            "type": "uint256"
        },
        {
            "internalType": "string",
            "name": "_brand",
            "type": "string"
        }
    ],
    "name": "newItem",
    "outputs": [
        {
            "internalType": "bool",
            "name": "",
            "type": "bool"
        }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
},
{
    "inputs": [
        {
            "internalType": "string",
            "name": "_a",
            "type": "string"
        },
        {
            "internalType": "string",
            "name": "_b",
            "type": "string"
        }
    ],
    "name": "concat",
    "outputs": [
        {
            "internalType": "string",
            "name": "",
            "type": "string"
        }
    ],
    "stateMutability": "pure",
    "type": "function"
},
{
    "inputs": [
        {
            "internalType": "string",
            "name": "_txn_id",
            "type": "string"
        }
    ],
    "name": "getItems",
    "outputs": [
        {
            "internalType": "string",
            "name": "",
            "type": "string"
        }
    ],
    "stateMutability": "view",
    "type": "function"
},
{
    "inputs": [
        {
            "internalType": "uint256",
            "name": "_id",
            "type": "uint256"
        }
    ],
    "name": "getProd",
    "outputs": [
        {
            "internalType": "string",
            "name": "",
            "type": "string"
        }
    ],
    "stateMutability": "view",
    "type": "function"
}

]';

    $contractAddress = "0x993158FCa323699f18D640aa0c5CECfB9bE2cc7b";

    $contract = new Contract('http://127.0.0.1:7545', $abi);

    $params = array("1");

    $contract->at($contractAddress)->call("concat", "hello", "fren" , function ($err, $data) {
            if ($err !== null) {
              throw new Exception($err->getMessage());
            }
            var_dump($data);
            $result = $data;
          });

    $contract->at($contractAddress)->call("getItems", "TXN1" , function ($err, $data) {
            if ($err !== null) {
              throw new Exception($err->getMessage());
            }
            var_dump($data);
            $result = $data;
          });
     $contract->at($contractAddress)->call("newItem", $_GET['name'], $_GET['pid'], $_GET['bname'] , ['from'=> "0xCe5C289Cf7ECeBd1886e17238FD976aEAb77caE0",'gas'=>"0x200b20"], function ($err, $data) {
            if ($err !== null) {
              throw new Exception($err->getMessage());
            }
            var_dump($data);
            $result = $data;
          });

     $contract->at($contractAddress)->call("getProd", $_GET['pid'] , function ($err, $data) {
            if ($err !== null) {
              throw new Exception($err->getMessage());
            }
            var_dump($data);
            $result = $data;
          });

?>

`

How do I use the methods newItem and addDetails