2024-01-29 04:46:32 +00:00
|
|
|
import fs from 'fs';
|
2024-09-06 09:44:47 +00:00
|
|
|
import path from 'path';
|
2024-01-29 04:46:32 +00:00
|
|
|
import yaml from 'js-yaml';
|
|
|
|
import { SpawnSyncReturns, spawnSync } from 'child_process';
|
2024-09-25 16:00:42 +00:00
|
|
|
import { Arguments } from 'yargs';
|
2024-01-29 04:46:32 +00:00
|
|
|
|
2024-09-25 16:00:42 +00:00
|
|
|
import { StdFee } from '@cosmjs/stargate';
|
|
|
|
|
|
|
|
import { getConfig, getGasAndFees } from '../src/util';
|
2024-09-06 09:44:47 +00:00
|
|
|
|
2024-01-29 04:46:32 +00:00
|
|
|
export const CHAIN_ID = 'laconic_9000-1';
|
2024-07-30 13:24:46 +00:00
|
|
|
export const TOKEN_TYPE = 'alnt';
|
2024-09-25 16:00:42 +00:00
|
|
|
export const CONFIG_FILE = 'config.yml';
|
2024-01-29 04:46:32 +00:00
|
|
|
|
2024-09-25 13:57:55 +00:00
|
|
|
export enum AUCTION_STATUS {
|
|
|
|
COMMIT = 'commit',
|
|
|
|
REVEAL = 'reveal',
|
|
|
|
COMPLETED = 'completed'
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:46:32 +00:00
|
|
|
export const AUCTION_FEES = {
|
|
|
|
commit: 1000000,
|
|
|
|
reveal: 1000000,
|
|
|
|
minimumBid: 5000000
|
|
|
|
};
|
|
|
|
export const AUCTION_COMMIT_DURATION = 60; // 60s
|
|
|
|
export const AUCTION_REVEAL_DURATION = 60; // 60s
|
|
|
|
|
2024-01-29 05:21:34 +00:00
|
|
|
export function checkResultAndRetrieveOutput (result: SpawnSyncReturns<Buffer>): any {
|
2024-09-25 13:57:55 +00:00
|
|
|
if (result.status !== 0) {
|
|
|
|
console.log('stderr', result.stderr.toString().trim());
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:46:32 +00:00
|
|
|
expect(result.status).toBe(0);
|
|
|
|
|
|
|
|
const errorOutput = result.stderr.toString().trim();
|
|
|
|
expect(errorOutput).toBe('');
|
|
|
|
|
|
|
|
const output = result.stdout.toString().trim();
|
|
|
|
expect(output.length).toBeGreaterThan(0);
|
|
|
|
return JSON.parse(output);
|
|
|
|
}
|
|
|
|
|
2024-01-29 05:21:34 +00:00
|
|
|
export function createBond (quantity: number): { bondId: string } {
|
2024-03-19 04:42:42 +00:00
|
|
|
const result = spawnSync('laconic', ['registry', 'bond', 'create', '--type', TOKEN_TYPE, '--quantity', quantity.toString(), '--gas', '200000', '--fees', `200000${TOKEN_TYPE}`]);
|
2024-01-29 04:46:32 +00:00
|
|
|
const output = result.stdout.toString().trim();
|
|
|
|
|
|
|
|
return JSON.parse(output);
|
|
|
|
}
|
|
|
|
|
2024-01-29 05:21:34 +00:00
|
|
|
export function getBondObj (params: { id: string, owner: string, balance: number}): any {
|
2024-01-29 04:46:32 +00:00
|
|
|
return {
|
|
|
|
id: params.id,
|
|
|
|
owner: params.owner,
|
|
|
|
balance: [
|
|
|
|
{
|
|
|
|
type: TOKEN_TYPE,
|
|
|
|
quantity: params.balance
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-29 05:21:34 +00:00
|
|
|
export function getAccountObj (params: { address: string, balance?: number }): any {
|
2024-01-29 04:46:32 +00:00
|
|
|
const balanceObj: any = { type: TOKEN_TYPE };
|
|
|
|
if (params.balance) {
|
|
|
|
balanceObj.quantity = params.balance;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
address: params.address,
|
|
|
|
balance: [balanceObj]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-29 05:21:34 +00:00
|
|
|
export function getRecordObj (recordFilePath: string, params: { bondId: string, recordId: string, names: any }): any {
|
2024-01-29 04:46:32 +00:00
|
|
|
const recordContent = yaml.load(fs.readFileSync(recordFilePath, 'utf8')) as any;
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: params.recordId,
|
|
|
|
names: params.names,
|
|
|
|
bondId: params.bondId,
|
|
|
|
attributes: recordContent.record
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-29 05:21:34 +00:00
|
|
|
export function getAuthorityObj (params: { owner: string, status: string, auction: any, bondId?: string }): any {
|
2024-01-29 04:46:32 +00:00
|
|
|
return {
|
|
|
|
ownerAddress: params.owner,
|
|
|
|
status: params.status,
|
|
|
|
bondId: params.bondId || '',
|
|
|
|
auction: params.auction
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-29 05:21:34 +00:00
|
|
|
export function getAuctionObj (params: { owner: string, status?: string }): any {
|
2024-01-29 04:46:32 +00:00
|
|
|
return {
|
|
|
|
status: params.status || 'commit',
|
|
|
|
ownerAddress: params.owner,
|
|
|
|
commitFee: {
|
|
|
|
type: TOKEN_TYPE,
|
|
|
|
quantity: AUCTION_FEES.commit
|
|
|
|
},
|
|
|
|
revealFee: {
|
|
|
|
type: TOKEN_TYPE,
|
|
|
|
quantity: AUCTION_FEES.reveal
|
|
|
|
},
|
|
|
|
minimumBid: {
|
|
|
|
type: TOKEN_TYPE,
|
|
|
|
quantity: AUCTION_FEES.minimumBid
|
|
|
|
},
|
2024-09-25 13:57:55 +00:00
|
|
|
winnerAddresses: [],
|
|
|
|
winnerBids: []
|
2024-01-29 04:46:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-29 05:21:34 +00:00
|
|
|
export function getBidObj (params: { bidder: string, status?: string }): any {
|
2024-01-29 04:46:32 +00:00
|
|
|
return {
|
|
|
|
bidderAddress: params.bidder,
|
|
|
|
status: params.status || 'commit',
|
|
|
|
commitFee: {
|
|
|
|
type: TOKEN_TYPE,
|
|
|
|
quantity: AUCTION_FEES.commit
|
|
|
|
},
|
|
|
|
revealFee: {
|
|
|
|
type: TOKEN_TYPE,
|
|
|
|
quantity: AUCTION_FEES.reveal
|
|
|
|
},
|
|
|
|
bidAmount: {
|
|
|
|
type: '',
|
|
|
|
quantity: 0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-29 05:21:34 +00:00
|
|
|
export async function delay (ms: number): Promise<any> {
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
2024-01-29 04:46:32 +00:00
|
|
|
}
|
2024-09-06 09:44:47 +00:00
|
|
|
|
2024-09-25 16:00:42 +00:00
|
|
|
export function getFeesConfig (): number {
|
|
|
|
const { services: { registry: registryConfig } } = getConfig(CONFIG_FILE);
|
|
|
|
const fee = getGasAndFees({} as Arguments, registryConfig);
|
|
|
|
return Number((fee as StdFee).amount[0].amount);
|
|
|
|
}
|
|
|
|
|
2024-09-06 09:44:47 +00:00
|
|
|
export function updateGasAndFeesConfig (gas?: string | null, fees?: string | null, gasPrice?: string | null): void {
|
2024-09-25 16:00:42 +00:00
|
|
|
const config = getConfig(path.resolve(CONFIG_FILE));
|
2024-09-06 09:44:47 +00:00
|
|
|
|
|
|
|
if (gas) {
|
|
|
|
config.services.registry.gas = gas;
|
|
|
|
} else if (gas === null) {
|
|
|
|
delete config.services.registry.gas;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fees) {
|
|
|
|
config.services.registry.fees = fees;
|
|
|
|
} else if (fees === null) {
|
|
|
|
delete config.services.registry.fees;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gasPrice) {
|
|
|
|
config.services.registry.gasPrice = gasPrice;
|
|
|
|
} else if (gasPrice === null) {
|
|
|
|
delete config.services.registry.gasPrice;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-09-25 16:00:42 +00:00
|
|
|
fs.writeFileSync(CONFIG_FILE, yaml.dump(config), 'utf8');
|
2024-09-06 09:44:47 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error('Error writing config file:', e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|