Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ltfschoen committed Dec 30, 2024
1 parent 2291807 commit 5e03a3d
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 54 deletions.
4 changes: 2 additions & 2 deletions packages/secret-contracts-scripts/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let initialNetworkSettings = {
evm: {
network: "localhost",
localhost: {
chainId: 31337,
chainId: "31337",
endpoint: "http://127.0.0.1:8545/",
// Account #0: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 (10000 ETH)
privateKey: "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
Expand All @@ -28,7 +28,7 @@ let initialNetworkSettings = {
gatewayContractAddress: "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512", // only know after deploy
},
sepolia: {
chainId: 11155111,
chainId: "11155111",
endpoint: process.env.PROVIDER_RPC_ETHEREUM_SEPOLIA,
privateKey: process.env.DEPLOYER_PRIVATE_KEY,
nunyaBusinessContractAddress: "0xAFFF311821C3F3AF863C7103BB17BDC1Ba04603D", // only know after deploy
Expand Down
17 changes: 15 additions & 2 deletions packages/secret-contracts-scripts/src/evm/setEVMGatewayAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ethers, Contract, Wallet, utils } from "ethers";
import gatewayAbi from "../../../hardhat/artifacts/contracts/Gateway.sol/Gateway.json" assert { type: "json" };
import nunyaAbi from "../../../hardhat/artifacts/contracts/NunyaBusiness.sol/NunyaBusiness.json" assert { type: "json" };
import config from '../config/config.js';
import { loadDeployed } from "../loadDeployed.js";

let varsEvm;
if (config.networkSettings.evm.network == "sepolia") {
Expand All @@ -14,10 +15,22 @@ if (config.networkSettings.evm.network == "sepolia") {
} else {
throw new Error(`Unsupported network.`)
}
const { chainId, endpoint, nunyaBusinessContractAddress, gatewayContractAddress, privateKey } = varsEvm;
const { endpoint: evmEndpoint, privateKey } = varsEvm;

// Sets the deployed Gateway address storage value for the NunyaBusiness contract
async function setGatewayAddress() {

let deployed = await loadDeployed();
let varsDeployedEvm;
if (deployed.data.evm.network == "sepolia") {
varsDeployedEvm = deployed.data.evm.sepolia;
} else if (deployed.data.evm.network == "localhost") {
varsDeployedEvm = deployed.data.evm.localhost;
} else {
throw new Error(`Unsupported network.`)
}
const { nunyaBusinessContractAddress, gatewayContractAddress } = varsDeployedEvm;

if (nunyaBusinessContractAddress == "") {
console.error("Please deploy Nunya.business contract first");
}
Expand All @@ -28,7 +41,7 @@ async function setGatewayAddress() {
}

let provider;
provider = new ethers.providers.JsonRpcProvider(endpoint);
provider = new ethers.providers.JsonRpcProvider(evmEndpoint);
// console.log(provider);
await provider.detectNetwork();
const signer = new Wallet(privateKey, provider);
Expand Down
58 changes: 35 additions & 23 deletions packages/secret-contracts-scripts/src/functions/evm/requestNunya.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dotenv.config();
import { ethers, Wallet } from "ethers";
import { NonceManager } from "@ethersproject/experimental";
import config from './../../config/config.js';
import { loadDeployed } from "../../loadDeployed.js";
import gatewayAbi from "../../../../hardhat/artifacts/contracts/Gateway.sol/Gateway.json" assert { type: "json" };
import nunyaAbi from "../../../../hardhat/artifacts/contracts/NunyaBusiness.sol/NunyaBusiness.json" assert { type: "json" };
import { generateKeys } from "../../functions/secretpath/generateKeys.js";
Expand All @@ -15,16 +16,6 @@ import { hexlify } from "ethers/lib/utils.js";
import { assert } from "console";
import { RequestParams } from "../../types/index.js";

let varsSecret;
if (config.networkSettings.secret.network == "testnet") {
varsSecret = config.networkSettings.secret.testnet;
} else if (config.networkSettings.secret.network == "localhost") {
varsSecret = config.networkSettings.secret.localhost;
} else {
throw new Error(`Unsupported Secret network.`)
}
const { chainId: secretChainId, secretNunya: { nunyaContractAddress, nunyaContractCodeHash } } = varsSecret;

let varsEvm;
if (config.networkSettings.evm.network == "sepolia") {
varsEvm = config.networkSettings.evm.sepolia;
Expand All @@ -33,13 +24,34 @@ if (config.networkSettings.evm.network == "sepolia") {
} else {
throw new Error(`Unsupported network.`)
}
const { chainId: evmChainId, endpoint, nunyaBusinessContractAddress, gatewayContractAddress, privateKey } = varsEvm;
const { privateKey } = varsEvm;

export const requestNunya = async (params: RequestParams) => {
const { callbackSelectorName, callbackGasLimitAmount, requestFunctionName, requestEthValue, secretContractRequestHandle, secretContractRequestHandleArgs } = params;
const ifaceGateway = new ethers.utils.Interface(gatewayAbi.abi);
const ifaceNunya = new ethers.utils.Interface(nunyaAbi.abi);

let deployed = await loadDeployed();
let varsDeployedEvm;
if (deployed.data.evm.network == "sepolia") {
varsDeployedEvm = deployed.data.evm.sepolia;
} else if (deployed.data.evm.network == "localhost") {
varsDeployedEvm = deployed.data.evm.localhost;
} else {
throw new Error(`Unsupported network.`)
}
const { chainId: evmChainId, endpoint: evmEndpoint, nunyaBusinessContractAddress, gatewayContractAddress } = varsDeployedEvm;

let varsDeployedSecret;
if (deployed.data.secret.network == "testnet") {
varsDeployedSecret = deployed.data.secret.testnet;
} else if (deployed.data.secret.network == "localhost") {
varsDeployedSecret = deployed.data.secret.localhost;
} else {
throw new Error(`Unsupported network.`)
}
const { secretNunya: { nunyaContractCodeHash, nunyaContractAddress } } = varsDeployedSecret;

const routing_contract = nunyaContractAddress;
const routing_code_hash = nunyaContractCodeHash;

Expand All @@ -49,7 +61,7 @@ export const requestNunya = async (params: RequestParams) => {
}

let provider;
provider = new ethers.providers.JsonRpcProvider(endpoint);
provider = new ethers.providers.JsonRpcProvider(evmEndpoint);
console.log(provider);
await provider.detectNetwork();
const signer = new Wallet(privateKey, provider);
Expand Down Expand Up @@ -93,7 +105,7 @@ export const requestNunya = async (params: RequestParams) => {
// Data are the calldata/parameters that are passed into the contract
const data = JSON.stringify(secretContractRequestHandleArgs);

assert!(evmChainId.toString() == (await provider.getNetwork()).chainId.toString());
assert!(evmChainId == (await provider.getNetwork()).chainId.toString());

// EVM gateway contract address
// const publicClientAddress = await getPublicClientAddress(evmChainId);
Expand Down Expand Up @@ -188,31 +200,31 @@ export const requestNunya = async (params: RequestParams) => {
// let amountOfGas;
// let my_gas = 150000;

// if (evmChainId.toString() === "4202") {
// if (evmChainId === "4202") {
// amountOfGas = gasFee.mul(callbackGasLimit).mul(100000).div(2);
// } else if (evmChainId.toString() === "128123") {
// } else if (evmChainId === "128123") {
// amountOfGas = gasFee.mul(callbackGasLimit).mul(1000).div(2);
// my_gas = 15000000;
// } else if (evmChainId.toString() === "1287") {
// } else if (evmChainId === "1287") {
// amountOfGas = gasFee.mul(callbackGasLimit).mul(1000).div(2);
// my_gas = 15000000;
// } else if (evmChainId.toString() === "300") {
// } else if (evmChainId === "300") {
// amountOfGas = gasFee.mul(callbackGasLimit).mul(100000).div(2);
// my_gas = 15000000;
// } else if (evmChainId.toString() === "5003") {
// } else if (evmChainId === "5003") {
// amountOfGas = gasFee.mul(callbackGasLimit).mul(1000000).div(2);
// my_gas = 1500000000;
// } else if (evmChainId.toString() === "80002") {
// } else if (evmChainId === "80002") {
// amountOfGas = gasFee.mul(callbackGasLimit).mul(100).div(2);
// my_gas = 200000;
// } else if (evmChainId.toString() === "1995") {
// } else if (evmChainId === "1995") {
// amountOfGas = gasFee.mul(callbackGasLimit).mul(100).div(2);
// my_gas = 200000;
// } else if (evmChainId.toString() === "713715") {
// } else if (evmChainId === "713715") {
// amountOfGas = gasFee.mul(callbackGasLimit).mul(100).div(2);
// my_gas = 200000;
// } else {
// // Note: Sepolia Ethereum has evmChainId 11155111
// // Note: Sepolia Ethereum has chainId 11155111
// amountOfGas = gasFee.mul(callbackGasLimit).mul(3).div(2);
// }
// // Note: Only if get error `replacement fee too low` then just increase gasPrice by 10%
Expand Down Expand Up @@ -256,7 +268,7 @@ export const requestNunya = async (params: RequestParams) => {
gasPrice: hexlify(my_gas),
nonce: nextNonceNum,
data: functionData, // function to call and args
chainId: evmChainId,
chainId: parseInt(evmChainId),
}

tx = await managedSigner.sendTransaction(txParamsSend);
Expand Down
15 changes: 5 additions & 10 deletions packages/secret-contracts-scripts/src/loadDeployed.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import { readFile, writeFile } from "fs/promises";
import * as path from 'path';
// https://flaviocopes.com/fix-dirname-not-defined-es-module-scope/
import { fileURLToPath } from 'url';

// replicate the functionality of __dirname
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const filePath = path.resolve(__dirname, '../../../deployed.json');
// console.log('File path:', filePath);
console.log('File path:', filePath);

async function loadDeployed () {
try {
const rawData = await readFile(filePath);
if (!rawData.length) {
console.error("Empty file");
console.log("empty file");
process.exit()
}
let jsonData: any = JSON.parse(rawData.toString());
// console.log('jsonData: ', jsonData);
const jsonData: any = JSON.parse(rawData.toString());
// console.log('loadDeployed: jsonData: ', JSON.stringify(jsonData, null, 2));

return jsonData;
} catch (err) {
console.error(err);
console.log(err);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,12 @@ const walletOptions = {
}

let isLocal: boolean;
if (config.networkSettings.secret.network == "testnet") {
isLocal = false;
} else if (config.networkSettings.secret.network == "localhost") {
isLocal = true;
} else if (config.networkSettings.secret.network == "mainnet") {
throw new Error(`Unsupported Secret network.`)
}

let varsSecret;
if (config.networkSettings.secret.network == "testnet") {
isLocal = false;
varsSecret = config.networkSettings.secret.testnet;
} else if (config.networkSettings.secret.network == "localhost") {
isLocal = true;
varsSecret = config.networkSettings.secret.localhost;
} else {
throw new Error(`Unsupported Secret network.`)
Expand Down
11 changes: 3 additions & 8 deletions packages/secret-contracts-scripts/src/writeDeployed.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import { readFile, writeFile } from "fs/promises";
import * as path from 'path';
// https://flaviocopes.com/fix-dirname-not-defined-es-module-scope/
import { fileURLToPath } from 'url';

// replicate the functionality of __dirname
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const filePath = path.resolve(__dirname, '../../../deployed.json');
// console.log('File path:', filePath);

async function writeDeployed (newDeployed: any) {
try {
let newJsonData = JSON.stringify(newDeployed, null, 2);
const newJsonData = JSON.stringify(newDeployed, null, 2);

await writeFile(filePath, newJsonData, { flag: 'w+' });
console.log("Updated deployed.json");
// console.log("writeDeployed: newDeployed: ", newJsonData);
} catch (err) {
console.error(err);
console.log(err);
}
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ sudo chown -R ethlocal_service /root/nunya/packages/hardhat/node_modules/hardhat
sudo chmod 755 /opt/ethlocal/hardhat
sudo chmod 755 ~/nunya/packages/hardhat/node_modules/.bin/hardhat
sudo chmod 755 ~/nunya/packages/hardhat/node_modules/hardhat/internal/cli/bootstrap.js
ls -al /opt/ethlocal
# ls -al /opt/ethlocal

# Create service file

Expand Down

0 comments on commit 5e03a3d

Please sign in to comment.