Contracts for Compact

A library for secure smart contract development written in Compact for Midnight. This library consists of modules to build custom smart contracts.

This repo contains highly experimental code. Expect rapid iteration. Use at your own risk.

Usage

Make sure you have nvm and yarn installed on your machine.

Follow Midnight's Compact Developer Tools installation guide and confirm that compact is in the PATH env variable.

$ compact compile --version

Compactc version: 0.29.0
0.29.0

Installation

Create a directory for your project.

mkdir my-project
cd my-project

Install the library.

yarn add @openzeppelin/compact-contracts

Write a custom contract using library modules

In the root of my-project, create a custom contract using OpenZeppelin Compact modules. Import the modules through ./node_modules/@openzeppelin/compact-contracts/....

// MyContract.compact

pragma language_version >= 0.21.0;

import CompactStandardLibrary;
import "./node_modules/@openzeppelin/compact-contracts/access/Ownable"
  prefix Ownable_;
import "./node_modules/@openzeppelin/compact-contracts/security/Pausable"
  prefix Pausable_;
import "./node_modules/@openzeppelin/compact-contracts/token/FungibleToken"
  prefix FungibleToken_;

constructor(
  _name: Opaque<"string">,
  _symbol: Opaque<"string">,
  _decimals: Uint<8>,
  _recipient: Either<Bytes<32>, ContractAddress>,
  _amount: Uint<128>,
  _initOwner: Either<Bytes<32>, ContractAddress>,
) {
  Ownable_initialize(_initOwner);
  FungibleToken_initialize(_name, _symbol, _decimals);
  FungibleToken__mint(_recipient, _amount);
}

/** IFungibleToken */

export circuit name(): Opaque<"string"> {
  return FungibleToken_name();
}

export circuit symbol(): Opaque<"string"> {
  return FungibleToken_symbol();
}

export circuit decimals(): Uint<8> {
  return FungibleToken_decimals();
}

export circuit totalSupply(): Uint<128> {
  return FungibleToken_totalSupply();
}

export circuit balanceOf(account: Either<Bytes<32>, ContractAddress>): Uint<128> {
  return FungibleToken_balanceOf(account);
}

export circuit allowance(
  owner: Either<Bytes<32>, ContractAddress>,
  spender: Either<Bytes<32>, ContractAddress>
): Uint<128> {
  return FungibleToken_allowance(owner, spender);
}

export circuit transfer(
  to: Either<Bytes<32>, ContractAddress>,
  value: Uint<128>,
): Boolean {
  Pausable_assertNotPaused();
  return FungibleToken_transfer(to, value);
}

export circuit transferFrom(
  fromAddress: Either<Bytes<32>, ContractAddress>,
  to: Either<Bytes<32>, ContractAddress>,
  value: Uint<128>
): Boolean {
  Pausable_assertNotPaused();
  return FungibleToken_transferFrom(fromAddress, to, value);
}

export circuit approve(spender: Either<Bytes<32>, ContractAddress>, value: Uint<128>): Boolean {
  Pausable_assertNotPaused();
  return FungibleToken_approve(spender, value);
}

/** IOwnable */

export circuit owner(): Either<Bytes<32>, ContractAddress> {
  return Ownable_owner();
}

export circuit transferOwnership(newOwner: Either<Bytes<32>, ContractAddress>): [] {
  return Ownable_transferOwnership(newOwner);
}

export circuit renounceOwnership(): [] {
  return Ownable_renounceOwnership();
}

/** IPausable */

export circuit pause(): [] {
  Ownable_assertOnlyOwner();
  Pausable__pause();
}

export circuit unpause(): [] {
  Ownable_assertOnlyOwner();
  Pausable__unpause();
}

Compile the contract

In the project root, compile the contract.

% compact compile MyContract.compact artifacts/MyContract
Compiling 11 circuits:
  circuit "allowance" (k=11, rows=1352)
  circuit "approve" (k=13, rows=3075)
  circuit "balanceOf" (k=10, rows=673)
  circuit "decimals" (k=6, rows=28)
  circuit "name" (k=6, rows=28)
  circuit "pause" (k=13, rows=2365)
  circuit "symbol" (k=6, rows=28)
  circuit "totalSupply" (k=6, rows=28)
  circuit "transfer" (k=13, rows=3990)
  circuit "transferFrom" (k=13, rows=4972)
  circuit "unpause" (k=13, rows=2362)
Overall progress [====================] 11/11