forked from cerc-io/registry-sdk
Update registry SDK auction methods to use cosmjs (#1)
Part of https://www.notion.so/Create-laconic-registry-SDK-d3a636d4aba44f7cbba3bd99b7146811 - Update account info and balance test - Update methods to commit and reveal bid Co-authored-by: neeraj <neeraj.rtly@gmail.com> Reviewed-on: #1 Co-authored-by: nabarun <nabarun@noreply.git.vdb.to> Co-committed-by: nabarun <nabarun@noreply.git.vdb.to>
This commit is contained in:
parent
2589aacdb4
commit
d4c168f10c
@ -3,9 +3,8 @@ import { getConfig, getLaconic2Config } from './testing/helper';
|
|||||||
import { DENOM } from './constants';
|
import { DENOM } from './constants';
|
||||||
|
|
||||||
jest.setTimeout(30 * 60 * 1000);
|
jest.setTimeout(30 * 60 * 1000);
|
||||||
const { fee: laconic2Fee } = getLaconic2Config();
|
|
||||||
|
|
||||||
const { chainId, restEndpoint, gqlEndpoint, privateKey, fee } = getConfig();
|
const { chainId, restEndpoint, gqlEndpoint, privateKey, fee } = getConfig();
|
||||||
|
const { fee: laconic2Fee } = getLaconic2Config();
|
||||||
|
|
||||||
const auctionTests = (numBidders = 3) => {
|
const auctionTests = (numBidders = 3) => {
|
||||||
let registry: Registry;
|
let registry: Registry;
|
||||||
@ -26,7 +25,7 @@ const auctionTests = (numBidders = 3) => {
|
|||||||
const mnenonic = Account.generateMnemonic();
|
const mnenonic = Account.generateMnemonic();
|
||||||
const account = await Account.generateFromMnemonic(mnenonic);
|
const account = await Account.generateFromMnemonic(mnenonic);
|
||||||
await account.init();
|
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') });
|
accounts.push({ address: account.address, privateKey: account.privateKey.toString('hex') });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -50,8 +49,8 @@ const auctionTests = (numBidders = 3) => {
|
|||||||
|
|
||||||
test('Commit bids.', async () => {
|
test('Commit bids.', async () => {
|
||||||
for (let i = 0; i < numBidders; i++) {
|
for (let i = 0; i < numBidders; i++) {
|
||||||
accounts[i].bid = await createBid(chainId, auctionId, accounts[i].address, `${10000000 + (i * 500)}aphoton`);
|
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, fee);
|
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');
|
expect(auction.status).toEqual('reveal');
|
||||||
|
|
||||||
for (let i = 0; i < numBidders; i++) {
|
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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
import { Account } from './account';
|
import { Account } from './account';
|
||||||
|
import { DENOM } from './constants';
|
||||||
import { Registry } from './index';
|
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);
|
jest.setTimeout(90 * 1000);
|
||||||
|
|
||||||
@ -15,30 +17,32 @@ const registryTests = () => {
|
|||||||
|
|
||||||
test('Get account info.', async () => {
|
test('Get account info.', async () => {
|
||||||
const account = new Account(Buffer.from(privateKey, 'hex'));
|
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);
|
expect(accounts).toHaveLength(1);
|
||||||
const [accountObj] = accounts;
|
const [accountObj] = accounts;
|
||||||
expect(accountObj.address).toBe(account.formattedCosmosAddress);
|
expect(accountObj.address).toBe(account.address);
|
||||||
expect(accountObj.pubKey).toBe(account.encodedPubkey);
|
expect(accountObj.pubKey).toBe(account.encodedPubkey);
|
||||||
expect(accountObj.number).toBe('0');
|
expect(accountObj.number).toBe('0');
|
||||||
expect(accountObj.sequence).toBeDefined();
|
expect(accountObj.sequence).toBeDefined();
|
||||||
expect(accountObj.balance).toHaveLength(1);
|
expect(accountObj.balance).toHaveLength(1);
|
||||||
const [{ type, quantity }] = accountObj.balance;
|
const [{ type, quantity }] = accountObj.balance;
|
||||||
expect(type).toBe('aphoton');
|
expect(type).toBe(DENOM);
|
||||||
expect(quantity).toBeDefined();
|
expect(quantity).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Get account balance.', async () => {
|
test('Get account balance.', async () => {
|
||||||
const mnenonic1 = Account.generateMnemonic();
|
const mnenonic1 = Account.generateMnemonic();
|
||||||
const otherAccount = await Account.generateFromMnemonic(mnenonic1);
|
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).toBeDefined();
|
||||||
expect(accountObj.address).toBe(otherAccount.formattedCosmosAddress);
|
expect(accountObj.address).toBe(otherAccount.address);
|
||||||
const [{ type, quantity }] = accountObj.balance;
|
const [{ type, quantity }] = accountObj.balance;
|
||||||
expect(type).toBe('aphoton');
|
expect(type).toBe(DENOM);
|
||||||
expect(quantity).toBe('100000000');
|
expect(quantity).toBe('10000');
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
37
src/index.ts
37
src/index.ts
@ -216,7 +216,8 @@ export class Registry {
|
|||||||
],
|
],
|
||||||
fee);
|
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.
|
* Commit auction bid.
|
||||||
*/
|
*/
|
||||||
async commitBid (params: MessageMsgCommitBid, privateKey: string, fee: Fee) {
|
async commitBid ({ auctionId, commitHash }: MessageMsgCommitBid, privateKey: string, fee: StdFee) {
|
||||||
let result;
|
|
||||||
const account = new Account(Buffer.from(privateKey, 'hex'));
|
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);
|
return laconicClient.registry.decode(response.msgResponses[0]);
|
||||||
result = await this._submitTx(msg, privateKey, sender);
|
|
||||||
|
|
||||||
return parseTxResponse(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reveal auction bid.
|
* Reveal auction bid.
|
||||||
*/
|
*/
|
||||||
async revealBid (params: MessageMsgRevealBid, privateKey: string, fee: Fee) {
|
async revealBid ({ auctionId, reveal }: MessageMsgRevealBid, privateKey: string, fee: StdFee) {
|
||||||
let result;
|
|
||||||
const account = new Account(Buffer.from(privateKey, 'hex'));
|
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);
|
return laconicClient.registry.decode(response.msgResponses[0]);
|
||||||
result = await this._submitTx(msg, privateKey, sender);
|
|
||||||
|
|
||||||
return parseTxResponse(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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 { MsgCancelBondEncodeObject, MsgCreateBondEncodeObject, MsgRefillBondEncodeObject, MsgWithdrawBondEncodeObject, bondTypes, typeUrlMsgCancelBond, typeUrlMsgCreateBond, typeUrlMsgRefillBond, typeUrlMsgWithdrawBond } from './types/cerc/bond/message';
|
||||||
import { Coin } from './proto2/cosmos/base/v1beta1/coin';
|
import { Coin } from './proto2/cosmos/base/v1beta1/coin';
|
||||||
import { MsgReserveAuthorityEncodeObject, MsgSetAuthorityBondEncodeObject, registryTypes, typeUrlMsgReserveAuthority, typeUrlMsgSetAuthorityBond } from './types/cerc/registry/message';
|
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]> = [
|
export const laconicDefaultRegistryTypes: ReadonlyArray<[string, GeneratedType]> = [
|
||||||
...defaultRegistryTypes,
|
...defaultRegistryTypes,
|
||||||
...bondTypes,
|
...bondTypes,
|
||||||
...registryTypes
|
...registryTypes,
|
||||||
|
...auctionTypes
|
||||||
];
|
];
|
||||||
|
|
||||||
function createDefaultRegistry (): Registry {
|
function createDefaultRegistry (): Registry {
|
||||||
@ -145,6 +147,44 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async commitBid (
|
||||||
|
signer: string,
|
||||||
|
auctionId: string,
|
||||||
|
commitHash: string,
|
||||||
|
fee: StdFee | 'auto' | number,
|
||||||
|
memo = ''
|
||||||
|
): Promise<DeliverTxResponse> {
|
||||||
|
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<DeliverTxResponse> {
|
||||||
|
const createMsg: MsgRevealBidEncodeObject = {
|
||||||
|
typeUrl: typeUrlMsgRevealBid,
|
||||||
|
value: {
|
||||||
|
signer,
|
||||||
|
auctionId,
|
||||||
|
reveal
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
|
}
|
||||||
|
|
||||||
public async setAuthorityBond (
|
public async setAuthorityBond (
|
||||||
signer: string,
|
signer: string,
|
||||||
bondId: string,
|
bondId: string,
|
||||||
|
25
src/types/cerc/auction/message.ts
Normal file
25
src/types/cerc/auction/message.ts
Normal file
@ -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<MsgCommitBid>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MsgRevealBidEncodeObject extends EncodeObject {
|
||||||
|
readonly typeUrl: '/cerc.auction.v1.MsgRevealBid';
|
||||||
|
readonly value: Partial<MsgRevealBid>;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user