From 01371286d057fa23ca435e323a5051b817292bf8 Mon Sep 17 00:00:00 2001 From: emochka2007 Date: Wed, 8 May 2024 03:36:45 +0300 Subject: [PATCH] config and dynamic generation of contracts --- chain-api/hardhat.config.ts | 1 + chain-api/package-lock.json | 36 +++++++++++++++ chain-api/package.json | 1 + chain-api/src/app.module.ts | 9 +++- chain-api/src/hardhat/contracts/Salaries.sol | 10 +---- .../src/hardhat/module/hardhat.service.ts | 45 ++++++++++++------- chain-api/tsconfig.json | 6 ++- 7 files changed, 79 insertions(+), 29 deletions(-) diff --git a/chain-api/hardhat.config.ts b/chain-api/hardhat.config.ts index 7979d1c..21a1c97 100644 --- a/chain-api/hardhat.config.ts +++ b/chain-api/hardhat.config.ts @@ -14,6 +14,7 @@ const config = { paths: { sources: './src/hardhat/contracts', // tests: './src/hardhat/test', + ignition: './src/hardhat/ignition', cache: './src/hardhat/cache', artifacts: './src/hardhat/artifacts', }, diff --git a/chain-api/package-lock.json b/chain-api/package-lock.json index 59d150d..235743e 100644 --- a/chain-api/package-lock.json +++ b/chain-api/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@chainlink/contracts": "^1.1.0", "@nestjs/common": "^10.0.0", + "@nestjs/config": "^3.2.2", "@nestjs/core": "^10.0.0", "@nestjs/mapped-types": "*", "@nestjs/platform-express": "^10.0.0", @@ -3595,6 +3596,33 @@ } } }, + "node_modules/@nestjs/config": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/@nestjs/config/-/config-3.2.2.tgz", + "integrity": "sha512-vGICPOui5vE6kPz1iwQ7oCnp3qWgqxldPmBQ9onkVoKlBtyc83KJCr7CjuVtf4OdovMAVcux1d8Q6jglU2ZphA==", + "dependencies": { + "dotenv": "16.4.5", + "dotenv-expand": "10.0.0", + "lodash": "4.17.21", + "uuid": "9.0.1" + }, + "peerDependencies": { + "@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0", + "rxjs": "^7.1.0" + } + }, + "node_modules/@nestjs/config/node_modules/uuid": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.1.tgz", + "integrity": "sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/@nestjs/core": { "version": "10.3.8", "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-10.3.8.tgz", @@ -7537,6 +7565,14 @@ "url": "https://dotenvx.com" } }, + "node_modules/dotenv-expand": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-10.0.0.tgz", + "integrity": "sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A==", + "engines": { + "node": ">=12" + } + }, "node_modules/eastasianwidth": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", diff --git a/chain-api/package.json b/chain-api/package.json index e5d6f4a..f0e7323 100644 --- a/chain-api/package.json +++ b/chain-api/package.json @@ -22,6 +22,7 @@ "dependencies": { "@chainlink/contracts": "^1.1.0", "@nestjs/common": "^10.0.0", + "@nestjs/config": "^3.2.2", "@nestjs/core": "^10.0.0", "@nestjs/mapped-types": "*", "@nestjs/platform-express": "^10.0.0", diff --git a/chain-api/src/app.module.ts b/chain-api/src/app.module.ts index 93c68e0..7185a27 100644 --- a/chain-api/src/app.module.ts +++ b/chain-api/src/app.module.ts @@ -3,9 +3,16 @@ import { AppController } from './app.controller'; import { AppService } from './app.service'; import { ContractFactoryModule } from './contract-factory/contract-factory.module'; import { ContractInteractModule } from './contract-interact/contract-interact.module'; +import { ConfigModule } from '@nestjs/config'; @Module({ - imports: [ContractFactoryModule, ContractInteractModule], + imports: [ + ConfigModule.forRoot({ + isGlobal: true, + }), + ContractFactoryModule, + ContractInteractModule, + ], controllers: [AppController], providers: [AppService], }) diff --git a/chain-api/src/hardhat/contracts/Salaries.sol b/chain-api/src/hardhat/contracts/Salaries.sol index ac4c346..5e5d7b7 100644 --- a/chain-api/src/hardhat/contracts/Salaries.sol +++ b/chain-api/src/hardhat/contracts/Salaries.sol @@ -1,25 +1,17 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.7; -import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol"; +import {AggregatorV3Interface} from '@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol'; contract Salaries { AggregatorV3Interface internal dataFeed; - /** - * Network: Sepolia - * Aggregator: BTC/USD - * Address: 0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43 - */ constructor() { dataFeed = AggregatorV3Interface( 0xF0d50568e3A7e8259E16663972b11910F89BD8e7 ); } - /** - * Returns the latest answer. - */ function getChainlinkDataFeedLatestAnswer() public view returns (int) { // prettier-ignore ( diff --git a/chain-api/src/hardhat/module/hardhat.service.ts b/chain-api/src/hardhat/module/hardhat.service.ts index 142d231..ad31ed6 100644 --- a/chain-api/src/hardhat/module/hardhat.service.ts +++ b/chain-api/src/hardhat/module/hardhat.service.ts @@ -1,28 +1,39 @@ -const hre = require('hardhat'); -// import hre from 'hardhat'; +// const hre = require('hardhat'); +import * as hre from 'hardhat'; import { Injectable } from '@nestjs/common'; - +import { ethers } from 'ethers'; +import { ConfigService } from '@nestjs/config'; @Injectable() export class HardhatService { + constructor(private readonly configService: ConfigService) {} async deploySalaryContract() { - // const { salaryAmount, userAddress } = req.body; + const provider = new ethers.JsonRpcProvider( + 'https://polygon-amoy.g.alchemy.com/v2/pEtFFy_Qr_NrM1vMnlzSXmYXkozVNzLy', + 80002, + ); - // // Read the Solidity contract template file - // const solidityCode = readSolidityTemplate(); // Implement this function to read the Solidity template file + const salary = await hre.artifacts.readArtifact('Salaries'); + const abi = salary.abi; + console.log('🚀 ~ HardhatService ~ deploySalaryContract ~ abi:', abi); + const bytecode = salary.deployedBytecode; + console.log( + '🚀 ~ HardhatService ~ deploySalaryContract ~ bytecode:', + bytecode, + ); + const signer = new ethers.Wallet( + this.configService.getOrThrow('POLYGON_PK'), + provider, + ); - // // Replace placeholders in the Solidity contract template with provided values - // const finalSolidityCode = replacePlaceholders(solidityCode, { - // salaryAmount, - // userAddress, - // }); + const salaryContract = new ethers.ContractFactory( + abi, + salary.bytecode, + signer, + ); - // // Compile the Solidity contract - // const compiledContract = await compileSolidity(finalSolidityCode); + const myContract = await salaryContract.deploy(); + await myContract.waitForDeployment(); - // // Deploy the contract - // const deployedContract = await deployContract(compiledContract); - const salaryC = await hre.ethers.getContractFactory('Salaries'); - const myContract = await salaryC.deploy(); console.log( '🚀 ~ HardhatService ~ deploySalaryContract ~ myContract:', myContract, diff --git a/chain-api/tsconfig.json b/chain-api/tsconfig.json index 95f5641..70ea190 100644 --- a/chain-api/tsconfig.json +++ b/chain-api/tsconfig.json @@ -16,6 +16,8 @@ "noImplicitAny": false, "strictBindCallApply": false, "forceConsistentCasingInFileNames": false, - "noFallthroughCasesInSwitch": false - } + "noFallthroughCasesInSwitch": false, + "resolveJsonModule": true + }, + "include": ["src/**/*", "src/**/*.json"] }