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

refactor: plutus v3; withdraw zero #59

Open
wants to merge 12 commits into
base: v2.1
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ node_modules
build
build-tsc
coverage
.idea
.idea
# enviroment variables
*.env*
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Continuous Integration

on:
push:
branches: ["main"]
pull_request:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: aiken-lang/setup-aiken@v1
with:
version: v1.1.9
- run: aiken fmt --check
- run: aiken check -D
- run: aiken build
6 changes: 6 additions & 0 deletions _tests/always_success_mint/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Aiken compilation artifacts
artifacts/
# Aiken's project working directory
build/
# Aiken's default documentation export
docs/
65 changes: 65 additions & 0 deletions _tests/always_success_mint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# always_success_mint

Write validators in the `validators` folder, and supporting functions in the `lib` folder using `.ak` as a file extension.

```aiken
validator my_first_validator {
spend(_datum: Option<Data>, _redeemer: Data, _output_reference: Data, _context: Data) {
True
}
}
```

## Building

```sh
aiken build
```

## Configuring

**aiken.toml**
```toml
[config.default]
network_id = 41
```

Or, alternatively, write conditional environment modules under `env`.

## Testing

You can write tests in any module using the `test` keyword. For example:

```aiken
use config

test foo() {
config.network_id + 1 == 42
}
```

To run all tests, simply do:

```sh
aiken check
```

To run only tests matching the string `foo`, do:

```sh
aiken check -m foo
```

## Documentation

If you're writing a library, you might want to generate an HTML documentation for it.

Use:

```sh
aiken docs
```

## Resources

Find more on the [Aiken's user manual](https://aiken-lang.org).
15 changes: 15 additions & 0 deletions _tests/always_success_mint/aiken.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This file was generated by Aiken
# You typically do not need to edit this file

[[requirements]]
name = "aiken-lang/stdlib"
version = "v2.2.0"
source = "github"

[[packages]]
name = "aiken-lang/stdlib"
version = "v2.2.0"
requirements = []
source = "github"

[etags]
18 changes: 18 additions & 0 deletions _tests/always_success_mint/aiken.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name = "scisamir/always_success_mint"
version = "0.0.0"
compiler = "v1.1.9"
plutus = "v3"
license = "Apache-2.0"
description = "Aiken contracts for project 'scisamir/always_success_mint'"

[repository]
user = "scisamir"
project = "always_success_mint"
platform = "github"

[[dependencies]]
name = "aiken-lang/stdlib"
version = "v2.2.0"
source = "github"

[config]
40 changes: 40 additions & 0 deletions _tests/always_success_mint/plutus.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"preamble": {
"title": "scisamir/always_success_mint",
"description": "Aiken contracts for project 'scisamir/always_success_mint'",
"version": "0.0.0",
"plutusVersion": "v3",
"compiler": {
"name": "Aiken",
"version": "v1.1.9+2217206"
},
"license": "Apache-2.0"
},
"validators": [
{
"title": "placeholder.placeholder.mint",
"redeemer": {
"title": "_redeemer",
"schema": {
"$ref": "#/definitions/Data"
}
},
"compiledCode": "5850010100323232323225333002323232323253330073370e900018041baa0011324a26eb8c028c024dd50008b1804980500198040011803801180380098021baa00114984d9595cd2ab9d5573cae855d11",
"hash": "0f6b02150cbcc7fedafa388abcc41635a9443afb860100099ba40f07"
},
{
"title": "placeholder.placeholder.else",
"redeemer": {
"schema": {}
},
"compiledCode": "5850010100323232323225333002323232323253330073370e900018041baa0011324a26eb8c028c024dd50008b1804980500198040011803801180380098021baa00114984d9595cd2ab9d5573cae855d11",
"hash": "0f6b02150cbcc7fedafa388abcc41635a9443afb860100099ba40f07"
}
],
"definitions": {
"Data": {
"title": "Data",
"description": "Any Plutus data."
}
}
}
13 changes: 13 additions & 0 deletions _tests/always_success_mint/validators/placeholder.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use cardano/assets.{PolicyId}
use cardano/transaction.{Transaction}
use cardano/script_context.{ScriptContext}

validator placeholder {
mint(_redeemer: Data, _policy_id: PolicyId, _self: Transaction) {
True
}

else(_ctx: ScriptContext) {
fail
}
}
18 changes: 18 additions & 0 deletions _tests/batching.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { txBuilder, wallet2, wallet2Address, wallet2Collateral, wallet2Utxos } from "./setup";

// wallet 2 acting as the batcher
const unsignedTx = await txBuilder
.txInCollateral(
wallet2Collateral.input.txHash,
wallet2Collateral.input.outputIndex,
wallet2Collateral.output.amount,
wallet2Collateral.output.address,
)
.changeAddress(wallet2Address)
.selectUtxosFrom(wallet2Utxos)
.complete();

const signedTx = await wallet2.signTx(unsignedTx);
const txHash = await wallet2.submitTx(signedTx);

console.log("pool batching tx hash:", txHash);
67 changes: 67 additions & 0 deletions _tests/cancel_order_by_anyone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { mConStr1, mConStr2, SLOT_CONFIG_NETWORK, unixTimeToEnclosingSlot } from "@meshsdk/core";
import { blockchainProvider, orderValidatorScript, orderValidatorAddress, txBuilder, wallet1, wallet1Address, wallet1Collateral, wallet1Utxos, wallet1VK, orderValidatorRewardAddress, orderValidatorScriptHash, orderCanclValidatorRewardAddress, orderCanclValidatorScript } from "./setup.js"

// register order cancellation validator stake key
// const unsignedTx = await txBuilder
// // .registerStakeCertificate(orderValidatorScriptHash)
// .registerStakeCertificate(orderCanclValidatorRewardAddress)
// .selectUtxosFrom(wallet1Utxos)
// .changeAddress(wallet1Address)
// .complete();
// const signedTx = await wallet1.signTx(unsignedTx);
// const txHash = await wallet1.submitTx(signedTx);
// console.log("register order cancel stake certificate tx hash:", txHash);


console.log("orderValidatorAddress:", orderValidatorAddress);
const orderUtxo = (await blockchainProvider.fetchAddressUTxOs(orderValidatorAddress))[0];
if (!orderUtxo) {
throw new Error("order utxo not found!");
}
console.log("orderUtxo:", orderUtxo);

const invalidBefore = unixTimeToEnclosingSlot(
(Date.now() - 15000),
SLOT_CONFIG_NETWORK.preview
)

console.log('\n', "expired time: 1738668722616");
console.log(" current time:", Date.now() - 15000, '\n');

const unsignedTx = await txBuilder
.spendingPlutusScriptV3()
.txIn(
orderUtxo.input.txHash,
orderUtxo.input.outputIndex,
orderUtxo.output.amount,
orderUtxo.output.address,
)
.txInScript(orderValidatorScript)
.spendingReferenceTxInInlineDatumPresent()
.spendingReferenceTxInRedeemerValue("")
// withdraw zero (order validator)
.withdrawalPlutusScriptV3()
.withdrawal(orderValidatorRewardAddress, "0")
.withdrawalScript(orderValidatorScript)
.withdrawalRedeemerValue(mConStr2([]))
// order cancellation validator withdrawal
.withdrawalPlutusScriptV3()
.withdrawal(orderCanclValidatorRewardAddress, "0")
.withdrawalScript(orderCanclValidatorScript)
.withdrawalRedeemerValue("")
.txOut(wallet1Address, orderUtxo.output.amount)
.txInCollateral(
wallet1Collateral.input.txHash,
wallet1Collateral.input.outputIndex,
wallet1Collateral.output.amount,
wallet1Collateral.output.address,
)
.invalidBefore(invalidBefore)
.changeAddress(wallet1Address)
.selectUtxosFrom(wallet1Utxos)
.complete()

const signedTx = await wallet1.signTx(unsignedTx);
const txHash = await wallet1.submitTx(signedTx);

console.log("Cancel order by anyone tx hash:", txHash);
54 changes: 54 additions & 0 deletions _tests/cancel_order_by_owner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { mConStr1 } from "@meshsdk/core";
import { blockchainProvider, orderValidatorScript, orderValidatorAddress, txBuilder, wallet1, wallet1Address, wallet1Collateral, wallet1Utxos, wallet1VK, orderValidatorRewardAddress, orderValidatorScriptHash, alwaysSuccessValidatorScript } from "./setup.js"

// withdraw zero setup (register order validator stake cert)
// const unsignedTx = await txBuilder
// .registerStakeCertificate(orderValidatorRewardAddress)
// .selectUtxosFrom(wallet1Utxos)
// .changeAddress(wallet1Address)
// .complete();
// const signedTx = await wallet1.signTx(unsignedTx);
// const txHash = await wallet1.submitTx(signedTx);
// console.log("register order stake certificate tx hash:", txHash);


// console.log("orderValidatorAddress:", orderValidatorAddress);
const orderUtxo = (await blockchainProvider.fetchAddressUTxOs(orderValidatorAddress))[0];
if (!orderUtxo) {
throw new Error("order utxo not found!");
}
// console.log("orderUtxo:", orderUtxo);

const unsignedTx = await txBuilder
.spendingPlutusScriptV3()
.txIn(
orderUtxo.input.txHash,
orderUtxo.input.outputIndex,
orderUtxo.output.amount,
orderUtxo.output.address,
)
.txInScript(alwaysSuccessValidatorScript)
// .txInScript(orderValidatorScript)
.spendingReferenceTxInInlineDatumPresent()
.spendingReferenceTxInRedeemerValue("")
// withdraw zero
.withdrawalPlutusScriptV3()
.withdrawal(orderValidatorRewardAddress, "0")
.withdrawalScript(orderValidatorScript)
.withdrawalRedeemerValue(mConStr1([]))
.txOut(wallet1Address, orderUtxo.output.amount)
.txInCollateral(
wallet1Collateral.input.txHash,
wallet1Collateral.input.outputIndex,
wallet1Collateral.output.amount,
wallet1Collateral.output.address,
)
.changeAddress(wallet1Address)
.selectUtxosFrom(wallet1Utxos)
.requiredSignerHash(wallet1VK)
.complete()

const signedTx = await wallet1.signTx(unsignedTx);
const txHash = await wallet1.submitTx(signedTx);

console.log("Cancel order by owner tx hash:", txHash);
41 changes: 41 additions & 0 deletions _tests/create_order.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { mConStr0, mConStr1, mPubKeyAddress } from "@meshsdk/core";
import { authenPolicyId, orderValidatorAddress, txBuilder, wallet1, wallet1Address, wallet1Collateral, wallet1SK, wallet1Utxos, wallet1VK } from "./setup.js"

const orderStep = mConStr0([
mConStr1([]), // True
mConStr0([20]),
20,
mConStr1([]), // True
]);
const orderDatum = mConStr0([
mConStr0([wallet1VK]),
mPubKeyAddress(wallet1VK, wallet1SK),
mConStr0([]),
mPubKeyAddress(wallet1VK, wallet1SK),
mConStr0([]),
mConStr0([
authenPolicyId, // policy id
"my_asset" // asset name
]), // template lp_asset (just for an order to cancel) <=== To be editted when submitting a real order
orderStep,
10,
mConStr0([[(Date.now() + (10 * 60 * 1000)), 0]]), // 10 mins exp time; tip 0
]);

const unsignedTx = await txBuilder
.txOut(orderValidatorAddress, [{ unit: "lovelace", quantity: "120000000" }])
.txOutInlineDatumValue(orderDatum)
.txInCollateral(
wallet1Collateral.input.txHash,
wallet1Collateral.input.outputIndex,
wallet1Collateral.output.amount,
wallet1Collateral.output.address,
)
.changeAddress(wallet1Address)
.selectUtxosFrom(wallet1Utxos)
.complete()

const signedTx = await wallet1.signTx(unsignedTx);
const txHash = await wallet1.submitTx(signedTx);

console.log("Create order tx hash:", txHash);
Loading