From d6ab7dd3373f6672e1865af1b26fd344bd37ee01 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Sun, 6 Oct 2024 03:05:47 +0200 Subject: [PATCH] fix typescript errors. integrate upload and instantiate contract --- .../my-counter-contract/tests/package.json | 4 +- .../secret-instantiate-contract/.env-sample | 6 - .../secret-instantiate-contract/README.md | 18 --- .../secret-instantiate-contract/package.json | 16 --- .../secret-instantiate-contract/src/index.ts | 61 --------- .../secret-instantiate-contract/tsconfig.json | 29 ----- packages/secret-upload-contract/package.json | 3 +- packages/secret-upload-contract/src/index.ts | 123 ++++++++++++++---- 8 files changed, 99 insertions(+), 161 deletions(-) delete mode 100644 packages/secret-instantiate-contract/.env-sample delete mode 100644 packages/secret-instantiate-contract/README.md delete mode 100644 packages/secret-instantiate-contract/package.json delete mode 100644 packages/secret-instantiate-contract/src/index.ts delete mode 100644 packages/secret-instantiate-contract/tsconfig.json diff --git a/packages/secret-contracts/my-counter-contract/tests/package.json b/packages/secret-contracts/my-counter-contract/tests/package.json index 40d453a..868ef3b 100644 --- a/packages/secret-contracts/my-counter-contract/tests/package.json +++ b/packages/secret-contracts/my-counter-contract/tests/package.json @@ -1,9 +1,9 @@ { "devDependencies": { - "@types/node": "^17.0.23", + "@types/node": "^22.7.4", "axios": "^0.26.1", "secretjs": "1.5.1", - "typescript": "^4.6.3" + "typescript": "^5.6.2" }, "dependencies": { "assert": "^2.0.0" diff --git a/packages/secret-instantiate-contract/.env-sample b/packages/secret-instantiate-contract/.env-sample deleted file mode 100644 index bc90ee1..0000000 --- a/packages/secret-instantiate-contract/.env-sample +++ /dev/null @@ -1,6 +0,0 @@ -# secret1682pvf3la9uq9d0utfupyg2j4md5w2207mqul3 -# note: from console.log(secretjs) -WALLET_MNEMONIC_LOCAL="juice math fringe believe garbage over avocado demand devote afford organ analyst sheriff bless bird decrease empower limb boring family find author call wrestle" -WALLET_MNEMONIC_TESTNET="" -ENDPOINT_TESTNET="https://api.pulsar3.scrttestnet.com" -ENDPOINT_LOCAL="" diff --git a/packages/secret-instantiate-contract/README.md b/packages/secret-instantiate-contract/README.md deleted file mode 100644 index f7772f3..0000000 --- a/packages/secret-instantiate-contract/README.md +++ /dev/null @@ -1,18 +0,0 @@ -# Instantiate Contract - -Install [NVM](https://github.com/nvm-sh/nvm?tab=readme-ov-file#install--update-script) - -Install Node.js and Lerna -```bash -nvm use -npm install --global lerna -``` - -Run the following commands in this directory: -```bash -yarn -yarn run build -yarn run start -``` - -View the output. diff --git a/packages/secret-instantiate-contract/package.json b/packages/secret-instantiate-contract/package.json deleted file mode 100644 index 919a54a..0000000 --- a/packages/secret-instantiate-contract/package.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "secret-instantiate-contract", - "version": "0.0.1", - "main": "index.js", - "private": true, - "scripts": { - "build": "./node_modules/.bin/tsc --build", - "clean": "./node_modules/.bin/tsc --clean", - "start": "yarn build && node ./dist/index.js" - }, - "dependencies": { - "dotenv": "^16.4.5", - "secretjs": "^1.12.5", - "typescript": "^5.6.2" - } -} diff --git a/packages/secret-instantiate-contract/src/index.ts b/packages/secret-instantiate-contract/src/index.ts deleted file mode 100644 index e7bbb64..0000000 --- a/packages/secret-instantiate-contract/src/index.ts +++ /dev/null @@ -1,61 +0,0 @@ -import dotenv from "dotenv"; -dotenv.config(); -import { SecretNetworkClient, Wallet } from "secretjs"; -import * as fs from "fs"; -import path from 'path'; - -// const wallet = new Wallet(process.env.WALLET_MNEMONIC_LOCAL); -const wallet = new Wallet(process.env.WALLET_MNEMONIC_TESTNET); -const rootPath = path.resolve(__dirname, '../../../'); // relative to ./dist -const contract_wasm = fs.readFileSync(`${rootPath}/packages/secret-contracts/my-counter-contract/contract.wasm`); - -async function main () { - const secretjs = new SecretNetworkClient({ - // chainId: "secretdev-1", - // url: process.env.ENDPOINT_LOCAL || "", - chainId: "pulsar-3", - url: process.env.ENDPOINT_TESTNET || "", - wallet: wallet, - walletAddress: wallet.address - }); - - // Add your contract codeId here - let codeId = "" - - // Add your contractCodeHash here - let contractCodeHash = "" - - let instantiate_contract = async () => { - //instantiate message is empty in this example. If your contract needs to be instantiated with additional variables, be sure to include them. - - const initMsg = {}; - let tx = await secretjs.tx.compute.instantiateContract( - { - code_id: codeId, - sender: wallet.address, - code_hash: contractCodeHash, - init_msg: initMsg, - label: "Demo" + Math.ceil(Math.random() * 10000), - }, - { - gasLimit: 400_000, - } - ); - - //Find the contract_address in the logs - const contractAddress = tx.arrayLog.find( - (log) => log.type === "message" && log.key === "contract_address" - ).value; - - console.log(contractAddress); - }; - - instantiate_contract(); - - process.exit() -} - -main().catch((error) => { - console.error(error); - process.exit(-1); -}); diff --git a/packages/secret-instantiate-contract/tsconfig.json b/packages/secret-instantiate-contract/tsconfig.json deleted file mode 100644 index 126932a..0000000 --- a/packages/secret-instantiate-contract/tsconfig.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "extends": "../../tsconfig.json", - "include": ["./src/**/*.d.ts", "./src/**/*.js", "./src/**/*.ts"], - "exclude": ["node_modules"], - "compilerOptions": { - - /* Projects */ - "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ - /* Language and Environment */ - "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ - /* Modules */ - "module": "commonjs", /* Specify what module code is generated. */ - "rootDir": "./src", /* Specify the root folder within your source files. */ - "outDir": "./dist", /* Specify an output folder for all emitted files. */ - "noEmit": false, /* Disable emitting files from a compilation. */ - - /* Interop Constraints */ - "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ - "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ - "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ - "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ - - /* Type Checking */ - "strict": true, /* Enable all strict type-checking options. */ - - /* Completeness */ - "skipLibCheck": true /* Skip type checking all .d.ts files. */ - } -} diff --git a/packages/secret-upload-contract/package.json b/packages/secret-upload-contract/package.json index 1a2e6e1..93b12bc 100644 --- a/packages/secret-upload-contract/package.json +++ b/packages/secret-upload-contract/package.json @@ -11,6 +11,7 @@ "dependencies": { "dotenv": "^16.4.5", "secretjs": "^1.12.5", - "typescript": "^5.6.2" + "typescript": "^5.6.2", + "@types/node": "^22.7.4" } } diff --git a/packages/secret-upload-contract/src/index.ts b/packages/secret-upload-contract/src/index.ts index a652478..26cda33 100644 --- a/packages/secret-upload-contract/src/index.ts +++ b/packages/secret-upload-contract/src/index.ts @@ -16,7 +16,20 @@ console.log('wallet: ', wallet); const rootPath = path.resolve(__dirname, '../../../'); // relative to ./dist console.log('rootPath', rootPath) -const contract_wasm = fs.readFileSync(`${rootPath}/packages/secret-contracts/my-counter-contract/contract.wasm`); +const contract_wasm: any = fs.readFileSync(`${rootPath}/packages/secret-contracts/my-counter-contract/contract.wasm`); + +const gatewayAddress = "secret10ex7r7c4y704xyu086lf74ymhrqhypayfk7fkj"; + +const gatewayHash = + "012dd8efab9526dec294b6898c812ef6f6ad853e32172788f54ef3c305c1ecc5"; + +const gatewayPublicKey = + "0x046d0aac3ef10e69055e934ca899f508ba516832dc74aa4ed4d741052ed5a568774d99d3bfed641a7935ae73aac8e34938db747c2f0e8b2aa95c25d069a575cc8b"; + +const gatewayPublicKeyBytes = Buffer.from( + gatewayPublicKey.substring(2), + "hex" +).toString("base64"); async function main () { const secretjs = new SecretNetworkClient({ @@ -37,33 +50,87 @@ async function main () { console.log('balance: ', balance); - // let upload_contract = async () => { - // let tx = await secretjs.tx.compute.storeCode( - // { - // sender: wallet.address, - // wasm_byte_code: contract_wasm, - // source: "", - // builder: "", - // }, - // { - // gasLimit: 4_000_000, - // } - // ); - - // const codeId = Number( - // tx.arrayLog.find((log) => log.type === "message" && log.key === "code_id") - // .value - // ); - - // console.log("codeId: ", codeId); - - // const contractCodeHash = ( - // await secretjs.query.compute.codeHashByCodeId({ code_id: codeId }) - // ).code_hash; - // console.log(`Contract hash: ${contractCodeHash}`); - // }; - - // upload_contract(); + type CODE_PARAMS = { + codeId: String | undefined, + contractCodeHash: String | undefined, + }; + + let upload_contract = async () => { + console.log("Starting deployment..."); + + let codeId: String | undefined; + let contractCodeHash: String | undefined; + let contractAddress; + + let tx = await secretjs.tx.compute.storeCode( + { + sender: wallet.address, + wasm_byte_code: contract_wasm, + source: "", + builder: "", + }, + { + gasLimit: 5_000_000, + } + ); + + codeId = String( + tx?.arrayLog?.find((log) => log?.type === "message" && log?.key === "code_id")?.value + ); + + console.log("codeId: ", codeId); + + contractCodeHash = ( + await secretjs.query.compute.codeHashByCodeId({ code_id: codeId.toString() }) + ).code_hash; + console.log(`CODE_HASH: ${contractCodeHash}`); + + return { + codeId, + contractCodeHash + } + }; + + let instantiate_contract = async (params: CODE_PARAMS) => { + if (!params.codeId || !params.contractCodeHash) { + throw new Error("codeId or contractCodeHash is not set."); + } + console.log("Instantiating contract..."); + + let initMsg = { + gateway_address: gatewayAddress, + gateway_hash: gatewayHash, + gateway_key: gatewayPublicKeyBytes, + }; + let tx = await secretjs.tx.compute.instantiateContract( + { + code_id: params.codeId.toString(), + sender: wallet.address, + code_hash: params.contractCodeHash.toString(), + init_msg: initMsg, + label: "Encrypt " + Math.ceil(Math.random() * 10000), + }, + { + gasLimit: 400_000, + } + ); + + //Find the contract_address in the logs + const contractAddress = tx?.arrayLog?.find( + (log) => log?.type === "message" && log?.key === "contract_address" + )?.value; + + console.log("SECRET_ADDRESS: ", contractAddress); + }; + + // Chain the execution using promises + upload_contract() + .then((res) => { + instantiate_contract(res); + }) + .catch((error) => { + console.error("Error:", error); + }); process.exit() }