Sending money to an address
From SolidCoin Wiki
Contents |
PHP
function sendpayout( $txto, $amount, $receiver ) {
$host = "backend.host.com";
$port = 6000;
// create Auth string for HTTP Authentication with solidcoind
$token = base64_encode("rpc_user:rpc_pass");
// prepare json request. Make sure TXTO, Amount and Receiver are sanitized BEFORE passing them on to
// this function to avoid being robbed by code injections - stripslashes is a good start here
$content =
'{
"jsonrpc": "1.0",
"id":"curltest",
"method": "sendtoaddress",
"params": ["'
. $txto . '", '
. $amount
. ', "Payout to '
. $receiver
. '","Payout to '
. $receiver . '"] }';
// Get the length of the json request - needed for proper HTTP/1.1 POST headers
$len = strlen($content);
// Open a connection to solidcoind.
$x = fsockopen( $host, $port);
// send HTTP post headers
fputs( $x, "POST / HTTP/1.1\r\n" );
fputs( $x, "Connection: close\r\n" );
fputs( $x, "Host: $host\r\n" );
fputs( $x, "Authorization: Basic $token\r\n" );
fputs( $x, "Content-type: text/plain\r\n" );
fputs( $x, "Content-Length: $len\r\n\r\n$content" );
// fetch and dispose of the HTTP Response headers. We don't need them, but fetching errors could be done in here
while ( !feof($x) ) { $z = fgets($x); if ( $z == "\r\n" ) break; }
// finally fetch the json response. this should always be one line. Note I set the receive buffer
// to 4096, which should always be enough. 1024 is probably way more than what could ever come back, so if you need to
// save memory, decrease it.
$y = fgets( $x, 4096 );
// hit the response json string. Now that I read that code, it'd be probably better to simply use json_decode,
// instead of manually fetching it out.
$y = str_replace( '{"result":', "", $y );
$response = substr( $y, 0, strpos( $y, "," ) );
$response = str_replace( '"', "", $response );
// we fetched the txid out of the json return. We send it as the function return value
return $response;
}
This is a snippet from the Mine-For pool graciously provided by the admin, BrightSky. Some additional help from pho in #solidcoin on FreeNode
- txto = solidcoin address
- amount is the correct amount. Note that you can use sc_sendtoaddress, which allows 4 decimals shifted left by 4 as it uses an intvalue
- receiver is a name, that one is just for making a comment to the transaction in your wallet file
- I think the GUI client disregards those comments
Please note that this function does minimal error checking. It also depends on it's inputs being sane. There are some possible simplifications, like using curl or something to replace the manual HTTP-Call I did here, or using json-decode to get the return value.
A perl version
sendtoaddress()
use strict;
use warnings;
use Mojo::Util;
my $host = 'host.com';
my $port = 8555;
my $auth = "user:pass";
my $to = 'sRpk3Qv6s5G4UQPVDucPrhHc5ZVFdYMhLJ';
my $amount = 0.567;
my $json = {
jsonrpc => '1.0',
id => 'curltest',
method => 'sendtoaddress',
params => [ $to, $amount, "Payout to bob", "Payout to john" ]
};
use Mojo::JSON;
$json = Mojo::JSON->new->encode($json);
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
my $tx =
$ua->post( "http://$auth" . '@'
. "$host:$port" => { Connection => 'close' } => $json );
warn $tx->res->body;
=pod
This is an example of how to write a client to
send a command to an rpc server
That server needs to have an rpcallowip in its solidcoin.conf that allows
this code to make a json-rpc connection to it.
=cut
getinfo()
use strict;
use warnings;
use Mojo::Util;
my $host = 'host.com';
my $port = 8555;
my $auth = "user:pass";
my $json = {
jsonrpc => '1.0',
id => 'curltest',
method => 'getinfo'
};
use Mojo::JSON;
$json = Mojo::JSON->new->encode($json);
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
my $tx = $ua->post(
"http://$auth" . '@' . "$host:$port" => {Connection => 'close'} => $json
);
warn $tx->res->body;
=pod
This is an example of how to write a client to
send a command to an rpc server
That server needs to have an rpcallowip in its solidcoin.conf that allows
this code to make a json-rpc connection to it.
=cut