Skip to content

Commit

Permalink
fix typescript errors. integrate upload and instantiate contract
Browse files Browse the repository at this point in the history
  • Loading branch information
ltfschoen committed Oct 6, 2024
1 parent b4feefc commit d6ab7dd
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 161 deletions.
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
6 changes: 0 additions & 6 deletions packages/secret-instantiate-contract/.env-sample

This file was deleted.

18 changes: 0 additions & 18 deletions packages/secret-instantiate-contract/README.md

This file was deleted.

16 changes: 0 additions & 16 deletions packages/secret-instantiate-contract/package.json

This file was deleted.

61 changes: 0 additions & 61 deletions packages/secret-instantiate-contract/src/index.ts

This file was deleted.

29 changes: 0 additions & 29 deletions packages/secret-instantiate-contract/tsconfig.json

This file was deleted.

3 changes: 2 additions & 1 deletion packages/secret-upload-contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
123 changes: 95 additions & 28 deletions packages/secret-upload-contract/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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()
}
Expand Down

0 comments on commit d6ab7dd

Please sign in to comment.