Skip to content

Commit 72ecd04

Browse files
committed
update giftcard
1 parent aa4629a commit 72ecd04

File tree

15 files changed

+189
-28
lines changed

15 files changed

+189
-28
lines changed

aiken-giftcard/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "aiken-giftcard",
33
"type": "module",
44
"dependencies": {
5-
"@meshsdk/core": "^1.9.0-beta.0",
5+
"@meshsdk/core": "^1.9.0-beta.1",
66
"dotenv": "^16.4.5",
77
"tsx": "^4.9.4"
88
},

aiken-giftcard/src/common.ts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {
22
BlockfrostProvider,
3+
builtinByteString,
34
MeshTxBuilder,
45
MeshWallet,
6+
outputReference,
57
serializePlutusScript,
68
UTxO,
79
} from "@meshsdk/core";
8-
import { applyParamsToScript } from "@meshsdk/core-csl";
10+
import { applyParamsToScript } from "@meshsdk/core-cst";
911
import dotenv from "dotenv";
1012
dotenv.config();
1113

@@ -45,18 +47,33 @@ export async function getWalletInfoForTx() {
4547

4648
export function getScript(
4749
blueprintCompiledCode: string,
48-
params: string[] = [],
49-
version: "V1" | "V2" | "V3" = "V3"
50+
tokenNameHex: string,
51+
utxoTxHash: string,
52+
utxoTxId: number,
53+
networkId = 0
5054
) {
51-
const scriptCbor = applyParamsToScript(blueprintCompiledCode, params);
55+
const utxo = outputReference(utxoTxHash, utxoTxId);
5256

53-
const scriptAddr = serializePlutusScript(
54-
{ code: scriptCbor, version: version },
57+
const scriptCbor = applyParamsToScript(
58+
blueprintCompiledCode,
59+
[builtinByteString(tokenNameHex), utxo],
60+
"JSON"
61+
);
62+
const { address } = serializePlutusScript(
63+
{ code: scriptCbor, version: "V3" },
5564
undefined,
56-
0
57-
).address;
65+
networkId
66+
);
5867

59-
return { scriptCbor, scriptAddr };
68+
return { scriptCbor, address };
69+
}
70+
71+
export function redeemCbor(
72+
blueprintCompiledCode: string,
73+
tokenNameHex: string,
74+
policyId: string
75+
) {
76+
return applyParamsToScript(blueprintCompiledCode, [tokenNameHex, policyId]);
6077
}
6178

6279
export function getTxBuilder() {
@@ -73,3 +90,16 @@ export async function getUtxoByTxHash(txHash: string): Promise<UTxO> {
7390
}
7491
return utxos[0];
7592
}
93+
94+
import * as readline from "node:readline/promises";
95+
96+
export async function prompt(question: string): Promise<string> {
97+
const rl = readline.createInterface({
98+
input: process.stdin,
99+
output: process.stdout,
100+
});
101+
102+
const res = await rl.question(question);
103+
rl.close();
104+
return res;
105+
}

aiken-giftcard/src/create.ts

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,61 @@
1-
import { Asset, deserializeAddress, mConStr0 } from "@meshsdk/core";
1+
import { Asset, mConStr0, resolveScriptHash, stringToHex } from "@meshsdk/core";
22
import { getScript, getTxBuilder, getWalletInfoForTx, wallet } from "./common";
33
import blueprint from "../aiken-workspace/plutus.json";
44

5+
const networkId = 0;
6+
57
export async function create(
68
tokenName: string,
79
assets: Asset[]
810
): Promise<string> {
9-
const { utxos, walletAddress } = await getWalletInfoForTx();
10-
const { scriptAddr } = getScript(blueprint.validators[0].compiledCode);
11-
return "";
11+
const { utxos, walletAddress, collateral } = await getWalletInfoForTx();
12+
const tokenNameHex = stringToHex(tokenName);
13+
14+
const firstUtxo = utxos[0];
15+
if (firstUtxo === undefined) throw new Error("No UTXOs available");
16+
const remainingUtxos = utxos.slice(1);
17+
const { scriptCbor, address } = getScript(
18+
blueprint.validators[0].compiledCode,
19+
tokenNameHex,
20+
firstUtxo.input.txHash,
21+
firstUtxo.input.outputIndex,
22+
networkId
23+
);
24+
25+
const giftCardPolicy = resolveScriptHash(scriptCbor, "V3");
26+
27+
const txBuilder = getTxBuilder();
28+
await txBuilder
29+
.txIn(
30+
firstUtxo.input.txHash,
31+
firstUtxo.input.outputIndex,
32+
firstUtxo.output.amount,
33+
firstUtxo.output.address
34+
)
35+
.mintPlutusScript("V3")
36+
.mint("1", giftCardPolicy, tokenNameHex)
37+
.mintingScript(scriptCbor)
38+
.mintRedeemerValue(mConStr0([]))
39+
.txOut(address, [
40+
...assets,
41+
{ unit: giftCardPolicy + tokenNameHex, quantity: "1" },
42+
])
43+
.txOutInlineDatumValue([
44+
firstUtxo.input.txHash,
45+
firstUtxo.input.outputIndex,
46+
tokenNameHex,
47+
])
48+
.changeAddress(walletAddress)
49+
.txInCollateral(
50+
collateral.input.txHash,
51+
collateral.input.outputIndex,
52+
collateral.output.amount,
53+
collateral.output.address
54+
)
55+
.selectUtxosFrom(remainingUtxos)
56+
.complete();
57+
58+
return txBuilder.txHex;
1259
}
1360

1461
async function main() {

aiken-giftcard/src/redeem.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { BuiltinByteString, Integer, List, mConStr1 } from "@meshsdk/common";
2+
import { deserializeDatum, resolveScriptHash, UTxO } from "@meshsdk/core";
3+
import {
4+
getScript,
5+
getTxBuilder,
6+
getUtxoByTxHash,
7+
getWalletInfoForTx,
8+
redeemCbor,
9+
wallet,
10+
prompt,
11+
} from "./common";
12+
import blueprint from "../aiken-workspace/plutus.json";
13+
14+
const networkId = 0;
15+
16+
export async function redeemGiftCard(giftCardUtxo: UTxO): Promise<string> {
17+
const { utxos, walletAddress, collateral } = await getWalletInfoForTx();
18+
19+
const inlineDatum = deserializeDatum<List>(
20+
giftCardUtxo.output.plutusData!
21+
).list;
22+
const paramTxHash = (inlineDatum[0] as BuiltinByteString).bytes;
23+
const paramTxId = (inlineDatum[1] as Integer).int as number;
24+
const tokenNameHex = (inlineDatum[2] as BuiltinByteString).bytes;
25+
26+
const { scriptCbor } = getScript(
27+
blueprint.validators[0].compiledCode,
28+
tokenNameHex,
29+
paramTxHash,
30+
paramTxId,
31+
networkId
32+
);
33+
34+
const giftCardPolicy = resolveScriptHash(scriptCbor, "V3");
35+
36+
const redeemScript = redeemCbor(
37+
blueprint.validators[0].compiledCode,
38+
tokenNameHex,
39+
giftCardPolicy
40+
);
41+
42+
const txBuilder = getTxBuilder();
43+
await txBuilder
44+
.spendingPlutusScript("V3")
45+
.txIn(
46+
giftCardUtxo.input.txHash,
47+
giftCardUtxo.input.outputIndex,
48+
giftCardUtxo.output.amount,
49+
giftCardUtxo.output.address
50+
)
51+
.spendingReferenceTxInInlineDatumPresent()
52+
.spendingReferenceTxInRedeemerValue("")
53+
.txInScript(redeemScript)
54+
.mintPlutusScript("V3")
55+
.mint("-1", giftCardPolicy, tokenNameHex)
56+
.mintingScript(scriptCbor)
57+
.mintRedeemerValue(mConStr1([]))
58+
.changeAddress(walletAddress)
59+
.txInCollateral(
60+
collateral.input.txHash,
61+
collateral.input.outputIndex,
62+
collateral.output.amount,
63+
collateral.output.address
64+
)
65+
.selectUtxosFrom(utxos)
66+
.complete();
67+
return txBuilder.txHex;
68+
}
69+
70+
async function main() {
71+
const txHashFromDesposit = await prompt(
72+
"Transaction hash from create giftcard: "
73+
);
74+
75+
const utxo = await getUtxoByTxHash(txHashFromDesposit);
76+
77+
const unsignedTx = await redeemGiftCard(utxo);
78+
79+
const signedTx = await wallet.signTx(unsignedTx);
80+
const txHash = await wallet.submitTx(signedTx);
81+
console.log("txHash", txHash);
82+
}
83+
84+
main();

aiken-hello-world/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "aiken-hello-world",
33
"type": "module",
44
"dependencies": {
5-
"@meshsdk/core": "^1.9.0-beta.0",
5+
"@meshsdk/core": "^1.9.0-beta.1",
66
"dotenv": "^16.4.5",
77
"tsx": "^4.9.4"
88
},

aiken-hello-world/src/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
serializePlutusScript,
66
UTxO,
77
} from "@meshsdk/core";
8-
import { applyParamsToScript } from "@meshsdk/core-csl";
8+
import { applyParamsToScript } from "@meshsdk/core-cst";
99
import dotenv from "dotenv";
1010
dotenv.config();
1111

aiken-vesting/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "aiken-vesting",
33
"type": "module",
44
"dependencies": {
5-
"@meshsdk/core": "^1.9.0-beta.0",
5+
"@meshsdk/core": "^1.9.0-beta.1",
66
"dotenv": "^16.4.5",
77
"tsx": "^4.9.4"
88
},

aiken-vesting/src/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
serializePlutusScript,
66
UTxO,
77
} from "@meshsdk/core";
8-
import { applyParamsToScript } from "@meshsdk/core-csl";
8+
import { applyParamsToScript } from "@meshsdk/core-cst";
99
import dotenv from "dotenv";
1010
dotenv.config();
1111

express/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"license": "ISC",
1111
"description": "",
1212
"dependencies": {
13-
"@meshsdk/core": "^1.9.0-beta.0",
13+
"@meshsdk/core": "^1.9.0-beta.1",
1414
"express": "^4.19.2"
1515
},
1616
"devDependencies": {

next-app-route/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"lint": "next lint"
1010
},
1111
"dependencies": {
12-
"@meshsdk/core": "^1.9.0-beta.0",
13-
"@meshsdk/react": "^1.9.0-beta.0",
12+
"@meshsdk/core": "^1.9.0-beta.1",
13+
"@meshsdk/react": "^1.9.0-beta.1",
1414
"next": "15.1.6",
1515
"react": "^19.0.0",
1616
"react-dom": "^19.0.0"

0 commit comments

Comments
 (0)