Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add deployment script #6

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Compiler files
cache/
out/
node_modules

# Ignores development broadcast logs
!/broadcast
Expand Down
4 changes: 4 additions & 0 deletions scripts/deployment/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RPC_API_KEY=
STARKNET_NETWORK=sepolia
STARKNET_ACCOUNT_ADDRESS=
STARKNET_ACCOUNT_PRIVATE_KEY=
28 changes: 28 additions & 0 deletions scripts/deployment/deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import colors from "colors";
import { deployLordship } from "./libs/contract.js";

const main = async () => {
console.log(` ____ _ `.red);
console.log(` | \\ ___ ___| |___ _ _ `.red);
console.log(` | | | -_| . | | . | | |`.red);
console.log(` |____/|___| _|_|___|_ |`.red);
console.log(` |_| |___|`.red);

let LORDSHIP_DEFAULT_ADMIN = 0x11;
let LORDSHIP_MINTER_BURNER = 0x22;
let LORDSHIP_UPGRADER = 0x33;
let LORDSHIP_FLOW_RATE = 3n * 10n ** 18n;
let LORDSHIP_REWARD_TOKEN = 0x55;
let LORDSHIP_REWARD_PAYER = 0x66;

await deployLordship(
LORDSHIP_DEFAULT_ADMIN,
LORDSHIP_MINTER_BURNER,
LORDSHIP_UPGRADER,
LORDSHIP_FLOW_RATE,
LORDSHIP_REWARD_TOKEN,
LORDSHIP_REWARD_PAYER
);
};

main();
9 changes: 9 additions & 0 deletions scripts/deployment/deployment.ansi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

- Class Hash:  0x1e151253b00340fd3ddb0ccba0f57a84e38df7b37ecec01a9dd7c862454d04c
- Tx Hash:  https://sepolia.voyager.online/tx/0x4a182bb7ca1ad2f7528a402a41a09646c903152abc2a7d85dfc6ef5a7254c51)

Deploying Lordship ...


Tx hash:  https://sepolia.voyager.online/tx/0x77067e140f2517aa60fa7f58e2e0d6a3cadc222818af2c6a89d4b3ca8941ce)
Contract Address:  0x62272f88ab93647f665572450ae592715e1efcd2e513e366e57d44a29e9b9f9
120 changes: 120 additions & 0 deletions scripts/deployment/libs/contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import "dotenv/config";
import * as fs from "fs";
import * as path from "path";
import { fileURLToPath } from "url";
import { json } from "starknet";
import { getNetwork, getAccount } from "./network.js";
import { assert } from "console";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const TARGET_PATH = path.join(
__dirname,
"..",
"..",
"..",
"contracts",
"target",
"release"
);

const getContracts = () => {
if (!fs.existsSync(TARGET_PATH)) {
throw new Error(`Target directory not found at path: ${TARGET_PATH}`);
}
const contracts = fs
.readdirSync(TARGET_PATH)
.filter((contract) => contract.includes(".contract_class.json"));
if (contracts.length === 0) {
throw new Error("No build files found. Run `scarb build` first");
}
return contracts;
};

const getPath = (contract_name) => {
const contracts = getContracts();
const c = contracts.find((contract) => contract.includes(contract_name));
if (!c) {
throw new Error(`Contract not found: ${contract_name}`);
}
return path.join(TARGET_PATH, c);
};

const declare = async (filepath, contract_name) => {
console.log(`\nDeclaring ${contract_name}...\n\n`.magenta);
const compiledSierraCasm = filepath.replace(
".contract_class.json",
".compiled_contract_class.json"
);
const compiledFile = json.parse(fs.readFileSync(filepath).toString("ascii"));
const compiledSierraCasmFile = json.parse(
fs.readFileSync(compiledSierraCasm).toString("ascii")
);
const account = getAccount();
const contract = await account.declareIfNot({
contract: compiledFile,
casm: compiledSierraCasmFile,
});

const network = getNetwork(process.env.STARKNET_NETWORK);
console.log(`- Class Hash: `.magenta, `${contract.class_hash}`);
if (contract.transaction_hash) {
console.log(
"- Tx Hash: ".magenta,
`${network.explorer_url}/tx/${contract.transaction_hash})`
);
await account.waitForTransaction(contract.transaction_hash);
} else {
console.log("- Tx Hash: ".magenta, "Already declared");
}

return contract;
};

export const deployLordship = async (
DEFAULT_ADMIN,
MINTER_BURNER,
UPGRADER,
FLOW_RATE,
REWARD_TOKEN,
REWARD_PAYER
) => {
///////////////////////////////////////////
//////// LORDSHIP //////////////
///////////////////////////////////////////

// Load account
const account = getAccount();

// declare contract
let name = "Lordship";
const class_hash = (await declare(getPath(name), name)).class_hash;

assert(FLOW_RATE <= 2n ** 128n);

let constructorCalldata = [
DEFAULT_ADMIN,
MINTER_BURNER,
UPGRADER,
FLOW_RATE,
0, // u256 high
REWARD_TOKEN,
REWARD_PAYER,
];

// Deploy contract
console.log(`\nDeploying ${name} ... \n\n`.green);
let contract = await account.deployContract({
classHash: class_hash,
constructorCalldata: constructorCalldata,
});

// Wait for transaction
let network = getNetwork(process.env.STARKNET_NETWORK);
console.log(
"Tx hash: ".green,
`${network.explorer_url}/tx/${contract.transaction_hash})`
);
let a = await account.waitForTransaction(contract.transaction_hash);
console.log("Contract Address: ".green, contract.address, "\n\n");
};
39 changes: 39 additions & 0 deletions scripts/deployment/libs/network.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import "dotenv/config";
import { Account, RpcProvider, json } from "starknet";

const NETWORKS = {
mainnet: {
name: "mainnet",
explorer_url: "https://voyager.online",
rpc_url: `${process.env.RPC_API_KEY}`,
feeder_gateway_url: "https://alpha-mainnet.starknet.io/feeder_gateway",
gateway_url: "https://alpha-mainnet.starknet.io/gateway",
},
sepolia: {
name: "sepolia",
explorer_url: "https://sepolia.voyager.online",
rpc_url: `https://free-rpc.nethermind.io/sepolia-juno`,
feeder_gateway_url: "https://alpha-sepolia.starknet.io/feeder_gateway",
gateway_url: "https://alpha-sepolia.starknet.io/gateway",
},
};

export const getNetwork = (network) => {
if (!NETWORKS[network.toLowerCase()]) {
throw new Error(`Network ${network} not found`);
}
return NETWORKS[network.toLowerCase()];
};

export const getProvider = () => {
let network = getNetwork(process.env.STARKNET_NETWORK);
return new RpcProvider({ nodeUrl: network.rpc_url });
};

export const getAccount = () => {
const provider = getProvider();
const accountAddress = process.env.STARKNET_ACCOUNT_ADDRESS;
const privateKey = process.env.STARKNET_ACCOUNT_PRIVATE_KEY;
const cairoVersion = "1";
return new Account(provider, accountAddress, privateKey, cairoVersion);
};
Loading
Loading