Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ More complex role relationships can be created by using [_setRoleAdmin](#AccessC
## AccessControl [toc] [#AccessControl]
<APIGithubLinkHeader
moduleName="AccessControl"
link={`https://github.com/OpenZeppelin/compact-contracts/blob/main/contracts/src/access/AccessControl.compact`}
link={`https://github.com/OpenZeppelin/compact-contracts/blob/v0.1.0/contracts/src/access/AccessControl.compact`}
/>

```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This module provides the full FungibleToken module API.
## FungibleToken [toc] [#FungibleToken]
<APIGithubLinkHeader
moduleName="FungibleToken"
link={`https://github.com/OpenZeppelin/compact-contracts/blob/main/contracts/src/token/FungibleToken.compact`}
link={`https://github.com/OpenZeppelin/compact-contracts/blob/v0.1.0/contracts/src/token/FungibleToken.compact`}
/>

```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This module provides the full MultiToken module API.
## MultiToken [toc] [#MultiToken]
<APIGithubLinkHeader
moduleName="MultiToken"
link={`https://github.com/OpenZeppelin/compact-contracts/blob/main/contracts/src/token/MultiToken.compact`}
link={`https://github.com/OpenZeppelin/compact-contracts/blob/v0.1.0/contracts/src/token/MultiToken.compact`}
/>

```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This module provides the full NonFungibleToken module API.
## NonFungibleToken [toc] [#NonFungibleToken]
<APIGithubLinkHeader
moduleName="NonFungibleToken"
link={`https://github.com/OpenZeppelin/compact-contracts/blob/main/contracts/src/token/NonFungibleToken.compact`}
link={`https://github.com/OpenZeppelin/compact-contracts/blob/v0.1.0/contracts/src/token/NonFungibleToken.compact`}
/>

```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ For an overview of the module, read the [Ownable guide](../ownable).
## Ownable [toc] [#Ownable]
<APIGithubLinkHeader
moduleName="Ownable"
link={`https://github.com/OpenZeppelin/compact-contracts/blob/main/contracts/src/access/Ownable.compact`}
link={`https://github.com/OpenZeppelin/compact-contracts/blob/v0.1.0/contracts/src/access/Ownable.compact`}
/>

```ts
Expand Down Expand Up @@ -250,7 +250,7 @@ import "./node_modules/@openzeppelin/compact-contracts/access/Ownable";
## ZOwnablePK [toc] [#ZOwnablePK]
<APIGithubLinkHeader
moduleName="ZOwnablePK"
link={`https://github.com/OpenZeppelin/compact-contracts/blob/main/contracts/src/access/ZOwnablePK.compact`}
link={`https://github.com/OpenZeppelin/compact-contracts/blob/v0.1.0/contracts/src/access/ZOwnablePK.compact`}
/>

```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This package provides the API for all Security modules.
## Initializable [toc] [#Initializable]
<APIGithubLinkHeader
moduleName="Initializable"
link={`https://github.com/OpenZeppelin/compact-contracts/blob/main/contracts/src/security/Initializable.compact`}
link={`https://github.com/OpenZeppelin/compact-contracts/blob/v0.1.0/contracts/src/security/Initializable.compact`}
/>

```ts
Expand Down Expand Up @@ -91,7 +91,7 @@ None.
## Pausable [toc] [#Pausable]
<APIGithubLinkHeader
moduleName="Pausable"
link={`https://github.com/OpenZeppelin/compact-contracts/blob/main/contracts/src/security/Pausable.compact`}
link={`https://github.com/OpenZeppelin/compact-contracts/blob/v0.1.0/contracts/src/security/Pausable.compact`}
/>

```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ More complex role relationships can be created by using [_setRoleAdmin](#Shielde
## ShieldedAccessControl [toc] [#ShieldedAccessControl]
<APIGithubLinkHeader
moduleName="ShieldedAccessControl"
link={`https://github.com/OpenZeppelin/compact-contracts/blob/main/contracts/src/access/ShieldedAccessControl.compact`}
link={`https://github.com/OpenZeppelin/compact-contracts/blob/v0.1.0/contracts/src/access/ShieldedAccessControl.compact`}
/>

```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ title: Utils
This package provides the API for all Utils modules.

<Callout type='info'>
For an overview of the module, read the [Utils guide](/contracts-compact/utils).
For an overview of the module, read the [Utils guide](../utils).
</Callout>

## Utils [toc] [#Utils]
<APIGithubLinkHeader
moduleName="Utils"
link={`https://github.com/OpenZeppelin/compact-contracts/blob/main/contracts/src/utils/Utils.compact`}
link={`https://github.com/OpenZeppelin/compact-contracts/blob/v0.1.0/contracts/src/utils/Utils.compact`}
/>

```ts
Expand Down
175 changes: 175 additions & 0 deletions content/contracts-compact/0.1.x/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
---
title: Contracts for Compact
---

{/* links */}
[Midnight]: https://midnight.network/
[nvm]: https://github.com/nvm-sh/nvm
[yarn]: https://yarnpkg.com/getting-started/install
[compact-dev-tools]: https://docs.midnight.network/blog/compact-developer-tools

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

<Callout type="warn">
This repo contains highly experimental code.
Expect rapid iteration. **Use at your own risk.**
</Callout>

## Usage

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

Follow Midnight's [Compact Developer Tools][compact-dev-tools] installation guide and confirm that `compact` is in the `PATH` env variable.

```bash
$ compact compile --version

Compactc version: {{compact_compiler_version}}
{{compact_compiler_version}}
```

### Installation

Create a directory for your project.

```bash
mkdir my-project
cd my-project
```

Install the library.

```bash
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/...`.

```typescript
// MyContract.compact

pragma language_version >= {{compact_language_version}};

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.

```bash
% 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
```
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { COMPACT_COMPILER_VERSION, COMPACT_LANGUAGE_VERSION } from "./constants";

export const REPLACEMENTS = {
include: ['**/content/contracts-compact/**/*.mdx'],
include: ['**/content/contracts-compact/0.1.x/**/*.mdx'],
replacements: {
compact_compiler_version: COMPACT_COMPILER_VERSION,
compact_language_version: COMPACT_LANGUAGE_VERSION
Expand Down
Loading
Loading