Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please set provider first. #350

Open
satriawisesa03 opened this issue Mar 28, 2024 · 0 comments
Open

Please set provider first. #350

satriawisesa03 opened this issue Mar 28, 2024 · 0 comments

Comments

@satriawisesa03
Copy link

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;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant