usingSecretNET;usingSecretNET.Common;usingSecretNET.Common.Storage;usingSecretNET.Tx;// Select a storage provider for the wallet// Docs: https://github.com/0xxCodemonkey/SecretNET#creating--initializing-the-walletvar storageProvider =newAesEncryptedFileStorage("","SuperSecurePassword");var createWalletOptions =newCreateWalletOptions(storageProvider);// Import wallet from mnemonic phrase// Use key created snippet "Create a new Wallet"Wallet wallet =null;if (awaitstorageProvider.HasPrivateKey()){var storedMnemonic =awaitstorageProvider.GetFirstMnemonic();Console.WriteLine("Use stored mnemonic: "+ storedMnemonic); wallet =awaitWallet.Create(storedMnemonic, options: createWalletOptions);Console.WriteLine("wallet.Address: "+wallet.Address);}// get infos from https://docs.scrt.network/secret-network-documentation/development/connecting-to-the-networkvar gprcUrl ="https://grpc.testnet.secretsaturn.net";var chainId ="pulsar-3";// Create a connection to Secret Network node// Pass in a wallet that can sign transactions// Docs: https://github.com/0xxCodemonkey/SecretNET#creating--initializing-the-walletvar secretClient =newSecretNetworkClient(gprcUrl, chainId, wallet: wallet);// Upload the wasm of a simple contractbyte[] wasmByteCode =File.ReadAllBytes(@"mysimplecounter.wasm.gz");// MsgStoreCodevar msgStoreCodeCounter =newMsgStoreCode( wasmByteCode, source:"",// Source is a valid absolute HTTPS URI to the contract's source code, optional builder:""// Builder is a valid docker image name with tag, optional );var storeCodeResponse = await secretClient.Tx.Compute.StoreCode(msgStoreCodeCounter, new TxOptions() { GasLimit = 2_000_000 });
var codeId =storeCodeResponse.Response.CodeId;// contract hash, useful for contract compositionvar contractCodeHash =awaitsecretClient.Query.Compute.GetCodeHashByCodeId(codeId);// Create an instance of the Counter contract, providing a starting countvar msgInitContract =newMsgInstantiateContract( codeId: codeId, label:$"My Counter {codeId}", initMsg:new { count =100 }, codeHash: contractCodeHash); // optional but way fastervar initContractResponse = await secretClient.Tx.Compute.InstantiateContract(msgInitContract, new TxOptions() { GasLimit = 200_000 });
//Find the contract_address in the logsvar contractAddress =initContractResponse?.Response?.Address;var msgExecuteContract =newMsgExecuteContract( contractAddress: contractAddress, msg:new { increment =new { } }, codeHash: contractCodeHash);var tx =awaitsecretClient.Tx.Compute.ExecuteContract(msgExecuteContract,newTxOptions() { GasLimit =200_000 });