hello world for starters
This commit is contained in:
parent
470e331b26
commit
1142c6ca6d
|
@ -2,3 +2,5 @@
|
|||
.idea
|
||||
_site/
|
||||
temp
|
||||
build/
|
||||
assets/
|
|
@ -0,0 +1,7 @@
|
|||
name = "aiken-lang/hello_world"
|
||||
version = "0.0.0"
|
||||
licences = ["Apache-2.0"]
|
||||
description = "Aiken contracts for project 'aiken-lang/hello_world'"
|
||||
dependencies = [
|
||||
{ name = "aiken-lang/stdlib", version = "main", source = "github" },
|
||||
]
|
|
@ -0,0 +1,9 @@
|
|||
import { Lucid } from "https://deno.land/x/lucid@0.8.3/mod.ts";
|
||||
|
||||
const lucid = await Lucid.new(undefined, "Preview");
|
||||
|
||||
const privateKey = lucid.utils.generatePrivateKey();
|
||||
await Deno.writeTextFile("key.sk", privateKey);
|
||||
|
||||
const address = await lucid.selectWalletFromPrivateKey(privateKey).wallet.address();
|
||||
await Deno.writeTextFile("key.addr", address);
|
|
@ -0,0 +1,65 @@
|
|||
import {
|
||||
Blockfrost,
|
||||
Constr,
|
||||
Data,
|
||||
Lucid,
|
||||
SpendingValidator,
|
||||
TxHash,
|
||||
fromHex,
|
||||
toHex
|
||||
} from "https://deno.land/x/lucid@0.8.3/mod.ts";
|
||||
import * as cbor from "https://deno.land/x/cbor@v1.4.1/index.js";
|
||||
|
||||
const lucid = await Lucid.new(
|
||||
new Blockfrost(
|
||||
"https://cardano-preview.blockfrost.io/api/v0",
|
||||
/* BLOCKFROST API KEY */
|
||||
),
|
||||
"Preview",
|
||||
);
|
||||
|
||||
lucid.selectWalletFromPrivateKey(await Deno.readTextFile("./key.sk"));
|
||||
|
||||
const validator = await readValidator("./assets/hello_world/spend/script.cbor");
|
||||
|
||||
// --- Supporting functions
|
||||
|
||||
async function readValidator(filepath: String): Promise<SpendingValidator> {
|
||||
return {
|
||||
type: "PlutusV2",
|
||||
script: toHex(cbor.encode(fromHex(await Deno.readTextFile(filepath)))),
|
||||
};
|
||||
}
|
||||
|
||||
const publicKeyHash = lucid.utils
|
||||
.getAddressDetails(await lucid.wallet.address())
|
||||
.paymentCredential
|
||||
.hash;
|
||||
|
||||
const datum = Data.to(new Constr(0, [ publicKeyHash ]));
|
||||
|
||||
const txLock = await lock(1000000, { into: validator, owner: datum });
|
||||
|
||||
await lucid.awaitTx(txLock);
|
||||
|
||||
console.log(`1 ADA locked into the contract
|
||||
Tx ID: ${txLock}
|
||||
Datum: ${datum}
|
||||
`);
|
||||
|
||||
// --- Supporting functions
|
||||
|
||||
async function lock(lovelace, { into, owner }): Promise<TxHash>{
|
||||
const contractAddress = lucid.utils.validatorToAddress(into);
|
||||
|
||||
const tx = await lucid
|
||||
.newTx()
|
||||
.payToContract(contractAddress, { inline: owner }, { lovelace })
|
||||
.complete();
|
||||
|
||||
const signedTx = await tx
|
||||
.sign()
|
||||
.complete();
|
||||
|
||||
return signedTx.submit();
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
import {
|
||||
Blockfrost,
|
||||
Constr,
|
||||
Data,
|
||||
Lucid,
|
||||
SpendingValidator,
|
||||
TxHash,
|
||||
fromHex,
|
||||
toHex,
|
||||
utf8ToHex
|
||||
} from "https://deno.land/x/lucid@0.8.3/mod.ts";
|
||||
import * as cbor from "https://deno.land/x/cbor@v1.4.1/index.js";
|
||||
|
||||
const lucid = await Lucid.new(
|
||||
new Blockfrost(
|
||||
"https://cardano-preview.blockfrost.io/api/v0",
|
||||
/* BLOCKFROST API KEY */
|
||||
),
|
||||
"Preview",
|
||||
);
|
||||
|
||||
lucid.selectWalletFromPrivateKey(await Deno.readTextFile("./key.sk"));
|
||||
|
||||
const validator = await readValidator("./assets/hello_world/spend/script.cbor");
|
||||
|
||||
const utxo = { txHash: '/* Tx ID from hello_world_lock */', outputIndex: 0 };
|
||||
|
||||
const redeemer = Data.to(new Constr(0, [ utf8ToHex("Hello, World!")]) );
|
||||
|
||||
const txUnlock = await unlock(utxo, { from: validator, using: redeemer });
|
||||
|
||||
await lucid.awaitTx(txUnlock);
|
||||
|
||||
console.log(`1 ADA recovered from the contract
|
||||
Tx ID: ${txUnlock}
|
||||
Redeemer: ${redeemer}
|
||||
`);
|
||||
|
||||
// --- Supporting functions
|
||||
|
||||
async function unlock(ref, { from, using }): Promise<TxHash> {
|
||||
const [utxo] = await lucid.utxosByOutRef([ref]);
|
||||
|
||||
const tx = await lucid
|
||||
.newTx()
|
||||
.collectFrom([utxo], using)
|
||||
.addSigner(await lucid.wallet.address())
|
||||
.attachSpendingValidator(from)
|
||||
.complete();
|
||||
|
||||
const signedTx = await tx
|
||||
.sign()
|
||||
.complete();
|
||||
|
||||
return signedTx.submit();
|
||||
}
|
||||
|
||||
async function readValidator(filepath: String): Promise<SpendingValidator> {
|
||||
return {
|
||||
type: "PlutusV2",
|
||||
script: toHex(cbor.encode(fromHex(await Deno.readTextFile(filepath)))),
|
||||
};
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
use aiken/hash.{Blake2b_224, Hash}
|
||||
use aiken/list
|
||||
use aiken/string
|
||||
use aiken/transaction.{ScriptContext}
|
||||
use aiken/transaction/credential.{VerificationKey}
|
||||
|
||||
pub type Datum {
|
||||
owner: Hash<Blake2b_224, VerificationKey>,
|
||||
}
|
||||
|
||||
pub type Redeemer {
|
||||
msg: ByteArray,
|
||||
}
|
||||
|
||||
pub fn spend(datum: Datum, redeemer: Redeemer, context: ScriptContext) -> Bool {
|
||||
let must_say_hello = string.from_bytearray(redeemer.msg) == "Hello, World!"
|
||||
|
||||
let must_be_signed =
|
||||
list.has(context.transaction.extra_signatories, datum.owner)
|
||||
|
||||
must_say_hello && must_be_signed
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
build/
|
|
@ -1,6 +0,0 @@
|
|||
name = "aiken/sample"
|
||||
version = "0.0.1"
|
||||
|
||||
dependencies = [
|
||||
{ name = "aiken-lang/stdlib", version = "main", source = "github" },
|
||||
]
|
|
@ -1 +0,0 @@
|
|||
addr1w9ke4flw57jhv5tjefm8xwkfdc56et559npkcycd6u9ascc07drtu
|
|
@ -1,5 +0,0 @@
|
|||
{
|
||||
"type": "PlutusScriptV2",
|
||||
"description": "Generated by Aiken",
|
||||
"cborHex": "58c058be0100002225333573464646464a666ae68008400452819191919800801919b8f001375c6600c600e01490001800800911192999aab9f00114a026464a666ae68c01000852889998030030008021aba2002375c6ae84004008dd619801180199801180180224000900819b97323001375c66004600600a900011b9900149010d48656c6c6f2c20576f726c64210022323330010014800000c888cccd5cd19b870040025742466600800866e0000d20023574400200246aae78dd50008a4c2d"
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
58be0100002225333573464646464a666ae68008400452819191919800801919b8f001375c6600c600e01490001800800911192999aab9f00114a026464a666ae68c01000852889998030030008021aba2002375c6ae84004008dd619801180199801180180224000900819b97323001375c66004600600a900011b9900149010d48656c6c6f2c20576f726c64210022323330010014800000c888cccd5cd19b870040025742466600800866e0000d20023574400200246aae78dd50008a4c2d
|
|
@ -1 +0,0 @@
|
|||
addr_test1wpke4flw57jhv5tjefm8xwkfdc56et559npkcycd6u9ascc5kelye
|
|
@ -1,7 +0,0 @@
|
|||
pub type Redeemer {
|
||||
signer: ByteArray,
|
||||
}
|
||||
|
||||
pub type Datum {
|
||||
random: ByteArray,
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
use aiken/builtin
|
||||
|
||||
test bar() {
|
||||
builtin.length_of_bytearray(#[2, 2, 3]) == 3
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
use aiken/list
|
||||
use aiken/string
|
||||
use aiken/hash.{Blake2b_224, Hash}
|
||||
use aiken/transaction.{ScriptContext}
|
||||
use aiken/transaction/credential.{VerificationKey}
|
||||
|
||||
pub type Datum {
|
||||
owner: Hash<Blake2b_224, VerificationKey>,
|
||||
}
|
||||
|
||||
pub type Redeemer {
|
||||
msg: ByteArray,
|
||||
}
|
||||
|
||||
pub type Dummy {
|
||||
Mannequin {
|
||||
hands: ByteArray,
|
||||
feet: Int,
|
||||
}
|
||||
Doll {
|
||||
hands: ByteArray,
|
||||
datum: Datum,
|
||||
feet: Int,
|
||||
}
|
||||
Puppet {
|
||||
hands: ByteArray,
|
||||
dummy: Dummy,
|
||||
}
|
||||
Statue {
|
||||
hands: ByteArray,
|
||||
boots: ByteArray,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn spending(datum: Datum, redeemer: Redeemer, context: Dummy) -> Bool {
|
||||
let must_say_hello = string.from_bytearray(redeemer.msg) == "Hello, World!"
|
||||
let must_be_signed = #(1, datum, #(redeemer, context ))
|
||||
// context.transaction.extra_signatories
|
||||
// |> list.any(fn(vk) { vk == datum.owner })
|
||||
|
||||
when must_be_signed is {
|
||||
#(a, b, #(c, Mannequin{ feet, ..})) -> feet == 2
|
||||
_ -> False
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
test spend_1(){
|
||||
spending(Datum{ owner: #[254]}, Redeemer{msg: string.to_bytearray("Hello, World!")}, Mannequin{hands: #[], feet: 2}) == True
|
||||
}
|
Loading…
Reference in New Issue