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

Please set provider first. #350

Open satriawisesa03 opened 8 months ago

satriawisesa03 commented 8 months ago

I have created a provider according to the existing documentation but why does the error still appear? I will show the code below

public function store(Request $request)
{
    $request->validate([
        'nama' => 'required',
        'deskripsi' => 'required',
        'gambar' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

    $gambarPath = null;

    if ($request->hasFile('gambar')) {
        $gambar = $request->file('gambar');
        //dd($gambar);
        $gambarPath = $gambar->store('sertifikat');
        //dd($gambar);
    }

    $sertifikat = new Sertifikat([
        'nama' => $request->get('nama'),
        'deskripsi' => $request->get('deskripsi'),
        'gambar' => $gambarPath,
    ]);
    //dd($sertifikat);
    $sertifikat->save();

    // Menyimpan sertifikat ke blockchain
    $this->saveSertifikatToBlockchain($sertifikat->nama, $sertifikat->deskripsi, $sertifikat->gambar);

    return redirect()->route('sertifikasi.create')->with('status', 'Berhasil Menambah Sertifikat');
}

    private function saveSertifikatToBlockchain($nama, $deskripsi, $gambar)
{
// Install web3

$web3 = new Web3('HTTP://127.0.0.1:8545');
$web3 = new Web3(new HttpProvider('HTTP://127.0.0.1:8545'));

$web3->clientVersion(function ($err, $version) {
     if ($err !== null) {
         // Menampilkan pesan kesalahan
         echo 'Error: ' . $err->getMessage();
         return;
     }
     if (isset($version)) {
         // Menampilkan versi client
         echo 'Client version: ' . $version;
     }
 });

$contractAddress = '0xD499AA1784d561084e90A175f7e8459e4Aac609D'; // Alamat kontrak setelah migrasi
$contractABI = [
    [
        "inputs" => [],
        "name" => "sertifikatCount",
        "outputs" => [
            [
                "internalType" => "uint256",
                "name" => "",
                "type" => "uint256",
            ],
        ],
        "stateMutability" => "view",
        "type" => "function",
        "constant" => true,
    ],
    [
        "inputs" => [
            [
                "internalType" => "uint256",
                "name" => "",
                "type" => "uint256",
            ],
        ],
        "name" => "sertifikats",
        "outputs" => [
            [
                "internalType" => "uint256",
                "name" => "id",
                "type" => "uint256",
            ],
            [
                "internalType" => "string",
                "name" => "nama",
                "type" => "string",
            ],
            [
                "internalType" => "string",
                "name" => "deskripsi",
                "type" => "string",
            ],
            [
                "internalType" => "string",
                "name" => "gambar",
                "type" => "string",
            ],
        ],
        "stateMutability" => "view",
        "type" => "function",
        "constant" => true,
    ],
    [
        "inputs" => [
            [
                "internalType" => "string",
                "name" => "_nama",
                "type" => "string",
            ],
            [
                "internalType" => "string",
                "name" => "_deskripsi",
                "type" => "string",
            ],
            [
                "internalType" => "string",
                "name" => "_gambar",
                "type" => "string",
            ],
        ],
        "name" => "createSertifikat",
        "outputs" => [],
        "stateMutability" => "nonpayable",
        "type" => "function",
    ],
]; // ABI kontrak setelah migrasi            

$contract = new Contract('HTTP://127.0.0.1:8545', $contractABI);

// Convert gambar menjadi base64 untuk dikirim ke smart contract
$base64Image = base64_encode(Storage::get($gambar));

// Panggil fungsi createSertifikat di smart contract
$contract->at($contractAddress)->send('createSertifikat',$nama, $deskripsi, $base64Image, function ($err, $txHash) {
    if ($err !== null) {
        // Tangkap dan tangani kesalahan jika terjadi
        dd($err->getMessage());
    }
    // Tampilkan hash transaksi jika berhasil
    echo 'Transaction Hash: ' . $txHash;
});
}