Skip to content

Commit

Permalink
Merge pull request #201 from MeshJS/aiken-lib
Browse files Browse the repository at this point in the history
Aiken lib
  • Loading branch information
jinglescode authored Apr 5, 2024
2 parents e3c8ef0 + bfdc42f commit 68b6400
Show file tree
Hide file tree
Showing 48 changed files with 2,374 additions and 1,268 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build-and-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
node-version: 20
- run: yarn ci
118 changes: 100 additions & 18 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,120 @@
name: Node.js Package

on:
release:
types: [created]
push:
pull_request:
types:
- closed
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
node-version: 20
- run: yarn ci

publish-npm:
needs: build
check-version:
runs-on: ubuntu-latest
if: github.event.pull_request.merged == true
outputs:
core-version-updated: ${{ steps.compare-versions.outputs.core-version-updated }}
react-version-updated: ${{ steps.compare-versions.outputs.react-version-updated }}
contracts-version-updated: ${{ steps.compare-versions.outputs.react-version-updated }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- name: Checkout main branch at commit before merge
uses: actions/checkout@v4
with:
node-version: 18
registry-url: https://registry.npmjs.org/
- run: yarn ci
- run: |
if [[ "${{ github.event.head_commit.message }}" =~ ^(docs:|chore:) ]]; then
echo "Skipping npm publish due to commit message."
ref: ${{ github.event.pull_request.base.sha }}

- name: Get package version from main branch before merge
id: pre-merge-version
run: |
CORE_PRE_MERGE_VERSION=$(node -p "require('./packages/module/package.json').version")
REACT_PRE_MERGE_VERSION=$(node -p "require('./packages/react/package.json').version")
CONTRACTS_PRE_MERGE_VERSION=$(node -p "require('./packages/contracts/package.json').version")
echo "core_pre_merge_version=$CORE_PRE_MERGE_VERSION" >> "$GITHUB_OUTPUT"
echo "react_pre_merge_version=$REACT_PRE_MERGE_VERSION" >> "$GITHUB_OUTPUT"
echo "react_pre_merge_version=$CONTRACTS_PRE_MERGE_VERSION" >> "$GITHUB_OUTPUT"
- name: Checkout main branch at commit after merge
uses: actions/checkout@v4
with:
ref: 'main'

- name: Get package version from main branch after merge
id: post-merge-version
run: |
CORE_POST_MERGE_VERSION=$(node -p "require('./packages/module/package.json').version")
REACT_POST_MERGE_VERSION=$(node -p "require('./packages/react/package.json').version")
CONTRACTS_POST_MERGE_VERSION=$(node -p "require('./packages/contracts/package.json').version")
echo "core_post_merge_version=$CORE_POST_MERGE_VERSION" >> "$GITHUB_OUTPUT"
echo "react_post_merge_version=$REACT_POST_MERGE_VERSION" >> "$GITHUB_OUTPUT"
echo "react_post_merge_version=$CONTRACTS_POST_MERGE_VERSION" >> "$GITHUB_OUTPUT"
- name: Compare versions
id: compare-versions
run: |
if [[ "${{ steps.pre-merge-version.outputs.core_pre_merge_version }}" != "${{ steps.post-merge-version.outputs.core_post_merge_version }}" ]]; then
echo "core-version-updated=true" >> "$GITHUB_OUTPUT"
else
echo "core-version-updated=false" >> "$GITHUB_OUTPUT"
fi
if [[ "${{ steps.pre-merge-version.outputs.react_pre_merge_version }}" != "${{ steps.post-merge-version.outputs.react_post_merge_version }}" ]]; then
echo "react-version-updated=true" >> "$GITHUB_OUTPUT"
else
cd packages/module && npm publish --access public
cd ../react && npm publish --access public
echo "react-version-updated=false" >> "$GITHUB_OUTPUT"
fi
if [[ "${{ steps.pre-merge-version.outputs.contracts_pre_merge_version }}" != "${{ steps.post-merge-version.outputs.contracts_post_merge_version }}" ]]; then
echo "contracts-version-updated=true" >> "$GITHUB_OUTPUT"
else
echo "contracts-version-updated=false" >> "$GITHUB_OUTPUT"
fi
publish-meshsdk-core:
needs: [build, check-version]
if: needs.check-version.outputs.core-version-updated == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://registry.npmjs.org/
- run: yarn ci
- run: cd packages/module && npm publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

publish-meshsdk-react:
needs: [build, check-version]
if: needs.check-version.outputs.react-version-updated == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://registry.npmjs.org/
- run: yarn ci
- run: cd packages/react && npm publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

publish-meshsdk-contracts:
needs: [build, check-version]
if: needs.check-version.outputs.contracts-version-updated == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://registry.npmjs.org/
- run: yarn ci
- run: cd packages/contracts && npm publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
5 changes: 3 additions & 2 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
"vitest": "0.28.4"
},
"peerDependencies": {
"@meshsdk/core": "1.5.11-beta.7"
"@meshsdk/core": "1.5.12"
},
"dependencies": {
"@harmoniclabs/plu-ts": "0.2.2"
"@harmoniclabs/plu-ts": "0.2.2",
"@meshsdk/mesh-csl": "^0.0.1-beta.1"
}
}
142 changes: 142 additions & 0 deletions packages/contracts/src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { MeshTxBuilder, IFetcher, UTxO, BrowserWallet } from '@meshsdk/core';

export type MeshTxInitiatorInput = {
mesh: MeshTxBuilder;
fetcher?: IFetcher;
wallet?: BrowserWallet;
};

export class MeshTxInitiator {
mesh: MeshTxBuilder;
fetcher?: IFetcher;
wallet?: BrowserWallet;

constructor({ mesh, fetcher, wallet }: MeshTxInitiatorInput) {
this.mesh = mesh;
if (fetcher) {
this.fetcher = fetcher;
}
if (wallet) {
this.wallet = wallet;
}
}

protected signSubmitReset = async () => {
const signedTx = this.mesh.completeSigning();
const txHash = await this.mesh.submitTx(signedTx);
this.mesh.meshTxBuilderBody = this.mesh.emptyTxBuilderBody();
return txHash;
};

protected queryUtxos = async (walletAddress: string): Promise<UTxO[]> => {
if (this.fetcher) {
const utxos = await this.fetcher.fetchAddressUTxOs(walletAddress);
return utxos;
}
return [];
};

protected getWalletDappAddress = async () => {
if (this.wallet) {
const usedAddresses = await this.wallet.getUsedAddresses();
if (usedAddresses.length > 0) {
return usedAddresses[0];
}
const unusedAddresses = await this.wallet.getUnusedAddresses();
if (unusedAddresses.length > 0) {
return unusedAddresses[0];
}
}
return '';
};

protected getWalletCollateral = async (): Promise<UTxO | undefined> => {
if (this.wallet) {
const utxos = await this.wallet.getCollateral();
return utxos[0];
}
return undefined;
};

protected getWalletUtxosWithMinLovelace = async (
lovelace: number,
providedUtxos: UTxO[] = []
) => {
let utxos: UTxO[] = providedUtxos;
if (this.wallet && (!providedUtxos || providedUtxos.length === 0)) {
utxos = await this.wallet.getUtxos();
}
return utxos.filter((u) => {
const lovelaceAmount = u.output.amount.find(
(a: any) => a.unit === 'lovelace'
)?.quantity;
return Number(lovelaceAmount) > lovelace;
});
};

protected getWalletUtxosWithToken = async (
assetHex: string,
userUtxos: UTxO[] = []
) => {
let utxos: UTxO[] = userUtxos;
if (this.wallet && userUtxos.length === 0) {
utxos = await this.wallet.getUtxos();
}
return utxos.filter((u) => {
const assetAmount = u.output.amount.find(
(a: any) => a.unit === assetHex
)?.quantity;
return Number(assetAmount) >= 1;
});
};

protected getAddressUtxosWithMinLovelace = async (
walletAddress: string,
lovelace: number,
providedUtxos: UTxO[] = []
) => {
let utxos: UTxO[] = providedUtxos;
if (this.fetcher && (!providedUtxos || providedUtxos.length === 0)) {
utxos = await this.fetcher.fetchAddressUTxOs(walletAddress);
}
return utxos.filter((u) => {
const lovelaceAmount = u.output.amount.find(
(a: any) => a.unit === 'lovelace'
)?.quantity;
return Number(lovelaceAmount) > lovelace;
});
};

protected getAddressUtxosWithToken = async (
walletAddress: string,
assetHex: string,
userUtxos: UTxO[] = []
) => {
let utxos: UTxO[] = userUtxos;
if (this.fetcher && userUtxos.length === 0) {
utxos = await this.fetcher.fetchAddressUTxOs(walletAddress);
}
return utxos.filter((u) => {
const assetAmount = u.output.amount.find(
(a: any) => a.unit === assetHex
)?.quantity;
return Number(assetAmount) >= 1;
});
};

protected getWalletInfoForTx = async () => {
const utxos = await this.wallet?.getUtxos();
const collateral = await this.getWalletCollateral();
const walletAddress = await this.getWalletDappAddress();
if (!utxos || utxos?.length === 0) {
throw new Error('No utxos found');
}
if (!collateral) {
throw new Error('No collateral found');
}
if (!walletAddress) {
throw new Error('No wallet address found');
}
return { utxos, collateral, walletAddress };
};
}
Empty file.
Empty file.
23 changes: 23 additions & 0 deletions packages/contracts/src/escrow/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Escrow

The escrow contract is a simple contract that allows two parties to agree on a transaction and then have the funds locked until the transaction is complete. The contract is designed to be used in a trustless manner, so that neither party can cheat the other.

Actors:
- initiator
- initiator is the party that starts the escrow
- initiator deposit assets into the escrow
- recipient
- recipient deposit assets into the escrow
- both parties
- either party can cancel the trade and all assets are returned to the original owner
- both parties must agree to the trade before the assets are released

Story:
When two parties agree on a transaction, they can deposit assets into the escrow and the recipient can withdraw the assets if the transaction is complete. if the transaction is not complete or canceled, the assets are returned to the original owner.

Endpoints:
- initiator deposit assets
- recipient deposit assets
- cancel trade
- complete trade
list, pay, report, dispute, claim?
6 changes: 6 additions & 0 deletions packages/contracts/src/giftcard/aiken-workspace/.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/
26 changes: 26 additions & 0 deletions packages/contracts/src/giftcard/aiken-workspace/aiken.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This file was generated by Aiken
# You typically do not need to edit this file

[[requirements]]
name = "aiken-lang/stdlib"
version = "1.7.0"
source = "github"

[[requirements]]
name = "sidan-lab/aiken-utils"
version = "0.0.1-beta"
source = "github"

[[packages]]
name = "aiken-lang/stdlib"
version = "1.7.0"
requirements = []
source = "github"

[[packages]]
name = "sidan-lab/aiken-utils"
version = "0.0.1-beta"
requirements = []
source = "github"

[etags]
19 changes: 19 additions & 0 deletions packages/contracts/src/giftcard/aiken-workspace/aiken.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name = "meshjs/giftcard"
version = "0.0.0"
license = "Apache-2.0"
description = "Aiken contracts for project 'meshjs/giftcard'"

[repository]
user = "meshjs"
project = "giftcard"
platform = "github"

[[dependencies]]
name = "aiken-lang/stdlib"
version = "1.7.0"
source = "github"

[[dependencies]]
name = "sidan-lab/aiken-utils"
version = "0.0.1-beta"
source = "github"
Loading

0 comments on commit 68b6400

Please sign in to comment.