Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Rust bindings to make the package publishable #19

Merged
merged 14 commits into from
Dec 26, 2023
Merged
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
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,3 @@
The Projected NFT Whirlpool is a new protocol for the Paima Whirlpool vision to allow users from other ecosystems to naturally be able to use existing NFTs in games from other ecosystems while still maintaining custody.

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

# Building

1. Install [Aiken](https://aiken-lang.org/installation-instructions)
2. Run `aiken build` or `aiken check`
11 changes: 11 additions & 0 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
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 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,12 @@ 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.
14 changes: 6 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,15 @@ 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 +97,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 +242,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 +269,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 +336,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
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-sdk-json-schema-gen"
version = "0.0.1"
edition = "2018"


[dependencies]
serde_json = "1.0.57"
schemars = "0.8.8"
cardano-projected-nft-sdk = { 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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MintRedeemer",
"oneOf": [
{
"type": "string",
"enum": [
"BurnTokens"
]
},
{
"type": "object",
"required": [
"MintTokens"
],
"properties": {
"MintTokens": {
"type": "object",
"required": [
"total"
],
"properties": {
"total": {
"type": "string"
}
}
}
},
"additionalProperties": false
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "OutRef",
"type": "object",
"required": [
"index",
"tx_id"
],
"properties": {
"index": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"tx_id": {
"type": "string"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Owner",
"oneOf": [
{
"type": "object",
"required": [
"PKH"
],
"properties": {
"PKH": {
"type": "string"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"NFT"
],
"properties": {
"NFT": {
"type": "array",
"items": [
{
"type": "string"
},
{
"$ref": "#/definitions/AssetName"
}
],
"maxItems": 2,
"minItems": 2
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"Receipt"
],
"properties": {
"Receipt": {
"$ref": "#/definitions/AssetName"
}
},
"additionalProperties": false
}
],
"definitions": {
"AssetName": {
"type": "object",
"required": [
"inner"
],
"properties": {
"inner": {
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
}
}
}
}
}
Loading
Loading