Skip to content

Commit

Permalink
Merge pull request #73 from White-Whale-Defi-Platform/bug/sequence_er…
Browse files Browse the repository at this point in the history
…rors

bug: fix sequence error and cleanup nomempoolloop
  • Loading branch information
SirTLB authored Apr 5, 2023
2 parents e63569e + df7195b commit 321afcd
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 113 deletions.
114 changes: 74 additions & 40 deletions src/core/chainOperator/chainAdapters/cosmjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,53 @@ import { ChainOperatorInterface, TxResponse } from "../chainOperatorInterface";
*
*/
class CosmjsAdapter implements ChainOperatorInterface {
signingCWClient!: SigningCosmWasmClient; //used to sign transactions
tmClient!: Tendermint34Client; //used to broadcast transactions
httpClient: HttpBatchClient | HttpClient; //used to query rpc methods (unconfirmed_txs, account)
wasmQueryClient!: QueryClient & WasmExtension; //used to query wasm methods (contract states)
account!: AccountData;
publicAddress!: string;
accountNumber = 0;
sequence = 0;
chainId!: string;
signer!: DirectSecp256k1HdWallet;
skipBundleClient?: SkipBundleClient;
private _signingCWClient!: SigningCosmWasmClient; //used to sign transactions
private _tmClient!: Tendermint34Client; //used to broadcast transactions
private _httpClient: HttpBatchClient | HttpClient; //used to query rpc methods (unconfirmed_txs, account)
private _wasmQueryClient!: QueryClient & WasmExtension; //used to query wasm methods (contract states)
private _account!: AccountData;
private _publicAddress!: string;
private _accountNumber = 0;
private _sequence = 0;

private _chainId!: string;

private _signer!: DirectSecp256k1HdWallet;
private _skipBundleClient?: SkipBundleClient;

/**
*
*/
constructor(botConfig: BotConfig) {
this.httpClient = new HttpBatchClient(botConfig.rpcUrl);
this._httpClient = new HttpBatchClient(botConfig.rpcUrl);
if (botConfig.skipConfig) {
this.skipBundleClient = new SkipBundleClient(botConfig.skipConfig.skipRpcUrl);
this._skipBundleClient = new SkipBundleClient(botConfig.skipConfig.skipRpcUrl);
}
}
/**
*
*/
public get sequence() {
return this._sequence;
}
/**
*
*/
public set sequence(value) {
this._sequence = value;
}
/**
*
*/
public get publicAddress(): string {
return this._publicAddress;
}
/**
*
*/
public get chainId(): string {
return this._chainId;
}
/**
*
*/
Expand All @@ -45,28 +71,27 @@ class CosmjsAdapter implements ChainOperatorInterface {
const signer = await DirectSecp256k1HdWallet.fromMnemonic(botConfig.mnemonic, {
prefix: botConfig.chainPrefix,
});
this.signer = signer;
this._signer = signer;

// connect to client and querier
this.signingCWClient = await SigningCosmWasmClient.connectWithSigner(botConfig.rpcUrl, signer, {
this._signingCWClient = await SigningCosmWasmClient.connectWithSigner(botConfig.rpcUrl, signer, {
prefix: botConfig.chainPrefix,
gasPrice: GasPrice.fromString(botConfig.gasPrice + botConfig.baseDenom),
});
this.httpClient = new HttpBatchClient(botConfig.rpcUrl);
this.tmClient = await Tendermint34Client.create(this.httpClient);
this.wasmQueryClient = QueryClient.withExtensions(this.tmClient, setupWasmExtension, setupAuthExtension);
this.account = (await signer.getAccounts())[0];
const { accountNumber, sequence } = await this.signingCWClient.getSequence(this.account.address);
this.chainId = await this.signingCWClient.getChainId();
this.accountNumber = accountNumber;
this.sequence = sequence;
this.publicAddress = this.account.address;
this._tmClient = await Tendermint34Client.create(this._httpClient);
this._wasmQueryClient = QueryClient.withExtensions(this._tmClient, setupWasmExtension, setupAuthExtension);
this._account = (await signer.getAccounts())[0];
const { accountNumber, sequence } = await this._signingCWClient.getSequence(this._account.address);
this._chainId = await this._signingCWClient.getChainId();
this._accountNumber = accountNumber;
this._sequence = sequence;
this._publicAddress = this._account.address;
}
/**
*
*/
async queryContractSmart(address: string, queryMsg: Record<string, unknown>): Promise<JsonObject> {
return await this.wasmQueryClient.wasm.queryContractSmart(address, queryMsg);
return await this._wasmQueryClient.wasm.queryContractSmart(address, queryMsg);
}
/**
*
Expand All @@ -77,16 +102,16 @@ class CosmjsAdapter implements ChainOperatorInterface {
memo?: string | undefined,
): Promise<TxResponse> {
if (fee === "auto") {
return await this.signingCWClient.signAndBroadcast(this.publicAddress, msgs, fee, memo);
return await this._signingCWClient.signAndBroadcast(this.publicAddress, msgs, fee, memo);
} else {
const signerData = {
accountNumber: this.accountNumber,
sequence: this.sequence,
chainId: this.chainId,
accountNumber: this._accountNumber,
sequence: this._sequence,
chainId: this._chainId,
};
const txRaw = await this.signingCWClient.sign(this.publicAddress, msgs, fee, "memo", signerData);
const txRaw = await this._signingCWClient.sign(this.publicAddress, msgs, fee, "memo", signerData);
const txBytes = TxRaw.encode(txRaw).finish();
const res = await this.tmClient.broadcastTxSync({ tx: txBytes });
const res = await this._tmClient.broadcastTxSync({ tx: txBytes });
console.log(res);
return {
height: 0,
Expand All @@ -100,35 +125,44 @@ class CosmjsAdapter implements ChainOperatorInterface {
*
*/
async signAndBroadcastSkipBundle(messages: Array<EncodeObject>, fee: StdFee, memo?: string, otherTx?: TxRaw) {
if (!this.skipBundleClient) {
if (!this._skipBundleClient) {
console.log("skip bundle client not initialised");
process.exit(1);
}

const signerData = {
accountNumber: this.accountNumber,
sequence: this.sequence,
chainId: this.chainId,
accountNumber: this._accountNumber,
sequence: this._sequence,
chainId: this._chainId,
};
const txRaw: TxRaw = await this.signingCWClient.sign(this.publicAddress, messages, fee, "", signerData);
const txRaw: TxRaw = await this._signingCWClient.sign(this.publicAddress, messages, fee, "", signerData);

let signed;
if (otherTx) {
signed = await this.skipBundleClient.signBundle([otherTx, txRaw], this.signer, this.publicAddress);
signed = await this._skipBundleClient.signBundle([otherTx, txRaw], this._signer, this.publicAddress);
} else {
signed = await this.skipBundleClient.signBundle([txRaw], this.signer, this.publicAddress);
signed = await this._skipBundleClient.signBundle([txRaw], this._signer, this.publicAddress);
}
const res = await this.skipBundleClient.sendBundle(signed, 0, true);
const res = await this._skipBundleClient.sendBundle(signed, 0, true);
return res;
}

/**
*
*/
async queryMempool(): Promise<Mempool> {
const mempoolResult = await this.httpClient.execute(createJsonRpcRequest("unconfirmed_txs"));
const mempoolResult = await this._httpClient.execute(createJsonRpcRequest("unconfirmed_txs"));
return mempoolResult.result;
}

/**
*
*/
async reset(): Promise<void> {
const { accountNumber, sequence } = await this._signingCWClient.getSequence(this._account.address);
this._accountNumber = accountNumber;
this._sequence = sequence;
}
}

export default CosmjsAdapter;
Loading

0 comments on commit 321afcd

Please sign in to comment.