简体   繁体   中英

how to send and receive tokens via Solana JSONRPC

there's the following ETH JSONRPC implementation to transfer tokens, which can be utilised via curl in PHP and I would like to do EXACTLY the same but on the Solana Blockchain - which supports it's own JSONRPC implementation

var whatever= {};
whatever.jsonrpc="2.0";
whatever.id=1;
whatever.method="eth_sendTransaction";
whatever.params= [];
whatever.params[0].from="0x52f273a06a420453aa5b33c4f175395c9a1fddd8";
whatever.params[0].to=data.ethAddress;
whatever.params[0].value=1e18;
whatever.params[0].currency="xxx";

source on stack overflow for the above code is here

as the Solana Documentation mentions only sendTrasactions here are my two questions:

  1. how to implement the above example using Solana (see below our curl implementation of the Solana JSONRPC to get a user's current token amount in php ie "getTokenAmount")
  2. where do I get the fully-signed Transaction as an encoded string which is a parameter for the "sendTransaction"*

*I assume the fully-signed Transaction is issued once the transfer is made, no?

----- example of our implementation as mentioned above (for those who might be interested in it -----

public function getTokenAmount($wallet_address, $token_address)
{
    $data = array(
        "jsonrpc" => "2.0",
        "id" => 1,
        "method" => "getTokenAccountsByOwner",

        "params" => array(
            0 => $wallet_address,
            1 => array(
                "mint" => $token_address
            ),
            2 => array(
                "encoding" => "jsonParsed"
            )
        )
    );


    $data = json_encode($customer);
    $response = $this->initCurl($data);

    return $response->result->value[0]->account->data->parsed->info->tokenAmount->uiAmount;
}

private function initCurl($data)
{

    $ch = @curl_init();
    @curl_setopt($ch, CURLOPT_POST, true);
    @curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    @curl_setopt($ch, CURLOPT_URL, $this->_endpoint);
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    @curl_setopt($ch, CURLOPT_HTTPHEADER, array( "accept: application/json", "content-type: application/json"));

    $response = json_decode(@curl_exec($ch));
    $err = @curl_error($ch);

    curl_close($ch);

    // TODO: ERROR HANDLING
    if ($err) {
        return false; //"cURL Error #:" . $err;
    }

    return $response;

}

The answers to 1 and 2 are very similar.

In order to send a transaction, you must create a base64 or base58 encoded string with all of the transaction data, which includes accounts, data, and any other flags needed. After that, you must sign the transaction with a ed25519 key, and provide that whole string as the transaction.

You can build this yourself by following how it's done for JS:

Your better bet, however, would be to reuse an existing library which has already done a lot of this work for you. For example, https://github.com/tighten/solana-php-sdk#transactions provides an easy API to do exactly what you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM