Skip to content

Commit

Permalink
Merge pull request #81 from spoo-bar/import-all-contracts
Browse files Browse the repository at this point in the history
feat: Import all contracts
  • Loading branch information
spoo-bar authored Dec 14, 2023
2 parents 7400e94 + 53f54a5 commit b9efd42
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 2 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ All notable changes to the Cosmy Wasmy extension will be documented in this file
### Security
-->

## [Unreleased]

### Added

- For localnet chains, all contracts on the chain can be imported in single click.

### Changed

### Deprecated

### Removed

### Fixed

### Security


## [v2.3.1] - 07 July 2023

### Removed
Expand Down
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "cosmy-wasmy",
"displayName": "Cosmy Wasmy",
"description": "wibbly wobbly cosmy wasmy tool to interact with cosmwasm smart contracts",
"version": "2.3.1",
"version": "2.3.2",
"preview": false,
"publisher": "Spoorthi",
"icon": "media/icon.png",
Expand Down Expand Up @@ -106,6 +106,12 @@
"category": "Cosmy Wasmy",
"icon": "$(add)"
},
{
"command": "cosmy-wasmy.addContractsAll",
"title": "%cosmy-wasmy.addContractsAll.title%",
"category": "Cosmy Wasmy",
"icon": "$(expand-all)"
},
{
"command": "cosmy-wasmy.deleteContract",
"title": "%cosmy-wasmy.deleteContract.title%",
Expand Down Expand Up @@ -645,6 +651,11 @@
"when": "view == contract",
"group": "navigation"
},
{
"command": "cosmy-wasmy.addContractsAll",
"when": "view == contract",
"group": "navigation"
},
{
"command": "cosmy-wasmy.history",
"when": "view == query || view == execute",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"cosmy-wasmy.sendTokens.title": "Send Tokens",
"cosmy-wasmy.refreshAccount.title": "Refresh Account View",
"cosmy-wasmy.addContract.title": "Import Contract",
"cosmy-wasmy.addContractsAll.title": "Import Contracts - All",
"cosmy-wasmy.deleteContract.title": "Delete Contract",
"cosmy-wasmy.updateContractAdmin.title": "Update Admin",
"cosmy-wasmy.clearContractAdmin.title": "Clear Admin",
Expand Down
51 changes: 51 additions & 0 deletions src/commands/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { WasmVmPanel } from '../views/WasmVmPanel';
export class ContractCmds {
public static async Register(context: vscode.ExtensionContext) {
this.registerAddContractCmd(context, contractViewProvider);
this.registerAddAllContractsCmd(context, contractViewProvider);
this.registerSelectContractCmd(context);
this.registerDeleteContractCmd(context, contractViewProvider);
this.registerUpdateContractAdminCmd(context);
Expand Down Expand Up @@ -73,6 +74,56 @@ 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();
}
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
}));
}
});
context.subscriptions.push(disposable);

function importAllContracts() {
vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: vscode.l10n.t("Fetching all contracts on the chain - {chain}", {
chain: global.workspaceChain.configName
}),
cancellable: false
}, (progress, token) => {
token.onCancellationRequested(() => { });
progress.report({ message: '' });
return new Promise((resolve, reject) => {
CosmwasmAPI.GetAllContracts().then(resContracts => {
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);
});
});
})
}
}

private static registerSelectContractCmd(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand('cosmy-wasmy.selectContract', (contract: Contract) => {
Workspace.SetSelectedContract(contract);
Expand Down
14 changes: 14 additions & 0 deletions src/helpers/Cosmwasm/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ export class CosmwasmAPI {
return contract;
}

public static async GetAllContracts(): Promise<Contract[]> {
let importedContracts = new Array<Contract>();
let client = await Cosmwasm.GetQueryClient();
const codes = await client.getCodes();
for (let code of codes) {
const contracts = await client.getContracts(code.id);
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
2 changes: 1 addition & 1 deletion src/models/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Account extends vscode.TreeItem {
try {
account.balance = await CosmwasmAPI.GetBalance(account.address);
}
catch {
catch (err) {
account.balance = "NaN"; // lol but yea todo - when cant fetch balance, show that balance was not fetched. until then making it seem like js is being naughty 😈
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/models/Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export class Contract extends vscode.TreeItem {
ExtData.SaveContracts(context, contracts);
}

public static AddManyContract(context: vscode.Memento, newContracts: Contract[]) {
let contracts = this.GetContracts(context);
contracts.push(...newContracts);
ExtData.SaveContracts(context, contracts);
}

public static DeleteContract(context: vscode.Memento, contract: Contract) {
let contracts = this.GetContracts(context).filter(c => c.contractAddress != contract.contractAddress);
ExtData.SaveContracts(context, contracts);
Expand Down

0 comments on commit b9efd42

Please sign in to comment.