Skip to content

Commit

Permalink
Update Rust bindings to make the package publishable (#19)
Browse files Browse the repository at this point in the history
* Move cardano code to its own folder

* Regenerate with new cddl-codegen

* work on json codegen

* Undo changes to Plutus.json

* cargo fmt

* Update github action cwd

* Fix git workflow configs

* Fix cargo fmt in github actions

* Fix clippy error

* Add stronger git hook

* cargo fmt

* Fix publish-helper script

* Add NPM packages to the README

* cargo fmt
  • Loading branch information
SebastienGllmt authored Dec 26, 2023
1 parent 102d909 commit 0479a97
Show file tree
Hide file tree
Showing 58 changed files with 2,798 additions and 630 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: fmt
args: -- --check
args: --check --manifest-path ./cardano/Cargo.toml --all

- name: "Linter checks"
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-features --all-targets -- --deny "clippy::all"
args: --all-features --all-targets --manifest-path ./cardano/Cargo.toml -- --deny "clippy::all"

- name: "Check"
uses: actions-rs/cargo@v1
with:
command: check
args: --all-features --all-targets
args: --all-features --all-targets --manifest-path ./cardano/Cargo.toml

- name: "Test"
uses: actions-rs/cargo@v1
with:
command: test
args: --all-features --all-targets
args: --all-features --all-targets --manifest-path ./cardano/Cargo.toml
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ jobs:
with:
version: v1.0.19-alpha

- run: aiken fmt --check
- run: aiken check
- run: aiken build
- run: aiken fmt ./cardano --check
- run: aiken check ./cardano
- run: aiken build ./cardano
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ sdk/Cargo.lock
**/target
**/pkg
**/node_modules
**/package-lock.json
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The Projected NFT Whirlpool is a new protocol for the Paima Whirlpool vision to

You can find the full docs on this [here](https://docs.paimastudios.com/home/multichain-support/projected-nfts/cross-chain/basics)

# Building
## Published packages

1. Install [Aiken](https://aiken-lang.org/installation-instructions)
2. Run `aiken build` or `aiken check`
- [Cardano NodeJS](https://www.npmjs.com/package/@paima/cardano-projected-nft-nodejs)
- [Cardano Browser](https://www.npmjs.com/package/@paima/cardano-projected-nft-browser)
17 changes: 14 additions & 3 deletions Cargo.lock → cardano/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml → cardano/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"sdk/rust",
"sdk/wasm",
"sdk/wasm/json-gen",
]

[workspace.metadata.release]
Expand Down
4 changes: 4 additions & 0 deletions cardano/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Building

1. Install [Aiken](https://aiken-lang.org/installation-instructions)
2. Run `aiken build` or `aiken check`
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions cardano/sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Generated inspired from the code generated using [projected-nft.cddl](./projected-nft.cddl) with the following cddl-codegen command

```bash
cargo run -- --input=projected-nft.cddl.cddl --output=. --json-serde-derives true --json-schema-export true --to-from-bytes-methods false --common-import-override=cml_core
```
30 changes: 30 additions & 0 deletions cardano/sdk/projected-nft.cddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
policy_id = _CDDL_CODEGEN_RAW_BYTES_TYPE_
asset_name = _CDDL_CODEGEN_EXTERN_TYPE_
ed25519_key_hash = _CDDL_CODEGEN_RAW_BYTES_TYPE_
out_ref = _CDDL_CODEGEN_EXTERN_TYPE_
big_int = _CDDL_CODEGEN_EXTERN_TYPE_ ; https://github.com/dcSpark/cddl-codegen/issues/220

owner =
ed25519_key_hash ; @name PKH
/ [policy_id, asset_name] ; @name NFT
/ asset_name ; @name Receipt

status =
0 ; @name Locked
/ [out_ref: out_ref, for_how_long: big_int] ; @name Unlocking


state =
[ owner: owner
, status: status
]

redeem =
[ partial_withdraw: bool
, nft_input_owner: out_ref / null
, new_receipt_owner: asset_name / null
]

mint_redeemer =
[total: big_int] ; @name MintTokens
/ 1 ; @name BurnTokens
2 changes: 1 addition & 1 deletion sdk/rust/Cargo.toml → cardano/sdk/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "cardano-projected-nft-sdk"
name = "cardano-projected-nft"
version = "0.1.0"
edition = "2021"

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use cml_chain::transaction::{
DatumOption, RequiredSigners, Transaction, TransactionInput, TransactionOutput,
TransactionWitnessSet,
};
use cml_chain::utils::BigInt;
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::{header, Client, StatusCode};
use serde::de::Error;
Expand All @@ -27,7 +28,7 @@ use std::path::PathBuf;
use std::str::FromStr;
use std::time::UNIX_EPOCH;

use cardano_projected_nft_sdk::{OutRef, Owner, Redeem, State, Status};
use cardano_projected_nft::{OutRef, Owner, Redeem, State, Status};
use cml_chain::builders::redeemer_builder::RedeemerWitnessKey;
use cml_chain::builders::tx_builder::{
ChangeSelectionAlgo, TransactionBuilder, TransactionBuilderConfigBuilder,
Expand Down Expand Up @@ -683,7 +684,7 @@ async fn handle_unlock(

let validity = ttl_by_posix(now - 100);
let ttl = ttl_by_posix(now + 100);
let for_how_long = (now + 400) * 1000;
let for_how_long = BigInt::from((now + 400) * 1000);

let new_datum = match config.control_nft.clone() {
None => State {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use std::fmt::Debug;

#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize, Serialize, schemars::JsonSchema)]
pub enum MintRedeemer {
MintTokens { total: u64 },
MintTokens { total: BigInt },
BurnTokens,
}

impl MintRedeemer {
pub fn new_mint(total: u64) -> MintRedeemer {
pub fn new_mint(total: BigInt) -> MintRedeemer {
MintRedeemer::MintTokens { total }
}

Expand All @@ -23,7 +23,7 @@ impl From<MintRedeemer> for PlutusData {
fn from(value: MintRedeemer) -> Self {
match value {
MintRedeemer::MintTokens { total } => PlutusData::new_constr_plutus_data(
ConstrPlutusData::new(0, vec![PlutusData::new_integer(BigInt::from(total))]),
ConstrPlutusData::new(0, vec![PlutusData::new_integer(total)]),
),
MintRedeemer::BurnTokens => {
PlutusData::new_constr_plutus_data(ConstrPlutusData::new(1, vec![]))
Expand All @@ -44,9 +44,7 @@ impl TryFrom<PlutusData> for MintRedeemer {
match constr.alternative {
0 => match constr.fields.get(0) {
Some(PlutusData::Integer(bigint)) => Ok(MintRedeemer::MintTokens {
total: bigint
.as_u64()
.ok_or("Mint tokens total valus can't be represented as u64".to_string())?,
total: bigint.clone(),
}),
_ => Err("constr field is not bigint".to_string()),
},
Expand All @@ -62,12 +60,14 @@ impl TryFrom<PlutusData> for MintRedeemer {
#[cfg(test)]
mod tests {
use crate::MintRedeemer;
use cml_chain::plutus::PlutusData;
use cml_chain::{plutus::PlutusData, utils::BigInt};

#[test]
fn test_mint_redeemer() {
let mint_redeemer = vec![
MintRedeemer::MintTokens { total: 253 },
MintRedeemer::MintTokens {
total: BigInt::from(253),
},
MintRedeemer::BurnTokens,
];
for redeem in mint_redeemer.into_iter() {
Expand Down
File renamed without changes.
File renamed without changes.
17 changes: 9 additions & 8 deletions sdk/rust/src/state.rs → cardano/sdk/rust/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,18 @@ impl TryFrom<PlutusData> for Owner {
)]
pub enum Status {
Locked,
Unlocking { out_ref: OutRef, for_how_long: u64 },
Unlocking {
out_ref: OutRef,
for_how_long: BigInt,
},
}

impl Status {
pub fn new_locked() -> Self {
Self::Locked
}

pub fn new_unlocking(out_ref: OutRef, for_how_long: u64) -> Self {
pub fn new_unlocking(out_ref: OutRef, for_how_long: BigInt) -> Self {
Self::Unlocking {
out_ref,
for_how_long,
Expand All @@ -97,7 +100,7 @@ impl From<Status> for PlutusData {
} => {
let out_ref = PlutusData::from(out_ref);

let for_how_long = PlutusData::new_integer(BigInt::from(for_how_long));
let for_how_long = PlutusData::new_integer(for_how_long);

PlutusData::new_constr_plutus_data(ConstrPlutusData::new(
1,
Expand Down Expand Up @@ -242,10 +245,7 @@ fn get_status(constr: ConstrPlutusData) -> Result<Status, String> {
let for_how_long = match constr.fields.get(1).ok_or(
"no field found for for_how_long while parsing unlocking status".to_string(),
)? {
PlutusData::Integer(bigint) => bigint.as_u64().ok_or(format!(
"can't convert for_how_long bigint {} to u64 while parsing unlocking status",
bigint
))?,
PlutusData::Integer(bigint) => bigint.clone(),
_ => {
return Err("expected to see for_how_long bigint field type while parsing unlocking status".to_string());
}
Expand All @@ -272,6 +272,7 @@ mod tests {
use cml_chain::plutus::PlutusData;
use cml_chain::PolicyId;

use cml_chain::utils::BigInt;
use cml_crypto::{Ed25519KeyHash, TransactionHash};

#[test]
Expand Down Expand Up @@ -338,7 +339,7 @@ mod tests {
.unwrap(),
index: 2,
},
for_how_long: 300,
for_how_long: BigInt::from(300),
},
};
let plutus_datum = PlutusData::from(datum.clone());
Expand Down
4 changes: 2 additions & 2 deletions sdk/wasm/Cargo.toml → cardano/sdk/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "cardano-projected-nft-sdk-wasm"
name = "cardano-projected-nft-wasm"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
cardano-projected-nft-sdk = { path = "../rust" }
cardano-projected-nft = { path = "../rust" }

cml-core = { version = "0.1.0" }
cml-chain = { version = "0.1.0" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"cbor": "^9.0.1",
"deno": "^0.1.1",
"lucid-cardano": "^0.10.7",
"cardano-projected-nft-sdk": "file:../pkg"
"cardano-projected-nft": "file:../pkg"
},
"keywords": [],
"author": "",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as projected_nft from "cardano-projected-nft-sdk";
import * as projected_nft from "cardano-projected-nft";

let state = projected_nft.State.new(
projected_nft.Owner.new_nft(
Expand Down
File renamed without changes.
12 changes: 12 additions & 0 deletions cardano/sdk/wasm/json-gen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "cardano-projected-nft-json-schema-gen"
version = "0.0.1"
edition = "2018"


[dependencies]
serde_json = "1.0.57"
schemars = "0.8.8"
cardano-projected-nft = { path = "../../rust" }
cml-chain = { version = "0.1.0" }
cml-crypto = { version = "0.1.0" }
46 changes: 46 additions & 0 deletions cardano/sdk/wasm/json-gen/output/json-types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export type MintRedeemerJSON =
| "BurnTokens"
| {
MintTokens: {
total: string;
[k: string]: unknown;
};
};
export interface OutRefJSON {
index: number;
tx_id: string;
}
export type OwnerJSON =
| {
PKH: string;
}
| {
NFT: [string, AssetNameJSON];
}
| {
Receipt: AssetNameJSON;
};
export interface RedeemJSON {
new_receipt_owner?: AssetNameJSON | null;
nft_input_owner?: OutRefJSON | null;
partial_withdraw: boolean;
}
export interface StateJSON {
owner: OwnerJSON;
status: StatusJSON;
}
export type StatusJSON =
| "Locked"
| {
Unlocking: {
for_how_long: string;
out_ref: OutRefJSON;
[k: string]: unknown;
};
};
export type ScriptHashJSON = string;
export interface AssetNameJSON {
inner: number[];
}
export type BigIntJSON = string;
export type Ed25519KeyHashJSON = string;
Loading

0 comments on commit 0479a97

Please sign in to comment.