MetacoSA / NBitcoin

Comprehensive Bitcoin library for the .NET framework.
MIT License
1.86k stars 840 forks source link

Need some code example on how to use RPC client on Bitcoin Test net. #1163

Closed zydjohnHotmail closed 1 year ago

zydjohnHotmail commented 1 year ago

Hello: I want to use RPC Client to retrieve account balance for one test net account. I have added some funds using faucet, I can use Bitcoin Core program for Windows 10 to get the account balance, but I want to use RPC client to do the same job. However, I looked at your repo, I found it is very complicated, I can't find any code sample for my job. I saw there are a number of tests in the repo, but how I can write a simple C# code to find account balance for one test net account? Using curl, the command looks like this: curl --location --request POST 'bitcoin_testnet_rpc_url' \ --data-raw '{"jsonrpc": "2.0", "method": "getbalance", "params": [null, null, null, null], "id": "1"}' By the way, I don't know how to use RPC to set the default account address. Please also advise on this. Thanks,

mjlamb commented 1 year ago

C# rpc call using httpClient....

        var payload = new
        {
            jsonrpc = "2.0",
            method = "getbalance",
            id = "1",
            @params = new object[] { }
        };

        // Serialize the object to a JSON string
        string payloadJson = JsonConvert.SerializeObject(payload);

        // Set your IP and port here for bitcoin rpc, make sure you ip is whitelisted from the node
        var uriBuilder = new UriBuilder("http", "192.168.1.xxx", 8332);

        // Set your rpc username and password here, from the bitcoin config file
        var username = "usernameHere";
        var password = "passwordHere";

        string encodedCredentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"));

        var request = new HttpRequestMessage(HttpMethod.Post, uriBuilder.Uri);

        // add the json-rpc
        request.Content = new StringContent(payloadJson, Encoding.UTF8, "application/json");

        // add the auth basic header
        request.Headers.Add("Authorization", $"Basic {encodedCredentials}");

        using var httpClient = new HttpClient();
        var response = httpClient.SendAsync(request).Result;

        var responseJson = response.Content.ReadAsStringAsync().Result;
zydjohnHotmail commented 1 year ago

Hi, Thank you very much for your response. However, I want to know if you have any BTC Testnet public RPC endpoints, so I can do some testing. Using localhost for testing is not very useful. Thanks,

mjlamb commented 1 year ago

I asked GPT for you.....

Yes, there are several public RPC (Remote Procedure Call) Bitcoin nodes that you can use to make API calls. Here are a few examples:

Blockchain.info: This is a popular website that provides various Bitcoin-related services, including an API that allows you to make RPC calls to their Bitcoin node. You can find their API documentation here: https://www.blockchain.com/api/blockchain_api.

Bitcore: This is a full Bitcoin node and development platform that provides APIs for developers to build Bitcoin applications. You can find their documentation here: https://bitcore.io/api/.

Blockstream.info: This is an open-source block explorer that also provides a public Bitcoin node API. You can find their documentation here: https://github.com/Blockstream/esplora/blob/master/API.md.

BTC.com: This is a Bitcoin mining pool that also provides a public Bitcoin node API. You can find their documentation here: https://btc.com/api-doc.

zydjohnHotmail commented 1 year ago

Hi, Thanks for your quick answer. I just want to know what is your question when you ask ChatGPT to get the answer. Thanks,

mjlamb commented 1 year ago

"are there any public rpc bitcoin text node servers to call" I was meant to say TEST, not text... You can always run your node against testnet and use that, you said you run bitcoin core.

zydjohnHotmail commented 1 year ago

Hi, One final question: do you think ChatGPT can write the test code just like you did here? Thanks,

mjlamb commented 1 year ago

oh for sure, it is very good at it.

zydjohnHotmail commented 1 year ago

How do you describe the code to ChatGPT, so it can write the code like here?

mjlamb commented 1 year ago

I just said.... "please write an http rpc call to bitcoin rpc using httpclient" it makes a start, you still have to be able to spot issue, it not perfect.

zydjohnHotmail commented 1 year ago

Hi, Thanks for your advice, I think this kind of job still need human action. By the way, I raised yet another issue, you can take a look. But I can close this one, as I learned more than I expected, but I need some time to review it. Thanks again!