-
Notifications
You must be signed in to change notification settings - Fork 0
/
MastercoinAPI.groovy
executable file
·113 lines (88 loc) · 3.57 KB
/
MastercoinAPI.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* Created by whoisjeremylam on 18/04/14.
* All amounts are in floating point, not willets
*/
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
import groovyx.net.http.AsyncHTTPBuilder
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
class MastercoinAPI {
private String mastercoinTransactionEncoding
private String mastercoinRpcURL
private String mastercoinRpcUser
private String mastercoinRpcPassword
private groovyx.net.http.AsyncHTTPBuilder mastercoinHttpAsync
private boolean mastercoinMultisendPerBlock
private log4j
private init() {
// Read in ini file
def iniConfig = new ConfigSlurper().parse(new File("MastercoinAPI.ini").toURL())
mastercoinRpcURL = iniConfig.mastercoin.rpcURL
mastercoinRpcUser = iniConfig.mastercoin.rpcUser
mastercoinRpcPassword = iniConfig.mastercoin.rpcPassword
mastercoinHttpAsync = new AsyncHTTPBuilder(
poolSize : 10,
uri : mastercoinRpcURL,
contentType : JSON )
mastercoinHttpAsync.auth.basic mastercoinRpcUser, mastercoinRpcPassword
}
private sendRPCMessage(command, in_params) {
def paramsJSON
def result = mastercoinHttpAsync.request( POST, JSON) { req ->
body = [method : command,
id : 'test',
params : in_params,
jsonrpc : "2.0"
]
paramsJSON = new groovy.json.JsonBuilder(body)
log4j.info(command + " payload: " + paramsJSON)
response.success = { resp, json ->
if (json.containsKey("error") && json.error != null) {
log4j.info(command + " error: " + json.error)
return json.error
}
return json.result
}
response.failure = { resp ->
log4j.info(command + " failed")
assert resp.responseBase == null
}
}
assert result instanceof java.util.concurrent.Future
while ( ! result.done ) {
Thread.sleep(100)
}
return result.get()
}
public getBalances(address) {
return sendRPCMessage('getallbalancesforaddress_MP', [address])
}
public getAssetBalance(address, asset) {
return sendRPCMessage('getbalance_MP', [address, Long.parseLong(asset, 10)])
}
public getSends(Long blockId) {
return sendRPCMessage('listtransactions_MP', ["*", 10000, 0, blockId,blockId])
}
// Recall asset is integer in mastercoin (amount in floating point, not willet)
public sendAsset(sourceAddress, destinationAddress, asset, amount, testMode) {
def myParams
if (testMode == false) {
myParams = [sourceAddress, destinationAddress, Long.parseLong(asset, 10), amount]
}
else {
myParams = ['12nY87y6qf4Efw5WZaTwgGeceXApRYAwC7', '142UYTzD1PLBcSsww7JxKLck871zRYG5D3', Long.parseLong(asset, 10), 20000]
}
return sendRPCMessage('send_MP', myParams)
}
// Amount is in floating point, not willets
public sendDividend(String sourceAddress, String dividend_asset, BigDecimal total_quantity) {
return sendRPCMessage('sendtoowners_MP',[sourceAddress, Long.parseLong(dividend_asset, 10), total_quantity])
}
public getAssetInfo(asset) {
return sendRPCMessage('getproperty_MP',[Long.parseLong(asset, 10)])
}
public MastercoinAPI(logger) {
init()
log4j = logger
}
}