-
Notifications
You must be signed in to change notification settings - Fork 837
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Blockr + RPC Transaction repository implementation + optimizations
- Loading branch information
1 parent
268814c
commit 9a5e496
Showing
6 changed files
with
134 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace NBitcoin | ||
{ | ||
[Serializable] | ||
public class BlockrException : Exception | ||
{ | ||
public BlockrException(JObject response) | ||
: base(response["message"] == null ? "Error from Blockr" : response["message"].ToString()) | ||
{ | ||
Code = response["code"] == null ? 0 : response["code"].Value<int>(); | ||
Status = response["status"] == null ? null : response["status"].ToString(); | ||
} | ||
|
||
public int Code | ||
{ | ||
get; | ||
set; | ||
} | ||
public string Status | ||
{ | ||
get; | ||
set; | ||
} | ||
} | ||
public class BlockrTransactionRepository : ITransactionRepository | ||
{ | ||
public BlockrTransactionRepository() | ||
{ | ||
|
||
} | ||
|
||
|
||
|
||
#region ITransactionRepository Members | ||
|
||
public Transaction Get(uint256 txId) | ||
{ | ||
while(true) | ||
{ | ||
WebClient client = new WebClient(); | ||
var result = client.DownloadString("http://btc.blockr.io/api/v1/tx/raw/" + txId); | ||
var json = JObject.Parse(result); | ||
var status = json["status"]; | ||
var code = json["code"]; | ||
if(status != null && status.ToString() == "error") | ||
{ | ||
throw new BlockrException(json); | ||
} | ||
var tx = new Transaction(json["data"]["tx"]["hex"].ToString()); | ||
return tx; | ||
} | ||
} | ||
|
||
public void Put(uint256 txId, Transaction tx) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using NBitcoin.RPC; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NBitcoin | ||
{ | ||
public class RPCTransactionRepository : ITransactionRepository | ||
{ | ||
RPCClient _Client; | ||
public RPCTransactionRepository(RPCClient client) | ||
{ | ||
if(client == null) | ||
throw new ArgumentNullException("client"); | ||
_Client = client; | ||
} | ||
#region ITransactionRepository Members | ||
|
||
public Transaction Get(uint256 txId) | ||
{ | ||
return _Client.GetRawTransaction(txId, false); | ||
} | ||
|
||
public void Put(uint256 txId, Transaction tx) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
#endregion | ||
} | ||
} |