Skip to content

Commit

Permalink
feat: add function uploadWasm
Browse files Browse the repository at this point in the history
  • Loading branch information
phamnam1805 committed Oct 14, 2022
1 parent 47c6911 commit 4df128c
Showing 1 changed file with 61 additions and 7 deletions.
68 changes: 61 additions & 7 deletions src/lib/wallet/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
SigningCosmWasmClient,
SigningCosmWasmClientOptions,
UploadResult,
} from '@cosmjs/cosmwasm-stargate';
import {
DirectSecp256k1HdWallet,
Expand All @@ -9,14 +10,20 @@ import {
} from '@cosmjs/proto-signing';
import { AccountData } from '@cosmjs/proto-signing/build/signer';
import {
calculateFee,
GasPrice,
SigningStargateClient,
SigningStargateClientOptions,
StdFee,
} from '@cosmjs/stargate';
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';
import { HttpEndpoint } from '@cosmjs/tendermint-rpc/build/rpcclients';

import Cosm from '../cosm';
import { provider } from '../providers';

import { TendermintBatchClient } from '../tendermint-rpc/tendermintbatchclient';
const defaultUploadGas = 2500000;
const defaultInitGas = 1000000;
const defaultExecGas = 500000;
const defaultGasPrice = 0.25;

export default OfflineSigner;

Expand All @@ -30,6 +37,7 @@ export class Wallet {
private _cosmWasmSigner: SigningCosmWasmClient;
private _stargateSigner: SigningStargateClient;
private _account: AccountData;
private _denom: string;

public static async getWalletFromMnemonic(
provider: provider,
Expand All @@ -52,7 +60,13 @@ export class Wallet {
options.stargateOptions
);
const [account] = await wallet.getAccounts();
return new Wallet(wallet, account, cosmWasmClient, stargateClient);
return new Wallet(
wallet,
account,
cosmWasmClient,
stargateClient,
await getDenom(provider)
);
}

public static async getWalletsFromMnemonic(
Expand Down Expand Up @@ -87,7 +101,13 @@ export class Wallet {
options.stargateOptions
);
results.push(
new Wallet(wallets, accounts[i], cosmWasmClient, stargateClient)
new Wallet(
wallets,
accounts[i],
cosmWasmClient,
stargateClient,
await getDenom(provider)
)
);
}
return results;
Expand All @@ -113,7 +133,13 @@ export class Wallet {
options.stargateOptions
);
results.push(
new Wallet(signer, accounts[i], cosmWasmClient, stargateClient)
new Wallet(
signer,
accounts[i],
cosmWasmClient,
stargateClient,
await getDenom(provider)
)
);
}
return results;
Expand All @@ -123,15 +149,43 @@ export class Wallet {
signer: OfflineSigner,
account: AccountData,
cosmWasmSigner: SigningCosmWasmClient,
stargateSigner: SigningStargateClient
stargateSigner: SigningStargateClient,
denom: string
) {
this._signer = signer;
this._account = account;
this._cosmWasmSigner = cosmWasmSigner;
this._stargateSigner = stargateSigner;
this._denom = denom;
}

public async uploadWasm(wasmCode: Uint8Array, fee?: StdFee, memo?: string): Promise<UploadResult> {
fee = fee == null ? this.getFee(defaultUploadGas, defaultGasPrice) : fee;
return await this._cosmWasmSigner.upload(this.address, wasmCode, fee, memo);
}

public async deloyContractFromCodeId(codeId: number){

}

public getFee(gas: number, gasPrice: number): StdFee {
return calculateFee(
gas,
GasPrice.fromString(gasPrice.toString() + this._denom)
);
}

get address(): string {
return this._account.address;
}

get denom(): string {
return this._denom;
}
}

async function getDenom(provider: provider): Promise<string> {
const cosm = new Cosm(provider);
const queryRs = await cosm.cosmos.mint.query.Params({});
return queryRs.params.mintDenom;
}

0 comments on commit 4df128c

Please sign in to comment.