2022-04-01 12:32:56 +00:00
|
|
|
import assert from 'assert';
|
|
|
|
import axios from 'axios';
|
2022-04-04 07:05:16 +00:00
|
|
|
import graphqlClient from 'graphql.js'
|
2022-04-08 10:32:24 +00:00
|
|
|
import { get, set } from 'lodash'
|
|
|
|
import { generateEndpointAccount, generateEndpointBroadcast } from '@tharsis/provider';
|
2022-04-01 12:32:56 +00:00
|
|
|
|
2022-04-04 07:05:16 +00:00
|
|
|
import { Util } from './util';
|
|
|
|
|
2022-04-08 10:32:24 +00:00
|
|
|
const attributeField = `
|
|
|
|
attributes {
|
|
|
|
key
|
|
|
|
value {
|
|
|
|
null
|
|
|
|
int
|
|
|
|
float
|
|
|
|
string
|
|
|
|
boolean
|
|
|
|
json
|
|
|
|
reference {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const refsField = `
|
|
|
|
references {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const historyFields = `
|
|
|
|
history {
|
|
|
|
id
|
|
|
|
height
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2022-04-05 05:31:10 +00:00
|
|
|
const auctionFields = `
|
|
|
|
id
|
|
|
|
status
|
|
|
|
ownerAddress
|
|
|
|
createTime
|
|
|
|
commitsEndTime
|
|
|
|
revealsEndTime
|
|
|
|
commitFee {
|
|
|
|
type
|
|
|
|
quantity
|
|
|
|
}
|
|
|
|
revealFee {
|
|
|
|
type
|
|
|
|
quantity
|
|
|
|
}
|
|
|
|
minimumBid {
|
|
|
|
type
|
|
|
|
quantity
|
|
|
|
}
|
|
|
|
winnerAddress
|
|
|
|
winnerBid {
|
|
|
|
type
|
|
|
|
quantity
|
|
|
|
}
|
|
|
|
winnerPrice {
|
|
|
|
type
|
|
|
|
quantity
|
|
|
|
}
|
|
|
|
bids {
|
|
|
|
bidderAddress
|
|
|
|
status
|
|
|
|
commitHash
|
|
|
|
commitTime
|
|
|
|
revealTime
|
|
|
|
commitFee {
|
|
|
|
type
|
|
|
|
quantity
|
|
|
|
}
|
|
|
|
revealFee {
|
|
|
|
type
|
|
|
|
quantity
|
|
|
|
}
|
|
|
|
bidAmount {
|
|
|
|
type
|
|
|
|
quantity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
/**
|
|
|
|
* Registry
|
|
|
|
*/
|
|
|
|
export class RegistryClient {
|
2022-04-04 07:05:16 +00:00
|
|
|
_restEndpoint: string
|
|
|
|
_graph: any
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get query result.
|
|
|
|
*/
|
2022-04-08 10:32:24 +00:00
|
|
|
static async getResult(query: any, key: string, modifier?: (rows: any[]) => {}) {
|
2022-04-04 07:05:16 +00:00
|
|
|
const result = await query;
|
|
|
|
if (result && result[key] && result[key].length && result[key][0] !== null) {
|
|
|
|
if (modifier) {
|
|
|
|
return modifier(result[key]);
|
|
|
|
}
|
|
|
|
return result[key];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
2022-04-01 12:32:56 +00:00
|
|
|
|
2022-04-08 10:32:24 +00:00
|
|
|
/**
|
|
|
|
* Prepare response attributes.
|
|
|
|
*/
|
|
|
|
static prepareAttributes(path: string) {
|
|
|
|
return (rows: any[]) => {
|
|
|
|
const result = rows.map(r => {
|
|
|
|
set(r, path, Util.fromGQLAttributes(get(r, path)));
|
|
|
|
return r;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
/**
|
|
|
|
* New Client.
|
|
|
|
*/
|
2022-12-09 08:55:13 +00:00
|
|
|
constructor(gqlEndpoint: string, restEndpoint: string) {
|
|
|
|
assert(gqlEndpoint);
|
2022-04-04 07:05:16 +00:00
|
|
|
this._graph = graphqlClient(gqlEndpoint, {
|
|
|
|
method: 'POST',
|
|
|
|
asJSON: true
|
|
|
|
});
|
2022-12-09 08:55:13 +00:00
|
|
|
|
|
|
|
this._restEndpoint = restEndpoint;
|
2022-04-01 12:32:56 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 09:42:46 +00:00
|
|
|
/**
|
|
|
|
* Get server status.
|
|
|
|
*/
|
|
|
|
async getStatus() {
|
|
|
|
const query = `query {
|
|
|
|
getStatus {
|
|
|
|
version
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
network
|
|
|
|
moniker
|
|
|
|
}
|
|
|
|
sync {
|
|
|
|
latest_block_hash
|
|
|
|
latest_block_height
|
|
|
|
latest_block_time
|
|
|
|
catching_up
|
|
|
|
}
|
|
|
|
validator {
|
|
|
|
address
|
|
|
|
voting_power
|
|
|
|
}
|
|
|
|
validators {
|
|
|
|
address
|
|
|
|
voting_power
|
|
|
|
proposer_priority
|
|
|
|
}
|
|
|
|
num_peers
|
|
|
|
peers {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
network
|
|
|
|
moniker
|
|
|
|
}
|
|
|
|
is_outbound
|
|
|
|
remote_ip
|
|
|
|
}
|
|
|
|
disk_usage
|
|
|
|
}
|
|
|
|
}`;
|
|
|
|
|
|
|
|
const { getStatus: status } = await this._graph(query)();
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2022-04-22 10:15:47 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
/**
|
2022-04-22 10:15:47 +00:00
|
|
|
* Fetch Accounts.
|
2022-04-01 12:32:56 +00:00
|
|
|
*/
|
2022-04-22 10:15:47 +00:00
|
|
|
async getAccounts(addresses: string[]) {
|
|
|
|
assert(addresses);
|
|
|
|
assert(addresses.length);
|
|
|
|
|
|
|
|
const query = `query ($addresses: [String!]) {
|
|
|
|
getAccounts(addresses: $addresses) {
|
|
|
|
address
|
|
|
|
pubKey
|
|
|
|
number
|
|
|
|
sequence
|
|
|
|
balance {
|
|
|
|
type
|
|
|
|
quantity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`;
|
2022-04-01 12:32:56 +00:00
|
|
|
|
2022-04-22 10:15:47 +00:00
|
|
|
const variables = {
|
|
|
|
addresses
|
|
|
|
};
|
2022-04-01 12:32:56 +00:00
|
|
|
|
2022-04-22 10:15:47 +00:00
|
|
|
return RegistryClient.getResult(this._graph(query)(variables), 'getAccounts');
|
2022-04-01 12:32:56 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 09:42:46 +00:00
|
|
|
/**
|
|
|
|
* Get records by ids.
|
|
|
|
*/
|
|
|
|
async getRecordsByIds(ids: string[], refs = false) {
|
|
|
|
assert(ids);
|
|
|
|
assert(ids.length);
|
|
|
|
|
|
|
|
const query = `query ($ids: [String!]) {
|
|
|
|
getRecordsByIds(ids: $ids) {
|
|
|
|
id
|
|
|
|
names
|
|
|
|
owners
|
|
|
|
bondId
|
|
|
|
createTime
|
|
|
|
expiryTime
|
|
|
|
${attributeField}
|
|
|
|
${refs ? refsField : ''}
|
|
|
|
}
|
|
|
|
}`;
|
|
|
|
|
|
|
|
const variables = {
|
|
|
|
ids
|
|
|
|
};
|
|
|
|
|
|
|
|
return RegistryClient.getResult(this._graph(query)(variables), 'getRecordsByIds', RegistryClient.prepareAttributes('attributes'));
|
|
|
|
}
|
|
|
|
|
2022-04-08 10:32:24 +00:00
|
|
|
/**
|
|
|
|
* Get records by attributes.
|
|
|
|
*/
|
2022-04-12 06:08:16 +00:00
|
|
|
async queryRecords(attributes: {[key: string]: any}, all = false, refs = false) {
|
2022-04-08 10:32:24 +00:00
|
|
|
if (!attributes) {
|
|
|
|
attributes = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const query = `query ($attributes: [KeyValueInput!], $all: Boolean) {
|
|
|
|
queryRecords(attributes: $attributes, all: $all) {
|
|
|
|
id
|
|
|
|
names
|
|
|
|
owners
|
|
|
|
bondId
|
|
|
|
createTime
|
|
|
|
expiryTime
|
|
|
|
${attributeField}
|
|
|
|
${refs ? refsField : ''}
|
|
|
|
}
|
|
|
|
}`;
|
|
|
|
|
|
|
|
const variables = {
|
|
|
|
attributes: Util.toGQLAttributes(attributes),
|
|
|
|
all
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = (await this._graph(query)(variables))['queryRecords'];
|
|
|
|
result = RegistryClient.prepareAttributes('attributes')(result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:31:10 +00:00
|
|
|
/**
|
|
|
|
* Lookup authorities by names.
|
|
|
|
*/
|
2022-04-12 06:08:16 +00:00
|
|
|
async lookupAuthorities(names: string[], auction = false) {
|
2022-04-05 05:31:10 +00:00
|
|
|
assert(names.length);
|
|
|
|
|
|
|
|
const query = `query ($names: [String!]) {
|
|
|
|
lookupAuthorities(names: $names) {
|
|
|
|
ownerAddress
|
|
|
|
ownerPublicKey
|
|
|
|
height
|
|
|
|
status
|
|
|
|
bondId
|
|
|
|
expiryTime
|
|
|
|
${auction ? ('auction { ' + auctionFields + ' }') : ''}
|
|
|
|
}
|
|
|
|
}`;
|
|
|
|
|
|
|
|
const variables = {
|
|
|
|
names
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = await this._graph(query)(variables);
|
|
|
|
|
|
|
|
return result['lookupAuthorities'];
|
|
|
|
}
|
|
|
|
|
2022-04-12 06:08:16 +00:00
|
|
|
/**
|
|
|
|
* Get auctions by ids.
|
|
|
|
*/
|
|
|
|
async getAuctionsByIds(ids: string[]) {
|
|
|
|
assert(ids);
|
|
|
|
assert(ids.length);
|
|
|
|
|
|
|
|
const query = `query ($ids: [String!]) {
|
|
|
|
getAuctionsByIds(ids: $ids) {
|
|
|
|
${auctionFields}
|
|
|
|
}
|
|
|
|
}`;
|
|
|
|
|
|
|
|
const variables = {
|
|
|
|
ids
|
|
|
|
};
|
|
|
|
|
|
|
|
return RegistryClient.getResult(this._graph(query)(variables), 'getAuctionsByIds');
|
|
|
|
}
|
|
|
|
|
2022-04-08 10:32:24 +00:00
|
|
|
/**
|
|
|
|
* Lookup names.
|
|
|
|
*/
|
|
|
|
async lookupNames(names: string[], history = false) {
|
|
|
|
assert(names.length);
|
|
|
|
|
|
|
|
const query = `query ($names: [String!]) {
|
|
|
|
lookupNames(names: $names) {
|
|
|
|
latest {
|
|
|
|
id
|
|
|
|
height
|
|
|
|
}
|
|
|
|
${history ? historyFields : ''}
|
|
|
|
}
|
|
|
|
}`;
|
|
|
|
|
|
|
|
const variables = {
|
|
|
|
names
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = await this._graph(query)(variables);
|
|
|
|
|
|
|
|
return result['lookupNames'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve names to records.
|
|
|
|
*/
|
|
|
|
async resolveNames(names: string[], refs = false) {
|
|
|
|
assert(names.length);
|
|
|
|
|
|
|
|
const query = `query ($names: [String!]) {
|
|
|
|
resolveNames(names: $names) {
|
|
|
|
id
|
|
|
|
names
|
|
|
|
owners
|
|
|
|
bondId
|
|
|
|
createTime
|
|
|
|
expiryTime
|
|
|
|
${attributeField}
|
|
|
|
${refs ? refsField : ''}
|
|
|
|
}
|
|
|
|
}`;
|
|
|
|
|
|
|
|
const variables = {
|
|
|
|
names
|
|
|
|
};
|
|
|
|
|
2022-04-08 12:32:47 +00:00
|
|
|
let result = (await this._graph(query)(variables))['resolveNames'];
|
|
|
|
result = RegistryClient.prepareAttributes('attributes')(result);
|
2022-04-08 10:32:24 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-04-04 07:05:16 +00:00
|
|
|
/**
|
|
|
|
* Get bonds by ids.
|
|
|
|
*/
|
|
|
|
async getBondsByIds(ids: string[]) {
|
|
|
|
assert(ids);
|
|
|
|
assert(ids.length);
|
|
|
|
|
|
|
|
const query = `query ($ids: [String!]) {
|
|
|
|
getBondsByIds(ids: $ids) {
|
|
|
|
id
|
|
|
|
owner
|
|
|
|
balance {
|
|
|
|
type
|
|
|
|
quantity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`;
|
|
|
|
|
|
|
|
const variables = {
|
|
|
|
ids
|
|
|
|
};
|
|
|
|
|
|
|
|
return RegistryClient.getResult(this._graph(query)(variables), 'getBondsByIds');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get records by attributes.
|
|
|
|
*/
|
|
|
|
async queryBonds(attributes = {}) {
|
|
|
|
const query = `query ($attributes: [KeyValueInput!]) {
|
|
|
|
queryBonds(attributes: $attributes) {
|
|
|
|
id
|
|
|
|
owner
|
|
|
|
balance {
|
|
|
|
type
|
|
|
|
quantity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`;
|
|
|
|
|
|
|
|
const variables = {
|
|
|
|
attributes: Util.toGQLAttributes(attributes)
|
|
|
|
};
|
|
|
|
|
|
|
|
return RegistryClient.getResult(this._graph(query)(variables), 'queryBonds');
|
|
|
|
}
|
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
/**
|
|
|
|
* Submit transaction.
|
|
|
|
*/
|
|
|
|
async submit(tx: string) {
|
|
|
|
assert(tx);
|
|
|
|
|
|
|
|
// Broadcast transaction.
|
|
|
|
const { data } = await axios.post(
|
2022-04-04 07:05:16 +00:00
|
|
|
`${this._restEndpoint}${generateEndpointBroadcast()}`,
|
2022-04-01 12:32:56 +00:00
|
|
|
tx
|
|
|
|
)
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|