PHP code example of l2iterative / bonsai-sdk-php

1. Go to this page and download the library: Download l2iterative/bonsai-sdk-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

l2iterative / bonsai-sdk-php example snippets




use L2Iterative\BonsaiSDK\Client;
use L2Iterative\BonsaiSDK\SessionId;
use L2Iterative\BonsaiSDK\SnarkId;

$client = new Client("https://api.bonsai.xyz", $test_key, $test_version);

$input_id = $client->upload_input($input_data);
$session_id = new SessionId($client->create_session($test_img_id, $input_id, []));

$status = NULL;
while(true) {
    usleep(300);
    $status = $session_id->status($client);
    
    if($status->status != 'RUNNING') {
        break;
    }
}

// error handling if the status is not successful

$snark_id = new SnarkId($client->create_snark($session_id->uuid));

$status = NULL;
while(true) {
    usleep(300);
    $status = $snark_id->status($client);
    
    if($status->status != 'RUNNING') {
        break;
    }
}

// error handling if the status is not successful

// the proof can be forwarded to the frontend to make RPC calls


public function test_serialization() {
    $example = new Struct([
        'u8v' => new SameTypeArray([
            new U8(1), new U8(231), new U8(123)
        ]),
        'u16v' => new SameTypeArray([
            new U16(124), new U16(41374)
        ]),
        'u32v' => new SameTypeArray([
            new U32(14710471), new U32(3590275702), new U32(1), new U32(2)
        ]),
        'u64v' => new SameTypeArray([
            new U64(352905235952532), new U64(2147102974910410)
        ]),
        'i8v' => new SameTypeArray([
            new I8(-1), new I8(120), new I8(-22)
        ]),
        'i16v' => new SameTypeArray([
            new I16(-7932)
        ]),
        'i32v' => new SameTypeArray([
            new I32(-4327), new I32(35207277)
        ]),
        'i64v' => new SameTypeArray([
            new I64(-1), new I64(1)
        ]),
        'u8s' => new U8(3),
        'bs' => new Boolean(true),
        'some_s' => new Some(new U16(5)),
        'none_s' => new None(),
        'strings' => new BinaryString("Here is a string."),
        'stringv' => new SameTypeArray([
            new BinaryString("string a"),
            new BinaryString("34720471290497230")
        ])
    ]);

    $serializer = new Serializer();
    $serializer->serialize($example);

    $php_output = $serializer->output();
    $rust_output = [3, 1, 231, 123, 2, 124, 41374, 4, 14710471, 3590275702, 1, 2, 2, 658142100, 82167, 1578999754, 499911, 3, 4294967295, 120, 4294967274, 1, 4294959364, 2, 4294962969, 35207277, 2, 4294967295, 4294967295, 1, 0, 3, 1, 1, 5, 0, 17, 1701995848, 544434464, 1953701985, 1735289202, 46, 2, 8, 1769108595, 1629513582, 17, 842478643, 825701424, 875575602, 858928953, 48];

    $this->assertEquals($php_output, $rust_output);
}