2022-04-01 12:32:56 +00:00
|
|
|
import { sha256 } from 'js-sha256';
|
2022-04-05 14:11:06 +00:00
|
|
|
import { generatePostBodyBroadcast, BroadcastMode } from '@tharsis/provider';
|
2022-03-30 05:21:03 +00:00
|
|
|
import {
|
|
|
|
Chain,
|
2022-03-31 10:00:02 +00:00
|
|
|
Sender,
|
2022-04-01 12:32:56 +00:00
|
|
|
Fee,
|
2022-04-05 09:20:12 +00:00
|
|
|
MessageSendParams
|
2024-03-07 04:17:05 +00:00
|
|
|
} from '@tharsis/transactions';
|
2024-03-11 08:51:00 +00:00
|
|
|
import { DeliverTxResponse, StdFee } from '@cosmjs/stargate';
|
2022-03-24 06:40:03 +00:00
|
|
|
|
2024-03-07 04:17:05 +00:00
|
|
|
import { RegistryClient } from './registry-client';
|
|
|
|
import { Account } from './account';
|
|
|
|
import { createTransaction } from './txbuilder';
|
2022-04-12 06:08:16 +00:00
|
|
|
import { Util } from './util';
|
|
|
|
import {
|
2022-04-27 10:00:21 +00:00
|
|
|
createTxMsgAssociateBond,
|
|
|
|
createTxMsgDissociateBond,
|
|
|
|
createTxMsgDissociateRecords,
|
|
|
|
createTxMsgReAssociateRecords,
|
|
|
|
MessageMsgAssociateBond,
|
2022-04-12 06:08:16 +00:00
|
|
|
MessageMsgCancelBond,
|
|
|
|
MessageMsgCreateBond,
|
2022-04-27 10:00:21 +00:00
|
|
|
MessageMsgDissociateBond,
|
|
|
|
MessageMsgDissociateRecords,
|
|
|
|
MessageMsgReAssociateRecords,
|
2022-04-12 06:08:16 +00:00
|
|
|
MessageMsgRefillBond,
|
|
|
|
MessageMsgWithdrawBond
|
2024-03-07 04:17:05 +00:00
|
|
|
} from './messages/bond';
|
2022-04-12 06:08:16 +00:00
|
|
|
import {
|
|
|
|
MessageMsgDeleteName,
|
|
|
|
MessageMsgSetAuthorityBond,
|
|
|
|
MessageMsgSetName,
|
2024-03-11 08:51:00 +00:00
|
|
|
NAMESERVICE_ERRORS
|
2022-12-19 06:14:18 +00:00
|
|
|
} from './messages/registry';
|
2022-04-12 06:08:16 +00:00
|
|
|
import {
|
|
|
|
MessageMsgCommitBid,
|
|
|
|
MessageMsgRevealBid
|
|
|
|
} from './messages/auction';
|
2024-03-06 14:18:45 +00:00
|
|
|
import { LaconicClient } from './laconic-client';
|
|
|
|
import { MsgCancelBondResponse, MsgCreateBondResponse, MsgRefillBondResponse, MsgWithdrawBondResponse } from './proto2/cerc/bond/v1/tx';
|
2024-03-07 10:36:52 +00:00
|
|
|
import { Coin } from './proto2/cosmos/base/v1beta1/coin';
|
2022-03-31 10:00:02 +00:00
|
|
|
|
2023-01-11 05:16:03 +00:00
|
|
|
export const DEFAULT_CHAIN_ID = 'laconic_9000-1';
|
2022-12-09 08:55:13 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
// Parse Tx response from cosmos-sdk.
|
2022-04-20 11:54:37 +00:00
|
|
|
export const parseTxResponse = (result: any, parseResponse?: (data: string) => any) => {
|
2022-04-01 12:32:56 +00:00
|
|
|
const { txhash: hash, height, ...txResponse } = result;
|
2022-04-20 11:54:37 +00:00
|
|
|
|
|
|
|
if (parseResponse) {
|
2024-03-07 04:17:05 +00:00
|
|
|
txResponse.data = parseResponse(txResponse.data);
|
2022-04-20 11:54:37 +00:00
|
|
|
}
|
2022-03-29 11:01:23 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
txResponse.events.forEach((event:any) => {
|
|
|
|
event.attributes = event.attributes.map(({ key, value }: { key: string, value: string }) => ({
|
|
|
|
key: Buffer.from(key, 'base64').toString('utf8'),
|
|
|
|
value: Buffer.from(value, 'base64').toString('utf8')
|
|
|
|
}));
|
|
|
|
});
|
2022-03-29 11:01:23 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
return { hash, height, ...txResponse };
|
|
|
|
};
|
2022-03-29 11:01:23 +00:00
|
|
|
|
2022-04-12 06:08:16 +00:00
|
|
|
/**
|
|
|
|
* Create an auction bid.
|
|
|
|
*/
|
2022-04-20 11:54:37 +00:00
|
|
|
export const createBid = async (chainId: string, auctionId: string, bidderAddress: string, bidAmount: string, noise?: string) => {
|
2022-04-12 06:08:16 +00:00
|
|
|
if (!noise) {
|
|
|
|
noise = Account.generateMnemonic();
|
|
|
|
}
|
|
|
|
|
|
|
|
const reveal = {
|
|
|
|
chainId,
|
|
|
|
auctionId,
|
|
|
|
bidderAddress,
|
|
|
|
bidAmount,
|
|
|
|
noise
|
|
|
|
};
|
|
|
|
|
|
|
|
const commitHash = await Util.getContentId(reveal);
|
|
|
|
const revealString = Buffer.from(JSON.stringify(reveal)).toString('hex');
|
|
|
|
|
|
|
|
return {
|
|
|
|
commitHash,
|
|
|
|
reveal,
|
|
|
|
revealString
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
export const isKeyValid = (key: string) => key && key.match(/^[0-9a-fA-F]{64}$/);
|
2022-03-24 06:40:03 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
export class Registry {
|
2024-03-07 04:17:05 +00:00
|
|
|
_endpoints: {[key: string]: string};
|
|
|
|
_chainID: string;
|
|
|
|
_chain: Chain;
|
|
|
|
_client: RegistryClient;
|
2022-03-24 06:40:03 +00:00
|
|
|
|
2024-03-07 04:17:05 +00:00
|
|
|
constructor (gqlUrl: string, restUrl = '', chainId: string = DEFAULT_CHAIN_ID) {
|
2022-04-25 09:42:46 +00:00
|
|
|
this._endpoints = {
|
|
|
|
rest: restUrl,
|
|
|
|
gql: gqlUrl
|
|
|
|
};
|
|
|
|
|
2022-12-09 08:55:13 +00:00
|
|
|
this._client = new RegistryClient(gqlUrl, restUrl);
|
2022-04-25 09:42:46 +00:00
|
|
|
this._chainID = chainId;
|
2022-03-30 05:21:03 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
this._chain = {
|
2022-04-18 11:27:37 +00:00
|
|
|
cosmosChainId: chainId,
|
|
|
|
chainId: this._parseEthChainId(chainId)
|
|
|
|
};
|
2022-03-30 05:21:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
/**
|
2022-04-22 10:15:47 +00:00
|
|
|
* Get accounts by addresses.
|
2022-04-01 12:32:56 +00:00
|
|
|
*/
|
2024-03-07 04:17:05 +00:00
|
|
|
async getAccounts (addresses: string[]) {
|
2022-04-22 10:15:47 +00:00
|
|
|
return this._client.getAccounts(addresses);
|
2022-03-30 05:21:03 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 04:17:05 +00:00
|
|
|
get endpoints () {
|
2022-04-25 09:42:46 +00:00
|
|
|
return this._endpoints;
|
|
|
|
}
|
|
|
|
|
2024-03-07 04:17:05 +00:00
|
|
|
get chainID () {
|
2022-04-25 09:42:46 +00:00
|
|
|
return this._chainID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get server status.
|
|
|
|
*/
|
2024-03-07 04:17:05 +00:00
|
|
|
async getStatus () {
|
2022-04-25 09:42:46 +00:00
|
|
|
return this._client.getStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get records by ids.
|
|
|
|
*/
|
2024-03-07 04:17:05 +00:00
|
|
|
async getRecordsByIds (ids: string[], refs = false) {
|
2022-04-25 09:42:46 +00:00
|
|
|
return this._client.getRecordsByIds(ids, refs);
|
|
|
|
}
|
|
|
|
|
2022-04-08 10:32:24 +00:00
|
|
|
/**
|
|
|
|
* Get records by attributes.
|
|
|
|
*/
|
2024-03-07 04:17:05 +00:00
|
|
|
async queryRecords (attributes: {[key: string]: any}, all = false, refs = false) {
|
2022-04-08 10:32:24 +00:00
|
|
|
return this._client.queryRecords(attributes, all, refs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve names to records.
|
|
|
|
*/
|
2024-03-07 04:17:05 +00:00
|
|
|
async resolveNames (names: string[], refs = false) {
|
2022-04-08 10:32:24 +00:00
|
|
|
return this._client.resolveNames(names, refs);
|
|
|
|
}
|
|
|
|
|
2022-04-08 05:29:03 +00:00
|
|
|
/**
|
2024-01-22 08:30:35 +00:00
|
|
|
* Publish record.
|
|
|
|
* @param transactionPrivateKey - private key in HEX to sign transaction.
|
|
|
|
*/
|
2024-03-07 04:17:05 +00:00
|
|
|
async setRecord (
|
2024-03-11 08:51:00 +00:00
|
|
|
{ privateKey, record, bondId }: { privateKey: string, record: any, bondId: string },
|
2022-04-08 05:29:03 +00:00
|
|
|
transactionPrivateKey: string,
|
2024-03-11 08:51:00 +00:00
|
|
|
fee: StdFee
|
2022-04-08 05:29:03 +00:00
|
|
|
) {
|
2024-03-11 08:51:00 +00:00
|
|
|
const account = new Account(Buffer.from(transactionPrivateKey, 'hex'));
|
|
|
|
await account.init();
|
|
|
|
const laconicClient = await this.getLaconicClient(account);
|
|
|
|
|
2024-03-12 04:55:59 +00:00
|
|
|
return laconicClient.setRecord({ privateKey, record, bondId },
|
2024-03-11 08:51:00 +00:00
|
|
|
account.address,
|
|
|
|
fee
|
|
|
|
);
|
2022-04-08 05:29:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 09:20:12 +00:00
|
|
|
/**
|
|
|
|
* Send coins.
|
|
|
|
*/
|
2024-03-07 10:36:52 +00:00
|
|
|
async sendCoins ({ amount, denom, destinationAddress }: MessageSendParams, privateKey: string, fee: StdFee) {
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
2024-03-07 10:36:52 +00:00
|
|
|
await account.init();
|
|
|
|
const laconicClient = await this.getLaconicClient(account);
|
2022-04-05 09:20:12 +00:00
|
|
|
|
2024-03-07 10:36:52 +00:00
|
|
|
const response: DeliverTxResponse = await laconicClient.sendTokens(
|
|
|
|
account.address,
|
|
|
|
destinationAddress,
|
|
|
|
[
|
|
|
|
Coin.fromPartial({
|
|
|
|
denom,
|
|
|
|
amount
|
|
|
|
})
|
|
|
|
],
|
|
|
|
fee);
|
2022-04-11 06:58:14 +00:00
|
|
|
|
2024-03-07 13:15:50 +00:00
|
|
|
// TODO: Register type /cosmos.bank.v1beta1.MsgSendResponse for decoding response
|
|
|
|
return response;
|
2022-04-05 09:20:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
/**
|
|
|
|
* Computes the next bondId for the given account private key.
|
|
|
|
*/
|
2024-03-07 04:17:05 +00:00
|
|
|
async getNextBondId (privateKey: string) {
|
2022-04-01 12:32:56 +00:00
|
|
|
let result;
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
2024-03-07 04:17:05 +00:00
|
|
|
await account.init();
|
2024-03-06 14:18:45 +00:00
|
|
|
const accounts = await this.getAccounts([account.address]);
|
2022-04-22 10:15:47 +00:00
|
|
|
if (!accounts.length) {
|
|
|
|
throw new Error('Account does not exist.');
|
|
|
|
}
|
2022-03-31 10:00:02 +00:00
|
|
|
|
2022-04-22 10:15:47 +00:00
|
|
|
const [accountObj] = accounts;
|
2022-04-11 06:58:14 +00:00
|
|
|
const nextSeq = parseInt(accountObj.sequence, 10) + 1;
|
2022-04-22 10:15:47 +00:00
|
|
|
result = sha256(`${accountObj.address}:${accountObj.number}:${nextSeq}`);
|
2022-03-31 10:00:02 +00:00
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
return result;
|
2022-03-31 10:00:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 07:05:16 +00:00
|
|
|
/**
|
|
|
|
* Get bonds by ids.
|
|
|
|
*/
|
2024-03-07 04:17:05 +00:00
|
|
|
async getBondsByIds (ids: string[]) {
|
2022-04-04 07:05:16 +00:00
|
|
|
return this._client.getBondsByIds(ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query bonds by attributes.
|
|
|
|
*/
|
2024-03-07 04:17:05 +00:00
|
|
|
async queryBonds (attributes = {}) {
|
2022-04-04 07:05:16 +00:00
|
|
|
return this._client.queryBonds(attributes);
|
|
|
|
}
|
|
|
|
|
2022-04-01 12:32:56 +00:00
|
|
|
/**
|
|
|
|
* Create bond.
|
|
|
|
*/
|
2024-03-07 04:17:05 +00:00
|
|
|
async createBond ({ denom, amount }: MessageMsgCreateBond, privateKey: string, fee: StdFee): Promise<MsgCreateBondResponse> {
|
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
await account.init();
|
|
|
|
const laconicClient = await this.getLaconicClient(account);
|
2022-03-31 10:00:02 +00:00
|
|
|
|
2024-03-12 04:55:59 +00:00
|
|
|
return laconicClient.createBond(
|
2024-03-06 14:18:45 +00:00
|
|
|
account.address,
|
|
|
|
denom,
|
|
|
|
amount,
|
|
|
|
fee
|
|
|
|
);
|
2022-03-31 10:00:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:20:20 +00:00
|
|
|
/**
|
|
|
|
* Refill bond.
|
|
|
|
*/
|
2024-03-07 04:17:05 +00:00
|
|
|
async refillBond ({ denom, amount, id }: MessageMsgRefillBond, privateKey: string, fee: StdFee): Promise<MsgRefillBondResponse> {
|
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
await account.init();
|
|
|
|
const laconicClient = await this.getLaconicClient(account);
|
2022-04-04 10:20:20 +00:00
|
|
|
|
2024-03-12 04:55:59 +00:00
|
|
|
return laconicClient.refillBond(
|
2024-03-06 14:18:45 +00:00
|
|
|
account.address,
|
|
|
|
denom,
|
|
|
|
amount,
|
|
|
|
id,
|
|
|
|
fee
|
|
|
|
);
|
2022-04-04 10:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Withdraw (from) bond.
|
|
|
|
*/
|
2024-03-07 04:17:05 +00:00
|
|
|
async withdrawBond ({ denom, amount, id }: MessageMsgWithdrawBond, privateKey: string, fee: StdFee): Promise<MsgWithdrawBondResponse> {
|
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
await account.init();
|
|
|
|
const laconicClient = await this.getLaconicClient(account);
|
2022-04-04 10:20:20 +00:00
|
|
|
|
2024-03-12 04:55:59 +00:00
|
|
|
return laconicClient.withdrawBond(
|
2024-03-06 14:18:45 +00:00
|
|
|
account.address,
|
|
|
|
denom,
|
|
|
|
amount,
|
|
|
|
id,
|
|
|
|
fee
|
|
|
|
);
|
2022-04-04 10:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel bond.
|
|
|
|
*/
|
2024-03-07 04:17:05 +00:00
|
|
|
async cancelBond ({ id }: MessageMsgCancelBond, privateKey: string, fee: StdFee): Promise<MsgCancelBondResponse> {
|
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
|
|
await account.init();
|
|
|
|
const laconicClient = await this.getLaconicClient(account);
|
2022-04-04 10:20:20 +00:00
|
|
|
|
2024-03-12 04:55:59 +00:00
|
|
|
return laconicClient.cancelBond(
|
2024-03-06 14:18:45 +00:00
|
|
|
account.address,
|
|
|
|
id,
|
|
|
|
fee
|
|
|
|
);
|
2022-04-04 10:20:20 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 10:00:21 +00:00
|
|
|
/**
|
|
|
|
* Associate record with bond.
|
|
|
|
*/
|
2024-03-11 11:27:27 +00:00
|
|
|
async associateBond ({ bondId, recordId }: MessageMsgAssociateBond, privateKey: string, fee: StdFee) {
|
2022-04-27 10:00:21 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
2024-03-11 11:27:27 +00:00
|
|
|
await account.init();
|
|
|
|
const laconicClient = await this.getLaconicClient(account);
|
2022-04-27 10:00:21 +00:00
|
|
|
|
2024-03-12 04:55:59 +00:00
|
|
|
return laconicClient.associateBond(
|
2024-03-11 11:27:27 +00:00
|
|
|
account.address,
|
|
|
|
recordId,
|
|
|
|
bondId,
|
|
|
|
fee
|
|
|
|
);
|
2022-04-27 10:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dissociate record from bond.
|
|
|
|
*/
|
2024-03-11 11:27:27 +00:00
|
|
|
async dissociateBond ({ recordId }: MessageMsgDissociateBond, privateKey: string, fee: StdFee) {
|
2022-04-27 10:00:21 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
2024-03-11 11:27:27 +00:00
|
|
|
await account.init();
|
|
|
|
const laconicClient = await this.getLaconicClient(account);
|
2022-04-27 10:00:21 +00:00
|
|
|
|
2024-03-12 04:55:59 +00:00
|
|
|
return laconicClient.dissociateBond(
|
2024-03-11 11:27:27 +00:00
|
|
|
account.address,
|
|
|
|
recordId,
|
|
|
|
fee
|
|
|
|
);
|
2022-04-27 10:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dissociate all records from bond.
|
|
|
|
*/
|
2024-03-11 11:27:27 +00:00
|
|
|
async dissociateRecords ({ bondId }: MessageMsgDissociateRecords, privateKey: string, fee: StdFee) {
|
2022-04-27 10:00:21 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
2024-03-11 11:27:27 +00:00
|
|
|
await account.init();
|
|
|
|
const laconicClient = await this.getLaconicClient(account);
|
2022-04-27 10:00:21 +00:00
|
|
|
|
2024-03-12 04:55:59 +00:00
|
|
|
return laconicClient.dissociateRecords(
|
2024-03-11 11:27:27 +00:00
|
|
|
account.address,
|
|
|
|
bondId,
|
|
|
|
fee
|
|
|
|
);
|
2022-04-27 10:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reassociate records (switch bond).
|
|
|
|
*/
|
2024-03-11 11:27:27 +00:00
|
|
|
async reassociateRecords ({ newBondId, oldBondId }: MessageMsgReAssociateRecords, privateKey: string, fee: StdFee) {
|
2022-04-27 10:00:21 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
2024-03-11 11:27:27 +00:00
|
|
|
await account.init();
|
|
|
|
const laconicClient = await this.getLaconicClient(account);
|
2022-04-27 10:00:21 +00:00
|
|
|
|
2024-03-12 04:55:59 +00:00
|
|
|
return laconicClient.reassociateRecords(
|
2024-03-11 11:27:27 +00:00
|
|
|
account.address,
|
|
|
|
oldBondId,
|
|
|
|
newBondId,
|
|
|
|
fee
|
|
|
|
);
|
2022-04-27 10:00:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 05:31:10 +00:00
|
|
|
/**
|
|
|
|
* Reserve authority.
|
|
|
|
*/
|
2024-03-07 10:36:52 +00:00
|
|
|
async reserveAuthority ({ name, owner }: { name: string, owner?: string }, privateKey: string, fee: StdFee) {
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
2024-03-07 10:36:52 +00:00
|
|
|
await account.init();
|
|
|
|
const laconicClient = await this.getLaconicClient(account);
|
2024-03-12 04:55:59 +00:00
|
|
|
|
|
|
|
return laconicClient.reserveAuthority(
|
2024-03-07 10:36:52 +00:00
|
|
|
account.address,
|
|
|
|
name,
|
|
|
|
owner || account.address,
|
|
|
|
fee
|
|
|
|
);
|
2022-04-05 05:31:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 05:29:03 +00:00
|
|
|
/**
|
|
|
|
* Set authority bond.
|
|
|
|
*/
|
2024-03-07 10:36:52 +00:00
|
|
|
async setAuthorityBond ({ bondId, name }: MessageMsgSetAuthorityBond, privateKey: string, fee: StdFee) {
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
2024-03-07 10:36:52 +00:00
|
|
|
await account.init();
|
|
|
|
const laconicClient = await this.getLaconicClient(account);
|
2024-03-12 04:55:59 +00:00
|
|
|
|
|
|
|
return laconicClient.setAuthorityBond(
|
2024-03-07 10:36:52 +00:00
|
|
|
account.address,
|
|
|
|
bondId,
|
|
|
|
name,
|
|
|
|
fee
|
|
|
|
);
|
2022-04-08 05:29:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 06:08:16 +00:00
|
|
|
/**
|
|
|
|
* Commit auction bid.
|
|
|
|
*/
|
2024-03-07 13:15:50 +00:00
|
|
|
async commitBid ({ auctionId, commitHash }: MessageMsgCommitBid, privateKey: string, fee: StdFee) {
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
2024-03-07 13:15:50 +00:00
|
|
|
await account.init();
|
|
|
|
const laconicClient = await this.getLaconicClient(account);
|
2024-03-12 04:55:59 +00:00
|
|
|
|
|
|
|
return laconicClient.commitBid(
|
2024-03-07 13:15:50 +00:00
|
|
|
account.address,
|
|
|
|
auctionId,
|
|
|
|
commitHash,
|
|
|
|
fee
|
|
|
|
);
|
2022-04-12 06:08:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reveal auction bid.
|
|
|
|
*/
|
2024-03-07 13:15:50 +00:00
|
|
|
async revealBid ({ auctionId, reveal }: MessageMsgRevealBid, privateKey: string, fee: StdFee) {
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
2024-03-07 13:15:50 +00:00
|
|
|
await account.init();
|
|
|
|
const laconicClient = await this.getLaconicClient(account);
|
2024-03-12 04:55:59 +00:00
|
|
|
return laconicClient.revealBid(
|
2024-03-07 13:15:50 +00:00
|
|
|
account.address,
|
|
|
|
auctionId,
|
|
|
|
reveal,
|
|
|
|
fee
|
|
|
|
);
|
2022-04-12 06:08:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get records by ids.
|
|
|
|
*/
|
2024-03-07 04:17:05 +00:00
|
|
|
async getAuctionsByIds (ids: string[]) {
|
2022-04-12 06:08:16 +00:00
|
|
|
return this._client.getAuctionsByIds(ids);
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:31:10 +00:00
|
|
|
/**
|
|
|
|
* Lookup authorities by names.
|
|
|
|
*/
|
2024-03-07 04:17:05 +00:00
|
|
|
async lookupAuthorities (names: string[], auction = false) {
|
2022-04-05 05:31:10 +00:00
|
|
|
return this._client.lookupAuthorities(names, auction);
|
|
|
|
}
|
|
|
|
|
2022-04-08 05:29:03 +00:00
|
|
|
/**
|
2024-03-11 08:51:00 +00:00
|
|
|
* Set name (LRN) to record ID (CID).
|
2022-04-08 05:29:03 +00:00
|
|
|
*/
|
2024-03-11 08:51:00 +00:00
|
|
|
async setName ({ cid, lrn }: MessageMsgSetName, privateKey: string, fee: StdFee) {
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
2024-03-11 08:51:00 +00:00
|
|
|
await account.init();
|
|
|
|
const laconicClient = await this.getLaconicClient(account);
|
2022-04-08 05:29:03 +00:00
|
|
|
|
2024-03-12 04:55:59 +00:00
|
|
|
return laconicClient.setName(
|
2024-03-11 08:51:00 +00:00
|
|
|
account.address,
|
|
|
|
lrn,
|
|
|
|
cid,
|
|
|
|
fee
|
|
|
|
);
|
2022-04-08 05:29:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 10:32:24 +00:00
|
|
|
/**
|
|
|
|
* Lookup naming information.
|
|
|
|
*/
|
2024-03-07 04:17:05 +00:00
|
|
|
async lookupNames (names: string[], history = false) {
|
2022-04-08 10:32:24 +00:00
|
|
|
return this._client.lookupNames(names, history);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-03-11 08:51:00 +00:00
|
|
|
* Delete name (LRN) mapping.
|
2022-04-08 10:32:24 +00:00
|
|
|
*/
|
2024-03-11 08:51:00 +00:00
|
|
|
async deleteName ({ lrn }: MessageMsgDeleteName, privateKey: string, fee: StdFee) {
|
2022-04-12 10:54:26 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
2024-03-11 08:51:00 +00:00
|
|
|
await account.init();
|
|
|
|
const laconicClient = await this.getLaconicClient(account);
|
2024-03-12 04:55:59 +00:00
|
|
|
return laconicClient.deleteName(
|
2024-03-11 08:51:00 +00:00
|
|
|
account.address,
|
|
|
|
lrn,
|
|
|
|
fee
|
|
|
|
);
|
2022-04-01 03:31:00 +00:00
|
|
|
}
|
2022-04-18 11:27:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* https://evmos.dev/basics/chain_id.html
|
|
|
|
*/
|
|
|
|
_parseEthChainId (chainId: string) {
|
2024-03-07 04:17:05 +00:00
|
|
|
const [idWithChainNumber] = chainId.split('-');
|
|
|
|
const [_, ethChainId] = idWithChainNumber.split('_');
|
2022-04-18 11:27:37 +00:00
|
|
|
|
2024-03-07 04:17:05 +00:00
|
|
|
return Number(ethChainId);
|
2022-04-18 11:27:37 +00:00
|
|
|
}
|
2022-04-22 10:15:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get sender used for creating message.
|
|
|
|
*/
|
|
|
|
async _getSender (account: Account) {
|
|
|
|
const accounts = await this.getAccounts([account.formattedCosmosAddress]);
|
|
|
|
if (!accounts.length) {
|
|
|
|
throw new Error('Account does not exist.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const [{ number, sequence }] = accounts;
|
|
|
|
|
|
|
|
return {
|
|
|
|
accountAddress: account.formattedCosmosAddress,
|
|
|
|
sequence: sequence,
|
|
|
|
accountNumber: number,
|
2024-03-07 04:17:05 +00:00
|
|
|
pubkey: account.encodedPubkey
|
|
|
|
};
|
2022-04-22 10:15:47 +00:00
|
|
|
}
|
2024-03-06 14:18:45 +00:00
|
|
|
|
2024-03-07 04:17:05 +00:00
|
|
|
async getLaconicClient (account: Account) {
|
|
|
|
return LaconicClient.connectWithSigner(this._endpoints.rest, account.wallet);
|
2024-03-06 14:18:45 +00:00
|
|
|
}
|
2022-03-24 06:40:03 +00:00
|
|
|
}
|
2022-04-05 05:31:10 +00:00
|
|
|
|
2024-03-07 04:17:05 +00:00
|
|
|
export { Account };
|