swisstronik deploy contract on hardhat
Install
npm install --save-dev @nomicfoundation/hardhat-toolbox
Open File : hardhat.config.js
dan Paste
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
module.exports = {
solidity: "0.8.19",
networks: {
swisstronik: {
url: "https://json-rpc.testnet.swisstronik.com/",
accounts: [`0x${process.env.PRIVATE_KEY}`],
},
},
};
npm install dotenv
Create File .env
PRIVATE_KEY=Your-Private-Key-Paste-Here-and-save
Open File Contract > Edit File name Lock.sol
to Hello_swtr.sol
delete all and paste:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;
//This contract is only intended for testing purposes
contract Swisstronik {
string private message;
/**
* @dev Constructor is used to set the initial message for the contract
* @param _message the message to associate with the message variable.
*/
constructor(string memory _message) payable{
message = _message;
}
/**
* @dev setMessage() updates the stored message in the contract
* @param _message the new message to replace the existing one
*/
function setMessage(string memory _message) public {
message = _message;
}
/**
* @dev getMessage() retrieves the currently stored message in the contract
* @return The message associated with the contract
*/
function getMessage() public view returns(string memory){
return message;
}
}
Compile Contract
npx hardhat compile
Create New Folder Scripts
> Create 3 File
deploy.js
setMessage.js
getMessage.js
Create File deploy.js
:
deploy.js
const hre = require("hardhat");
async function main() {
/**
* @dev make sure the first argument has the same name as your contract in the Hello_swtr.sol file
* @dev the second argument must be the message we want to set in the contract during the deployment process
*/
const contract = await hre.ethers.deployContract("Swisstronik", ["Hello Swisstronik!!"]);
await contract.waitForDeployment();
console.log(`Swisstronik contract deployed to ${contract.target}`);
}
//DEFAULT BY HARDHAT:
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Deploy:
npx hardhat run scripts/deploy.js --network swisstronik
Create File setMessage.js
setMessage.js
const hre = require("hardhat");
const { encryptDataField, decryptNodeResponse } = require("@swisstronik/utils");
const sendShieldedTransaction = async (signer, destination, data, value) => {
const rpclink = hre.network.config.url;
const [encryptedData] = await encryptDataField(rpclink, data);
return await signer.sendTransaction({
from: signer.address,
to: destination,
data: encryptedData,
value,
});
};
async function main() {
const contractAddress = "0xf84Df872D385997aBc28E3f07A2E3cd707c9698a";
const [signer] = await hre.ethers.getSigners();
const contractFactory = await hre.ethers.getContractFactory("Swisstronik");
const contract = contractFactory.attach(contractAddress);
const functionName = "setMessage";
const messageToSet = "Hello Swisstronik!!";
const setMessageTx = await sendShieldedTransaction(signer, contractAddress, contract.interface.encodeFunctionData(functionName, [messageToSet]), 0);
await setMessageTx.wait();
console.log("Transaction Receipt: ", setMessageTx);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Deploy:
npx hardhat run scripts/setMessage.js --network swisstronik
Create File getMessage.js
getMessage.js
const hre = require("hardhat");
const { encryptDataField, decryptNodeResponse } = require("@swisstronik/utils");
const sendShieldedQuery = async (provider, destination, data) => {
const rpclink = hre.network.config.url;
const [encryptedData, usedEncryptedKey] = await encryptDataField(rpclink, data);
const response = await provider.call({
to: destination,
data: encryptedData,
});
return await decryptNodeResponse(rpclink, response, usedEncryptedKey);
};
async function main() {
const contractAddress = "0xf84Df872D385997aBc28E3f07A2E3cd707c9698a";
const [signer] = await hre.ethers.getSigners();
const contractFactory = await hre.ethers.getContractFactory("Swisstronik");
const contract = contractFactory.attach(contractAddress);
const functionName = "getMessage";
const responseMessage = await sendShieldedQuery(signer.provider, contractAddress, contract.interface.encodeFunctionData(functionName));
console.log("Decoded response:", contract.interface.decodeFunctionResult(functionName, responseMessage)[0]);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Deploy:
npx hardhat run scripts/getMessage.js --network swisstronik
DEPLOY YOUR SCRIPT TO GITHUB
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/Change-your-repository.git
git branch -M main
git push -u origin main