Skip to content

Commit

Permalink
Drafting deployment script and instructions
Browse files Browse the repository at this point in the history
The plan is to use deploy.sh script to deploy programs on Solana and use
a migration script to execute required instructions on the deployed
programs.
  • Loading branch information
dimpar committed Jul 22, 2023
1 parent e96d926 commit 8320f27
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 10 deletions.
1 change: 1 addition & 0 deletions cross-chain/solana/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ target
node_modules
test-ledger
.yarn
solana.env
64 changes: 64 additions & 0 deletions cross-chain/solana/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash
set -eo pipefail

help() {
echo -e "\nUsage: $0 --cluster <cluster>"

echo -e "\nCommand line arguments:\n"
echo -e "\t--cluster: <cluster>\n" \
"\t\tAvailable deployment clusters: 'mainnet-beta', 'devnet', and 'localnet'." \
"Overrides a cluster set in Anchor.toml"
exit 1 # Exit script after printing help
}

# Setting env variables in the current bash shell
source solana.env

# Transform long options to short ones
for arg in "$@"; do
shift
case "$arg" in
"--cluster") set -- "$@" "-n" ;;
"--help") set -- "$@" "-h" ;;
*) set -- "$@" "$arg" ;;
esac
done

# Parse short options
OPTIND=1
while getopts "n:w:h" opt; do
case "$opt" in
n) CLUSTER="$OPTARG" ;;
h) help ;;
?) help ;; # Print help in case parameter is non-existent
esac
done
shift $(expr $OPTIND - 1) # remove options from positional parameters

[ -z "$CLUSTER" ] && {
echo "'--cluster' option not provided" >&2
help
exit 1
}

[ -z "$WALLET" ] && {
echo "'WALLET' env var is not set" >&2
help
exit 1
}

echo "Building workspace for cluster: $CLUSTER"
anchor build --provider.cluster $CLUSTER

echo "Syncing the program's id ..."
anchor keys sync

echo "Building workspace again to include new program ID in the binary ..."
anchor build --provider.cluster $CLUSTER

echo "Deploying program(s) for cluster: $CLUSTER"
anchor deploy --provider.cluster $CLUSTER --provider.wallet $WALLET

echo "Migrating..."
anchor migrate --provider.cluster $CLUSTER --provider.wallet $WALLET
# TODO: anchor migrate // executes migrations/deploy.ts script
57 changes: 50 additions & 7 deletions cross-chain/solana/migrations/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
// Migrations are an early feature. Currently, they're nothing more than this
// single deploy script that's invoked from the CLI, injecting a provider
// configured from the workspace's Anchor.toml.

const anchor = require("@coral-xyz/anchor");
import * as anchor from "@coral-xyz/anchor";
import * as web3 from "@solana/web3.js";
import fs from "fs";
import { Keypair } from "@solana/web3.js";
import dotenv from "dotenv";

module.exports = async function (provider) {
// Configure client to use the provider.
dotenv.config({ path: "../solana.env" })

anchor.setProvider(provider);

// Add your deploy script here.
const program = anchor.workspace.Tbtc;
// This wallet deployed the program and is also an authority
const authority = loadKey(process.env.WALLET);
const tbtcKeys = loadKey(process.env.TBTC_KEYS);

const [tbtcMintPDA, _] = web3.PublicKey.findProgramAddressSync(
[
anchor.utils.bytes.utf8.encode("tbtc-mint"),
tbtcKeys.publicKey.toBuffer(),
],
program.programId,
);

// Initalize tbtc program
await program.methods
.initialize()
.accounts({
tbtcMint: tbtcMintPDA,
tbtc: tbtcKeys.publicKey,
authority: authority.publicKey,
})
.signers([tbtcKeys])
.rpc();

// add a minter (wormhole gateway (minter keys))

// add a guardian?

// update mappings (self, arbitrum, optimism, polygon)

// transfer ownership to council
// solana program set-upgrade-authority -k <current_keypair_path> <programID> --new-upgrade-authority <pubkey>
};

function loadKey(filename: string): Keypair {
try {
const contents = fs.readFileSync(filename).toString();
const bs = Uint8Array.from(JSON.parse(contents));

return Keypair.fromSecretKey(bs);
} catch {
console.log("Unable to read keypair...", filename);
}
}
6 changes: 3 additions & 3 deletions cross-chain/solana/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
},
"dependencies": {
"@coral-xyz/anchor": "^0.28.0"
},
"devDependencies": {
"@coral-xyz/anchor": "^0.28.0",
"@solana/spl-token": "^0.3.8",
"@solana/web3.js": "^1.77.3",
"@types/bn.js": "^5.1.0",
"@types/chai": "^4.3.0",
"@types/mocha": "^9.0.0",
"@types/node": "^18.11.18",
"dotenv": "^16.3.1",
"chai": "^4.3.4",
"mocha": "^9.0.3",
"prettier": "^2.6.2",
Expand Down
3 changes: 3 additions & 0 deletions cross-chain/solana/solana.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
WALLET=<path>
TBTC_KEYS=<path>
UPGRADE_AUTHORITY=<threshold_council_multisig>

0 comments on commit 8320f27

Please sign in to comment.