Skip to content

Instantly share code, notes, and snippets.

@jhodges10
Last active September 5, 2018 23:40
Show Gist options
  • Save jhodges10/da088d60b9e27892eeb79aa1b6dbb7e2 to your computer and use it in GitHub Desktop.
Save jhodges10/da088d60b9e27892eeb79aa1b6dbb7e2 to your computer and use it in GitHub Desktop.
Figuring out type errors with gobjectprepare
import * as Req from 'request-promise-lite';
import * as DashD from './RPCDefinitions';
/**
* Each call returns this
*/
export interface CallResult<A> {
readonly result?: A;
readonly error?: any;
readonly id: number;
}
/**
* Configurations for instantiating DarkcoinClient
*/
export interface DashdConfig {
readonly url: string;
readonly user: string;
readonly password: string;
}
/**
* Client instance for doing RPC calls on Dashd
*/
export default class DarkcoinClient {
public readonly config: DashdConfig;
constructor(dashdConfig: DashdConfig) {
this.config = dashdConfig;
}
/**
* This methods allows to call any of the RPC methods provided by dashd directly.
* Call it with 'help' for the full list of commands.
* @param method name of the method to call, e.g getblockhash
* @param params params of the method, e.g. height for getblockhash
* @param callId id to associate with call
*/
public callRPCMethod<T>(
method: string,
params: ReadonlyArray<any>,
callId?: number
): Promise<CallResult<T>> {
const id: number = callId ? callId : Math.floor(Math.random() * 100000);
const auth = { user: this.config.user, password: this.config.password };
const body = { method, params, id };
const req = new Req.Request('POST', this.config.url, {
json: true,
body,
auth
});
return req.run().then(response => {
return (response as any) as CallResult<T>;
});
}
// Wallet Methods
/**
* Mark in-wallet transaction <txid> as abandoned
* This will mark this transaction and all its in-wallet descendants as abandoned which will allow
* for their inputs to be respent. It can be used to replace "stuck" or evicted transactions.
* It only works on transactions which are not included in a block and are not currently in the mempool.
* It has no effect on transactions which are already conflicted or abandoned.
*/
public abandonTransaction(txId: string): Promise<CallResult<null>> {
return this.callRPCMethod<null>('abandontransaction', [txId]);
}
/**
* Returns an object containing various wallet state info.
*/
public getWalletInfo(): Promise<CallResult<DashD.WalletInfo>> {
return this.callRPCMethod<DashD.WalletInfo>('getwalletinfo', []);
}
/**
* Returns a new Dash address for receiving payments.
*/
public getNewAddress(): Promise<CallResult<string>> {
return this.callRPCMethod<string>('getnewaddress', []);
}
/**
* Send an amount to a given address.
* Returns transaction id.
*/
public sendToAddress(
dest: string,
amount: number,
comment?: string,
commentTo?: string,
subtractFeeFromAmount?: boolean,
useIS?: boolean,
usePS?: boolean
): Promise<CallResult<string>> {
const params: ReadonlyArray<any> = [
dest,
amount,
comment,
commentTo,
subtractFeeFromAmount,
useIS,
usePS
];
return this.filterUndefined(arguments, params).then(filteredParams =>
this.callRPCMethod<string>('sendtoaddress', filteredParams)
);
}
/**
* Set the transaction fee per kB. Overwrites the paytxfee parameter.
* @param amount The transaction fee in DASH/kB
*/
public setTxFee(amount: number): Promise<CallResult<boolean>> {
return this.callRPCMethod<boolean>('settxfee', [amount]);
}
/**
* Sign a message with the private key of an address
* @param address The dash address to use for the private key.
* @param message The message to create a signature of.
* @returns The signature of the message encoded in base 64
*/
public signMessage(address: string, message: string): Promise<CallResult<string>> {
return this.callRPCMethod<string>('signmessage', [address, message]);
}
// Masternodes
/**
* Returns key/value dictionary pairs for all masternodes.
*/
public masternodeList(): Promise<CallResult<DashD.MasterNodeList>> {
return this.callRPCMethod<DashD.MasterNodeList>('masternodelist', []);
}
// GObjects
/**
* Prepare GObject
*/
public gobjectPrepare(parentHash: string, revision: number, creationTime: number, gobjectData: string): Promise<CallResult<string>> {
return this.callRPCMethod<string>('gobject', ['prepare', parentHash, revision, creationTime, gobjectData]);
}
/**
* Submit GObject
*/
public gobjectSubmit(parentHash: string, revision: number, creationTime: number, gobjectData: string, txId: string): Promise<CallResult<string>> {
return this.callRPCMethod<string>('gobject', ['submit', parentHash, revision, creationTime, gobjectData, txId]);
}
/**
* Returns key/value pairs for all current GObjects with the key. Will include both funding gobjects and trigger gobjects,
* Make sure to parse them and pull out the ones you want.
*/
public gobjectList(): Promise<CallResult<DashD.GObjectList>> {
return this.callRPCMethod<DashD.GObjectList>('gobject', ['list']);
}
public gobjectCurrentVotes(
hash: string
): Promise<CallResult<DashD.GObjectCurrentVotesList>> {
return this.callRPCMethod<DashD.GObjectCurrentVotesList>('gobject', ['getcurrentvotes', hash]);
}
// Network Information
/**
* The getnetworkinfo RPC returns information about the node’s connection to the network.
*/
public getNetworkInfo(): Promise<CallResult<DashD.NetworkInfo>> {
return this.callRPCMethod<DashD.NetworkInfo>('getnetworkinfo', []);
}
/**
* Returns network related governance info, i.e. superblock height, proposal fee, and minquorum.
*/
public getGovernanceInfo(): Promise<CallResult<DashD.GovernanceInfo>> {
return this.callRPCMethod<DashD.GovernanceInfo>('getgovernanceinfo', []);
}
/**
* Returns mining related info, i.e. difficulty, blocksize, currentblocktx, and a network hash rate estimate.
*/
public getMiningInfo(): Promise<CallResult<DashD.MiningInfo>> {
return this.callRPCMethod<DashD.MiningInfo>('getmininginfo', []);
}
// Private Methods
/**
* Build the parameter list by removing optional arguments
* @param originalArgs original argument list
* @param params
*/
private filterUndefined<T>(
originalArgs: IArguments,
params: ReadonlyArray<T>
): Promise<ReadonlyArray<T>> {
const undefinedIndex = params.findIndex(v => v === undefined);
if (undefinedIndex >= 0 && undefinedIndex < originalArgs.length - 1) {
return Promise.reject(
new Error('Undefined arguments found after defined arguments.')
);
}
return Promise.resolve(params.filter(v => v !== undefined));
}
}
"use strict";
var DarkcoinClient = require("darkcoin-client").default;
var config = {
url: 'http://localhost:19998',
user: "admin",
password: "password"
}
var client = new DarkcoinClient(config)
/*
client.masternodeList().then((res) => {
console.log(res.result)
})
client.gobjectList().then((res) => {
console.log(res.result)
})
client.masternodeList().then((res) => {
console.log(res.result)
})
client.getGovernanceInfo().then((res) => {
console.log(res.result)
})
client.getMiningInfo().then((res) => {
console.log(res.result)
})
client.getNetworkInfo().then((res) => {
console.log(res.result)
})
client.gobjectCurrentVotes("e77f59d842e9c9a76c4e62e5823cdd2396cd639be9b317c20962ae627ced43f0").then((res) => {
console.log(res.result)
})
client.getWalletInfo().then((res) => {
console.log("balance is " + res.result.balance);
console.log(res)
}).catch((e) => {
console.log(e)
})
client.sendToAddress("XbxkhvUL7KLF7633J7nySr3WFV6XnLGTf6", .0001, "", "", true, false, false).((res) =>
console.log(res)
}).catch((e) => {
console.log(e)
)
client.getNetworkInfo().then((res) => {
console.log(res.result)
})
client.getGovernanceInfo().then((res) => {
console.log(res.result)
})
*/
client.gobjectPrepare(0, 1, 1536125535, '5b5b2270726f706f73616c222c7b22656e645f65706f6368223a313533363133313635392c226e616d65223a226e657875735f746573745f31222c227061796d656e745f61646472657373223a227961597648465464434e6e575a365776564b684b646e315132796b4b465456557146222c227061796d656e745f616d6f756e74223a352c2273746172745f65706f6368223a313533363132383039342c2274797065223a312c2275726c223a2268747470733a2f2f7777772e646173686e657875732e6f7267227d5d5d').then((res) => {
console.log(res.result);
}).catch((e) => {
console.log(e)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment