From e5109f8635be2f5d7b57a760ac92c966f4897c20 Mon Sep 17 00:00:00 2001 From: neeraj Date: Thu, 7 Mar 2024 18:09:23 +0530 Subject: [PATCH 1/2] Update methods to commit and reveal bid --- src/auction.test.ts | 11 ++++---- src/index.ts | 37 ++++++++++++++++----------- src/laconic-client.ts | 42 ++++++++++++++++++++++++++++++- src/types/cerc/auction/message.ts | 25 ++++++++++++++++++ 4 files changed, 93 insertions(+), 22 deletions(-) create mode 100644 src/types/cerc/auction/message.ts diff --git a/src/auction.test.ts b/src/auction.test.ts index 0c13c87..4f8239a 100644 --- a/src/auction.test.ts +++ b/src/auction.test.ts @@ -3,9 +3,8 @@ import { getConfig, getLaconic2Config } from './testing/helper'; import { DENOM } from './constants'; jest.setTimeout(30 * 60 * 1000); -const { fee: laconic2Fee } = getLaconic2Config(); - const { chainId, restEndpoint, gqlEndpoint, privateKey, fee } = getConfig(); +const { fee: laconic2Fee } = getLaconic2Config(); const auctionTests = (numBidders = 3) => { let registry: Registry; @@ -26,7 +25,7 @@ const auctionTests = (numBidders = 3) => { const mnenonic = Account.generateMnemonic(); const account = await Account.generateFromMnemonic(mnenonic); await account.init(); - await registry.sendCoins({ denom: DENOM, amount: '100000', destinationAddress: account.address }, privateKey, laconic2Fee); + await registry.sendCoins({ denom: DENOM, amount: '20000000', destinationAddress: account.address }, privateKey, laconic2Fee); accounts.push({ address: account.address, privateKey: account.privateKey.toString('hex') }); } }); @@ -50,8 +49,8 @@ const auctionTests = (numBidders = 3) => { test('Commit bids.', async () => { for (let i = 0; i < numBidders; i++) { - accounts[i].bid = await createBid(chainId, auctionId, accounts[i].address, `${10000000 + (i * 500)}aphoton`); - await registry.commitBid({ auctionId, commitHash: accounts[i].bid.commitHash }, accounts[i].privateKey, fee); + accounts[i].bid = await createBid(chainId, auctionId, accounts[i].address, `${10000000 + (i * 500)}${DENOM}`); + await registry.commitBid({ auctionId, commitHash: accounts[i].bid.commitHash }, accounts[i].privateKey, laconic2Fee); } }); @@ -75,7 +74,7 @@ const auctionTests = (numBidders = 3) => { expect(auction.status).toEqual('reveal'); for (let i = 0; i < numBidders; i++) { - await registry.revealBid({ auctionId, reveal: accounts[i].bid.revealString }, accounts[i].privateKey, fee); + await registry.revealBid({ auctionId, reveal: accounts[i].bid.revealString }, accounts[i].privateKey, laconic2Fee); } }); diff --git a/src/index.ts b/src/index.ts index 889bff8..e7399ed 100644 --- a/src/index.ts +++ b/src/index.ts @@ -216,7 +216,8 @@ export class Registry { ], fee); - return laconicClient.registry.decode(response.msgResponses[0]); + // TODO: Register type /cosmos.bank.v1beta1.MsgSendResponse for decoding response + return response; } /** @@ -419,29 +420,35 @@ export class Registry { /** * Commit auction bid. */ - async commitBid (params: MessageMsgCommitBid, privateKey: string, fee: Fee) { - let result; + async commitBid ({ auctionId, commitHash }: MessageMsgCommitBid, privateKey: string, fee: StdFee) { const account = new Account(Buffer.from(privateKey, 'hex')); - const sender = await this._getSender(account); + await account.init(); + const laconicClient = await this.getLaconicClient(account); + const response: DeliverTxResponse = await laconicClient.commitBid( + account.address, + auctionId, + commitHash, + fee + ); - const msg = createTxMsgCommitBid(this._chain, sender, fee, '', params); - result = await this._submitTx(msg, privateKey, sender); - - return parseTxResponse(result); + return laconicClient.registry.decode(response.msgResponses[0]); } /** * Reveal auction bid. */ - async revealBid (params: MessageMsgRevealBid, privateKey: string, fee: Fee) { - let result; + async revealBid ({ auctionId, reveal }: MessageMsgRevealBid, privateKey: string, fee: StdFee) { const account = new Account(Buffer.from(privateKey, 'hex')); - const sender = await this._getSender(account); + await account.init(); + const laconicClient = await this.getLaconicClient(account); + const response: DeliverTxResponse = await laconicClient.revealBid( + account.address, + auctionId, + reveal, + fee + ); - const msg = createTxMsgRevealBid(this._chain, sender, fee, '', params); - result = await this._submitTx(msg, privateKey, sender); - - return parseTxResponse(result); + return laconicClient.registry.decode(response.msgResponses[0]); } /** diff --git a/src/laconic-client.ts b/src/laconic-client.ts index 6e8b343..1f2a854 100644 --- a/src/laconic-client.ts +++ b/src/laconic-client.ts @@ -12,11 +12,13 @@ import { Comet38Client } from '@cosmjs/tendermint-rpc'; import { MsgCancelBondEncodeObject, MsgCreateBondEncodeObject, MsgRefillBondEncodeObject, MsgWithdrawBondEncodeObject, bondTypes, typeUrlMsgCancelBond, typeUrlMsgCreateBond, typeUrlMsgRefillBond, typeUrlMsgWithdrawBond } from './types/cerc/bond/message'; import { Coin } from './proto2/cosmos/base/v1beta1/coin'; import { MsgReserveAuthorityEncodeObject, MsgSetAuthorityBondEncodeObject, registryTypes, typeUrlMsgReserveAuthority, typeUrlMsgSetAuthorityBond } from './types/cerc/registry/message'; +import { MsgCommitBidEncodeObject, MsgRevealBidEncodeObject, auctionTypes, typeUrlMsgCommitBid, typeUrlMsgRevealBid } from './types/cerc/auction/message'; export const laconicDefaultRegistryTypes: ReadonlyArray<[string, GeneratedType]> = [ ...defaultRegistryTypes, ...bondTypes, - ...registryTypes + ...registryTypes, + ...auctionTypes ]; function createDefaultRegistry (): Registry { @@ -145,6 +147,44 @@ export class LaconicClient extends SigningStargateClient { return this.signAndBroadcast(signer, [createMsg], fee, memo); } + public async commitBid ( + signer: string, + auctionId: string, + commitHash: string, + fee: StdFee | 'auto' | number, + memo = '' + ): Promise { + const createMsg: MsgCommitBidEncodeObject = { + typeUrl: typeUrlMsgCommitBid, + value: { + signer, + auctionId, + commitHash + } + }; + + return this.signAndBroadcast(signer, [createMsg], fee, memo); + } + + public async revealBid ( + signer: string, + auctionId: string, + reveal: string, + fee: StdFee | 'auto' | number, + memo = '' + ): Promise { + const createMsg: MsgRevealBidEncodeObject = { + typeUrl: typeUrlMsgRevealBid, + value: { + signer, + auctionId, + reveal + } + }; + + return this.signAndBroadcast(signer, [createMsg], fee, memo); + } + public async setAuthorityBond ( signer: string, bondId: string, diff --git a/src/types/cerc/auction/message.ts b/src/types/cerc/auction/message.ts new file mode 100644 index 0000000..87c4abe --- /dev/null +++ b/src/types/cerc/auction/message.ts @@ -0,0 +1,25 @@ +import { EncodeObject, GeneratedType } from '@cosmjs/proto-signing'; + +import { MsgCommitBidResponse, MsgCommitBid, MsgRevealBid, MsgRevealBidResponse } from '../../../proto2/cerc/auction/v1/tx'; + +export const typeUrlMsgCommitBid = '/cerc.auction.v1.MsgCommitBid'; +export const typeUrlMsgCommitBidResponse = '/cerc.auction.v1.MsgCommitBidResponse'; +export const typeUrlMsgRevealBid = '/cerc.auction.v1.MsgRevealBid'; +export const typeUrlMsgRevealBidResponse = '/cerc.auction.v1.MsgRevealBidResponse'; + +export const auctionTypes: ReadonlyArray<[string, GeneratedType]> = [ + [typeUrlMsgCommitBid, MsgCommitBid], + [typeUrlMsgCommitBidResponse, MsgCommitBidResponse], + [typeUrlMsgRevealBid, MsgRevealBid], + [typeUrlMsgRevealBidResponse, MsgRevealBidResponse] +]; + +export interface MsgCommitBidEncodeObject extends EncodeObject { + readonly typeUrl: '/cerc.auction.v1.MsgCommitBid'; + readonly value: Partial; +} + +export interface MsgRevealBidEncodeObject extends EncodeObject { + readonly typeUrl: '/cerc.auction.v1.MsgRevealBid'; + readonly value: Partial; +} -- 2.45.2 From b183cfe9eec52528e55d0182eeb43c49d928cbac Mon Sep 17 00:00:00 2001 From: neeraj Date: Thu, 7 Mar 2024 18:11:23 +0530 Subject: [PATCH 2/2] Update account info and balance test --- src/index.test.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index 9b84da2..33c782d 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,8 +1,10 @@ import { Account } from './account'; +import { DENOM } from './constants'; import { Registry } from './index'; -import { getConfig } from './testing/helper'; +import { getConfig, getLaconic2Config } from './testing/helper'; -const { chainId, restEndpoint, gqlEndpoint, privateKey, fee } = getConfig(); +const { chainId, restEndpoint, gqlEndpoint, privateKey } = getConfig(); +const { fee } = getLaconic2Config(); jest.setTimeout(90 * 1000); @@ -15,30 +17,32 @@ const registryTests = () => { test('Get account info.', async () => { const account = new Account(Buffer.from(privateKey, 'hex')); - const accounts = await registry.getAccounts([account.formattedCosmosAddress]); + await account.init(); + const accounts = await registry.getAccounts([account.address]); expect(accounts).toHaveLength(1); const [accountObj] = accounts; - expect(accountObj.address).toBe(account.formattedCosmosAddress); + expect(accountObj.address).toBe(account.address); expect(accountObj.pubKey).toBe(account.encodedPubkey); expect(accountObj.number).toBe('0'); expect(accountObj.sequence).toBeDefined(); expect(accountObj.balance).toHaveLength(1); const [{ type, quantity }] = accountObj.balance; - expect(type).toBe('aphoton'); + expect(type).toBe(DENOM); expect(quantity).toBeDefined(); }); test('Get account balance.', async () => { const mnenonic1 = Account.generateMnemonic(); const otherAccount = await Account.generateFromMnemonic(mnenonic1); - await registry.sendCoins({ denom: 'aphoton', amount: '100000000', destinationAddress: otherAccount.formattedCosmosAddress }, privateKey, fee); + await otherAccount.init(); + await registry.sendCoins({ denom: DENOM, amount: '10000', destinationAddress: otherAccount.address }, privateKey, fee); - const [accountObj] = await registry.getAccounts([otherAccount.formattedCosmosAddress]); + const [accountObj] = await registry.getAccounts([otherAccount.address]); expect(accountObj).toBeDefined(); - expect(accountObj.address).toBe(otherAccount.formattedCosmosAddress); + expect(accountObj.address).toBe(otherAccount.address); const [{ type, quantity }] = accountObj.balance; - expect(type).toBe('aphoton'); - expect(quantity).toBe('100000000'); + expect(type).toBe(DENOM); + expect(quantity).toBe('10000'); }); }; -- 2.45.2