@swaptoshi/governance-client
Version:
Client for interacting with blockchain networks that implements the Swaptoshi governance module
69 lines • 3.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GovernanceTransactionClient = void 0;
const client_1 = require("@klayr/client");
const constant_1 = require("./constant");
class GovernanceTransactionClient {
constructor(url, mode) {
this._url = url;
this._mode = mode;
}
async boostVote(params, senderPrivateKey) {
const client = await this._getClient();
const result = await this._sendTransaction(client, 'boostVote', params, senderPrivateKey);
await client.disconnect();
return result;
}
async createProposal(params, senderPrivateKey) {
const client = await this._getClient();
const result = await this._sendTransaction(client, 'createProposal', params, senderPrivateKey);
await client.disconnect();
return result;
}
async delegateVote(params, senderPrivateKey) {
const client = await this._getClient();
const result = await this._sendTransaction(client, 'delegateVote', params, senderPrivateKey);
await client.disconnect();
return result;
}
async revokeDelegatedVote(params, senderPrivateKey) {
const client = await this._getClient();
const result = await this._sendTransaction(client, 'createProposal', params, senderPrivateKey);
await client.disconnect();
return result;
}
async setProposalAttributes(params, senderPrivateKey) {
const client = await this._getClient();
const result = await this._sendTransaction(client, 'setProposalAttributes', params, senderPrivateKey);
await client.disconnect();
return result;
}
async vote(params, senderPrivateKey) {
const client = await this._getClient();
const result = await this._sendTransaction(client, 'vote', params, senderPrivateKey);
await client.disconnect();
return result;
}
async _sendTransaction(client, command, params, senderPrivateKey) {
const senderPublicKey = client_1.cryptography.ed.getPublicKeyFromPrivateKey(senderPrivateKey);
const { nonce } = (await client.invoke('auth_getAuthAccount', {
address: client_1.cryptography.address.getKlayr32AddressFromPublicKey(senderPublicKey),
}));
const payload = { module: 'governance', command, nonce, params: params.params };
let transaction = await client.transaction.create({ ...payload, fee: constant_1.INITIAL_MOCK_FEE }, senderPrivateKey.toString('hex'));
const fee = params.fee && params.fee > BigInt(0) ? params.fee : client.transaction.computeMinFee(transaction);
transaction = await client.transaction.create({ ...payload, fee }, senderPrivateKey.toString('hex'));
return client.transaction.send(transaction);
}
async _getClient() {
if (this._mode === 'ipc') {
return client_1.apiClient.createIPCClient(this._url);
}
if (this._mode === 'ws') {
return client_1.apiClient.createWSClient(this._url);
}
throw new Error('unknown mode');
}
}
exports.GovernanceTransactionClient = GovernanceTransactionClient;
//# sourceMappingURL=transaction.js.map