From 0ea99ecea65e51e197ac8ecb3693c9377b1f1563 Mon Sep 17 00:00:00 2001 From: Spoorthi Satheesha <9302666+spoo-bar@users.noreply.github.com> Date: Wed, 13 Dec 2023 18:45:45 +0000 Subject: [PATCH 1/4] init import all contracts on a chain --- package.json | 13 ++++++++- package.nls.json | 1 + src/commands/contract.ts | 54 +++++++++++++++++++++++++++++++++++++ src/helpers/Cosmwasm/API.ts | 14 ++++++++++ src/models/Account.ts | 2 +- 5 files changed, 82 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index afe7a43..7316eb4 100644 --- a/package.json +++ b/package.json @@ -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", @@ -106,6 +106,12 @@ "category": "Cosmy Wasmy", "icon": "$(add)" }, + { + "command": "cosmy-wasmy.addAllContracts", + "title": "%cosmy-wasmy.addAllContracts.title%", + "category": "Cosmy Wasmy", + "icon": "$(cloud-download)" + }, { "command": "cosmy-wasmy.deleteContract", "title": "%cosmy-wasmy.deleteContract.title%", @@ -645,6 +651,11 @@ "when": "view == contract", "group": "navigation" }, + { + "command": "cosmy-wasmy.addAllContracts", + "when": "view == contract", + "group": "navigation" + }, { "command": "cosmy-wasmy.history", "when": "view == query || view == execute", diff --git a/package.nls.json b/package.nls.json index 954be34..28b008f 100644 --- a/package.nls.json +++ b/package.nls.json @@ -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.addAllContracts.title": "Import All Contracts", "cosmy-wasmy.deleteContract.title": "Delete Contract", "cosmy-wasmy.updateContractAdmin.title": "Update Admin", "cosmy-wasmy.clearContractAdmin.title": "Clear Admin", diff --git a/src/commands/contract.ts b/src/commands/contract.ts index 057e81d..b90ccb5 100644 --- a/src/commands/contract.ts +++ b/src/commands/contract.ts @@ -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); @@ -73,6 +74,59 @@ export class ContractCmds { } } + private static registerAddAllContractsCmd(context: vscode.ExtensionContext, contractViewProvider: ContractDataProvider) { + let disposable = vscode.commands.registerCommand('cosmy-wasmy.addAllContracts', () => { + CosmwasmAPI.GetAllContracts().then(contracts => { + console.log("done") + }) + if (global.workspaceChain.chainEnvironment == "localnet") { + + } + else { + vscode.window.showErrorMessage(vscode.l10n.t("Sorry! This command is only available for localnet chains")); + } + }); + context.subscriptions.push(disposable); + + function importContract(contractAddr: string) { + if (!Contract.ContractAddressExists(context.globalState, contractAddr)) { + vscode.window.withProgress({ + location: vscode.ProgressLocation.Notification, + title: vscode.l10n.t("Fetching the details for the contract - {addr}", { + addr: contractAddr + }), + cancellable: false + }, (progress, token) => { + token.onCancellationRequested(() => { }); + progress.report({ message: '' }); + return new Promise((resolve, reject) => { + CosmwasmAPI.GetContract(contractAddr).then(contract => { + Contract.AddContract(context.globalState, contract); + vscode.window.showInformationMessage(vscode.l10n.t("Added new contract: {codeId} - {label}", { + codeId: contract.codeId, + label: contract.label + })); + const contracts = Contract.GetContracts(context.globalState); + contractViewProvider.refresh(contracts); + resolve(contract); + }).catch(err => { + vscode.window.showErrorMessage(vscode.l10n.t("Could not import contract: {addr} - {err}", { + addr: contractAddr, + err: err + })); + reject(err); + }); + }); + }); + } + else { + vscode.window.showErrorMessage(vscode.l10n.t("Contract has already been imported: {addr}", { + addr: contractAddr + })); + } + } + } + private static registerSelectContractCmd(context: vscode.ExtensionContext) { let disposable = vscode.commands.registerCommand('cosmy-wasmy.selectContract', (contract: Contract) => { Workspace.SetSelectedContract(contract); diff --git a/src/helpers/Cosmwasm/API.ts b/src/helpers/Cosmwasm/API.ts index f2c3306..4cd7983 100644 --- a/src/helpers/Cosmwasm/API.ts +++ b/src/helpers/Cosmwasm/API.ts @@ -18,6 +18,20 @@ export class CosmwasmAPI { return contract; } + public static async GetAllContracts(): Promise { + let importedContracts = new Array(); + 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 { let client = await Cosmwasm.GetQueryClient(); let denom = global.workspaceChain.chainDenom; diff --git a/src/models/Account.ts b/src/models/Account.ts index 642fc00..cf43fe6 100644 --- a/src/models/Account.ts +++ b/src/models/Account.ts @@ -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 😈 } } From 788afb99e4d712d990b1c9708364b95a30750064 Mon Sep 17 00:00:00 2001 From: Spoorthi Satheesha <9302666+spoo-bar@users.noreply.github.com> Date: Thu, 14 Dec 2023 15:41:24 +0000 Subject: [PATCH 2/4] fixing title order --- package.json | 8 ++++---- package.nls.json | 2 +- src/commands/contract.ts | 13 +++++++------ 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 7316eb4..15fec4e 100644 --- a/package.json +++ b/package.json @@ -107,10 +107,10 @@ "icon": "$(add)" }, { - "command": "cosmy-wasmy.addAllContracts", - "title": "%cosmy-wasmy.addAllContracts.title%", + "command": "cosmy-wasmy.addContractsAll", + "title": "%cosmy-wasmy.addContractsAll.title%", "category": "Cosmy Wasmy", - "icon": "$(cloud-download)" + "icon": "$(expand-all)" }, { "command": "cosmy-wasmy.deleteContract", @@ -652,7 +652,7 @@ "group": "navigation" }, { - "command": "cosmy-wasmy.addAllContracts", + "command": "cosmy-wasmy.addContractsAll", "when": "view == contract", "group": "navigation" }, diff --git a/package.nls.json b/package.nls.json index 28b008f..30e2789 100644 --- a/package.nls.json +++ b/package.nls.json @@ -8,7 +8,7 @@ "cosmy-wasmy.sendTokens.title": "Send Tokens", "cosmy-wasmy.refreshAccount.title": "Refresh Account View", "cosmy-wasmy.addContract.title": "Import Contract", - "cosmy-wasmy.addAllContracts.title": "Import All Contracts", + "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", diff --git a/src/commands/contract.ts b/src/commands/contract.ts index b90ccb5..9675652 100644 --- a/src/commands/contract.ts +++ b/src/commands/contract.ts @@ -75,12 +75,9 @@ export class ContractCmds { } private static registerAddAllContractsCmd(context: vscode.ExtensionContext, contractViewProvider: ContractDataProvider) { - let disposable = vscode.commands.registerCommand('cosmy-wasmy.addAllContracts', () => { - CosmwasmAPI.GetAllContracts().then(contracts => { - console.log("done") - }) + 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")); @@ -88,7 +85,11 @@ export class ContractCmds { }); context.subscriptions.push(disposable); - function importContract(contractAddr: string) { + function importAllContracts() { + CosmwasmAPI.GetAllContracts().then(contracts => { + console.log("done") + }) + if (!Contract.ContractAddressExists(context.globalState, contractAddr)) { vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, From d37a07dd9eaba46cf69e042324d3d16a6a9105d2 Mon Sep 17 00:00:00 2001 From: Spoorthi Satheesha <9302666+spoo-bar@users.noreply.github.com> Date: Thu, 14 Dec 2023 16:28:00 +0000 Subject: [PATCH 3/4] importing only unimported contracts --- src/commands/contract.ts | 70 +++++++++++++++++++--------------------- src/models/Contract.ts | 6 ++++ 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/commands/contract.ts b/src/commands/contract.ts index 9675652..005fb2a 100644 --- a/src/commands/contract.ts +++ b/src/commands/contract.ts @@ -80,51 +80,47 @@ export class ContractCmds { importAllContracts(); } else { - vscode.window.showErrorMessage(vscode.l10n.t("Sorry! This command is only available for localnet chains")); + 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() { - CosmwasmAPI.GetAllContracts().then(contracts => { - console.log("done") - }) - - if (!Contract.ContractAddressExists(context.globalState, contractAddr)) { - vscode.window.withProgress({ - location: vscode.ProgressLocation.Notification, - title: vscode.l10n.t("Fetching the details for the contract - {addr}", { - addr: contractAddr - }), - cancellable: false - }, (progress, token) => { - token.onCancellationRequested(() => { }); - progress.report({ message: '' }); - return new Promise((resolve, reject) => { - CosmwasmAPI.GetContract(contractAddr).then(contract => { - Contract.AddContract(context.globalState, contract); - vscode.window.showInformationMessage(vscode.l10n.t("Added new contract: {codeId} - {label}", { - codeId: contract.codeId, - label: contract.label - })); - const contracts = Contract.GetContracts(context.globalState); - contractViewProvider.refresh(contracts); - resolve(contract); - }).catch(err => { - vscode.window.showErrorMessage(vscode.l10n.t("Could not import contract: {addr} - {err}", { - addr: contractAddr, - err: err - })); - reject(err); + 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); }); }); - } - else { - vscode.window.showErrorMessage(vscode.l10n.t("Contract has already been imported: {addr}", { - addr: contractAddr - })); - } + }) } } diff --git a/src/models/Contract.ts b/src/models/Contract.ts index f577857..8de1132 100644 --- a/src/models/Contract.ts +++ b/src/models/Contract.ts @@ -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); From 53f54a54f80243e6833dd07bafa52beee4fc2dce Mon Sep 17 00:00:00 2001 From: Spoorthi Satheesha <9302666+spoo-bar@users.noreply.github.com> Date: Thu, 14 Dec 2023 16:29:31 +0000 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5a6e17..926f21d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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