Skip to content

Commit

Permalink
closes #53
Browse files Browse the repository at this point in the history
  • Loading branch information
spoo-bar committed Jun 18, 2024
1 parent 608defd commit d630806
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ All notable changes to the Cosmy Wasmy extension will be documented in this file
### Security
-->

## [Unreleased]

### Added

- For testnet and mainnet chain, all contracts by the selected account can be imported in a single click

### Changed

### Deprecated

### Removed

### Fixed

### Security

## [v2.4.0] - 12 January 2024

### Added
Expand Down
50 changes: 44 additions & 6 deletions src/commands/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,10 @@ export class ContractCmds {
private static registerAddAllContractsCmd(context: vscode.ExtensionContext, contractViewProvider: ContractDataProvider) {
let disposable = vscode.commands.registerCommand('cosmy-wasmy.addContractsAll', () => {
if (global.workspaceChain.chainEnvironment == "localnet") {
importAllContracts();
importAllContracts(); // imports all contracts on the chain
}
else {
vscode.window.showErrorMessage(vscode.l10n.t("Sorry! This command is only available for localnet chains. {chain} is marked as '{type}'", {
chain: global.workspaceChain.configName,
type: global.workspaceChain.chainEnvironment
}));
importUserContracts(); // imports all the contracts on the chain for given user
}
});
context.subscriptions.push(disposable);
Expand All @@ -100,7 +97,48 @@ export class ContractCmds {
progress.report({ message: '' });
return new Promise((resolve, reject) => {
CosmwasmAPI.GetAllContracts().then(resContracts => {
let contractsToAdd:Contract[] = [];
let contractsToAdd: Contract[] = [];
resContracts.forEach(contract => {
if (!Contract.ContractAddressExists(context.globalState, contract.contractAddress)) {
contractsToAdd.push(contract);
}
});
Contract.AddManyContract(context.globalState, contractsToAdd);
vscode.window.showInformationMessage(vscode.l10n.t("Added {count} contracts!", {
count: contractsToAdd.length
}));
const contracts = Contract.GetContracts(context.globalState);
contractViewProvider.refresh(contracts);
resolve(contracts);
}).catch(err => {
vscode.window.showErrorMessage(vscode.l10n.t("Could not import contracts: {err}", {
err: err
}));
reject(err);
});
});
})
}

function importUserContracts() {
const account = Workspace.GetSelectedAccount();
if (!account) {
vscode.window.showErrorMessage(vscode.l10n.t("No account selected to import their contracts. Select an account from the Accounts view."));
return;
}
vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: vscode.l10n.t("Fetching contracts on the chain - {chain} of {addr}", {
chain: global.workspaceChain.configName,
addr: account.address
}),
cancellable: false
}, (progress, token) => {
token.onCancellationRequested(() => { });
progress.report({ message: '' });
return new Promise((resolve, reject) => {
CosmwasmAPI.GetUserContracts(account.address).then(resContracts => {
let contractsToAdd: Contract[] = [];
resContracts.forEach(contract => {
if (!Contract.ContractAddressExists(context.globalState, contract.contractAddress)) {
contractsToAdd.push(contract);
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/cosmwasm/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ export class CosmwasmAPI {
return importedContracts;
}

public static async GetUserContracts(userAddress: string): Promise<Contract[]> {
let importedContracts = new Array<Contract>();
let client = await Cosmwasm.GetQueryClient();
const contracts = await client.getContractsByCreator(userAddress);
for (let contract of contracts) {
let contractInfo = await client.getContract(contract);
importedContracts.push(new Contract(contractInfo.label, contractInfo.address, contractInfo.codeId, contractInfo.creator, global.workspaceChain.configName));
}
return importedContracts;
}

public static async GetBalance(address: string): Promise<string> {
let client = await Cosmwasm.GetQueryClient();
let denom = global.workspaceChain.chainDenom;
Expand Down

1 comment on commit d630806

@elix1er
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep up the grind

Please sign in to comment.