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-01 12:32:56 +00:00
|
|
|
import { generateEndpointAccount, generateEndpointBroadcast, generatePostBodyBroadcast } from '@tharsis/provider';
|
|
|
|
|
2022-04-04 07:05:16 +00:00
|
|
|
import { Util } from './util';
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static async getResult(query: any, key: string, modifier?: (rows: any[]) => {}) {
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* New Client.
|
|
|
|
*/
|
2022-04-04 07:05:16 +00:00
|
|
|
constructor(restEndpoint: string, gqlEndpoint: string) {
|
|
|
|
assert(restEndpoint);
|
2022-04-01 12:32:56 +00:00
|
|
|
|
2022-04-04 07:05:16 +00:00
|
|
|
this._restEndpoint = restEndpoint;
|
|
|
|
|
|
|
|
this._graph = graphqlClient(gqlEndpoint, {
|
|
|
|
method: 'POST',
|
|
|
|
asJSON: true
|
|
|
|
});
|
2022-04-01 12:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch Account.
|
|
|
|
*/
|
|
|
|
async getAccount(address: string) {
|
|
|
|
assert(address);
|
|
|
|
|
2022-04-04 07:05:16 +00:00
|
|
|
let { data } = await axios.get(`${this._restEndpoint}${generateEndpointAccount(address)}`)
|
2022-04-01 12:32:56 +00:00
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:31:10 +00:00
|
|
|
/**
|
|
|
|
* Lookup authorities by names.
|
|
|
|
*/
|
|
|
|
async lookupAuthorities(names: string[], auction = false) {
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
}
|