Skip to content

Instantly share code, notes, and snippets.

@kalouo
Last active September 21, 2021 18:59
Show Gist options
  • Save kalouo/b80e4761c94c7768bbc295939a0688d8 to your computer and use it in GitHub Desktop.
Save kalouo/b80e4761c94c7768bbc295939a0688d8 to your computer and use it in GitHub Desktop.
Tezos IDE
#!/usr/bin/env bash
set -e -o pipefail
echo "----------------------------------------"
echo "Compiling contracts ... "
echo "----------------------------------------"
# Expected location of SmartPy CLI.
SMART_PY_CLI=~/smartpy-cli/SmartPy.sh
# Build artifact directory.
OUT_DIR=./build/.tmp_contract_build
# Array of SmartPy files to compile.
# CONTRACTS_ARRAY=(counter)
# Exit if SmartPy is not installed.
if [ ! -f "$SMART_PY_CLI" ]; then
echo "Fatal: Please install SmartPy CLI at $SMART_PY_CLI" && exit
fi
function processContract {
CONTRACT_NAME=$1
OUT_DIR=$2
CONTRACT_IN="./contracts/${CONTRACT_NAME}.py"
CONTRACT_OUT="${CONTRACT_NAME}.json"
STORAGE_OUT="${CONTRACT_NAME}_storage.json"
CONTRACT_COMPILED="${CONTRACT_NAME}/step_000_cont_0_contract.json"
STORAGE_COMPILED="${CONTRACT_NAME}/step_000_cont_0_storage.json"
echo ">> Processing ${CONTRACT_NAME}"
# Ensure file exists.
if [ ! -f "$CONTRACT_IN" ]; then
echo "Fatal: $CONTRACT_IN not found. Running from wrong dir?" && exit
fi
echo ">>> [1 / 3] Testing ${CONTRACT_NAME} ... "
$SMART_PY_CLI test $CONTRACT_IN $OUT_DIR --html
echo ">>> [2 / 3] Compiling ${CONTRACT_NAME} ..."
$SMART_PY_CLI compile $CONTRACT_IN $OUT_DIR --html
echo ">>> [3 / 3] Extracting Michelson contract ... "
cp $OUT_DIR/$CONTRACT_COMPILED ./build/$CONTRACT_OUT
cp $OUT_DIR/$STORAGE_COMPILED ./build/$STORAGE_OUT
echo ">>> Michelson contract written to ${CONTRACT_OUT}"
}
export PYTHONPATH=$PWD
echo "> [1 / 2] Unit Testing and Compiling Contracts."
# Use if you want to pass a contract or more as arguments.
for n in $(seq 1 $#); do
processContract $1 $OUT_DIR
shift
done
# Use if you want to compile all contracts in CONTRACTS_ARRAY. No arguments needed.
# for i in ${!CONTRACTS_ARRAY[@]}; do
# processContract ${CONTRACTS_ARRAY[$i]} $OUT_DIR
# done
# Remove build artifacts.
echo "> [2 / 2] Cleaning up ..."
rm -rf $OUT_DIR
rm -rf ./contracts/__pycache__
rm -rf ./__pycache__
echo "> Removed artifacts."
import smartpy as sp
class Counter(sp.Contract):
def __init__(self, initial_value):
self.init(stored_value=initial_value)
@sp.entry_point
def increment(self, params):
self.data.stored_value += params.value
@sp.entry_point
def decrement(self, params):
self.data.stored_value -= params.value
@sp.add_test(name="Counter")
def test():
scenario = sp.test_scenario()
counter = Counter(initial_value=0)
scenario += counter
scenario.verify(counter.data.stored_value == 0)
scenario += counter.increment(value = 1)
scenario.verify(counter.data.stored_value == 1)
scenario += counter.decrement(value = 1)
scenario.verify(counter.data.stored_value == 0)
sp.add_compilation_target("counter", Counter(initial_value=0))
import * as dotenv from "dotenv";
import { TezosToolkit } from "@taquito/taquito";
import { InMemorySigner } from "@taquito/signer";
dotenv.config(); /* This loads the variables in your .env file to process.env */
const deploy = async () => {
const { TEZOS_RPC_URL, ORIGINATOR_PRIVATE_KEY } = process.env;
const signer = await InMemorySigner.fromSecretKey(ORIGINATOR_PRIVATE_KEY);
const Tezos = new TezosToolkit(TEZOS_RPC_URL);
Tezos.setProvider({ signer: signer });
try {
const { hash, contractAddress } = await Tezos.contract.originate({
code: require("../build/counter.json"),
init: require("../build/counter_storage.json"),
});
console.log("Successfully deployed contract");
console.log(`>> Transaction hash: ${hash}`);
console.log(`>> Contract address: ${contractAddress}`);
} catch (error) {
console.log(error);
}
};
deploy();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment