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

Set state to busy or locked in gunJS #31

Open
wants to merge 1 commit into
base: main
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
161 changes: 121 additions & 40 deletions apps/web/cardano/off-chain/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { PlutusData, C, Lucid, Blockfrost, Tx, SpendingValidator } from 'lucid-cardano'
import { createLockingPolicyScript, DATUM_LABEL, Policy } from "../utils";
import Gun from 'gun'
import type { IGunInstance } from 'gun'
import data from '../../prisma/nft_metadata';

export const fromHex = (hex) => Buffer.from(hex, "hex");
export const toHex = (bytes) => Buffer.from(bytes).toString("hex");

Expand Down Expand Up @@ -100,7 +104,7 @@ export class LibraryRedeemer {
);
}
asPlutusDataHexString = () => {
return toHex(this.toPlutusData().to_bytes())
return toHex(this.toPlutusData().to_bytes())
}
}

Expand All @@ -110,6 +114,7 @@ export type Asset = {
}

export class LibraryValidator {

validatorAddress: string
spendingValidator: SpendingValidator

Expand All @@ -126,6 +131,7 @@ export class LibraryValidator {
asset: Asset,
adaPrice: number
) => {

asset.assetName = Buffer.from(asset.assetName, 'ascii').toString('hex')
await Lucid.initialize(
'Testnet',
Expand All @@ -136,20 +142,30 @@ export class LibraryValidator {

if (!walletAddr) return
const policy: Policy = createLockingPolicyScript(null, walletAddr)//address: string, expirationTime: Date, protocolParameters: ProtocolParameters

const lockTokenMint = {
assetName: 'CSlock' + Buffer.from(asset.assetName, 'hex').toString('ascii'),
quantity: '1',
policyId: policy.policyId,
policyScript: policy.script,
address: walletAddr
}

const fullName = policy.policyId + '.' + asset.assetName
const gun = Gun(['https://gun-server-1.glitch.me'])
const node = gun
.get("CardaSounds")
.get("testnet")
.get("assets")

const assetNode = gun.get(fullName)

const datum = new LibraryDatum({
lockTokenPolicy: lockTokenMint.policyId,
lockTokenName: Buffer.from(lockTokenMint.assetName, 'ascii').toString('hex'),
lovelacePrice: BigInt(adaPrice * 1000000)
}).asPlutusDataHexString()

let assets = {}
assets[asset.policyId + asset.assetName] = BigInt(1)
assets['lovelace'] = BigInt(2500000)
Expand All @@ -158,19 +174,28 @@ export class LibraryValidator {
mintAssets[lockTokenMint.policyId + Buffer.from(lockTokenMint.assetName, 'ascii').toString('hex')] = BigInt(1)

const tx = await Tx.new()
.attachMetadataWithConversion(DATUM_LABEL, { 0: "0x" + datum })
.attachMintingPolicy({
type: "Native",
script: Buffer.from(policy.script.to_bytes()).toString('hex')
})
.mintAssets(mintAssets)
.payToContract(this.validatorAddress, datum, assets)
.complete();
.attachMetadataWithConversion(DATUM_LABEL, { 0: "0x" + datum })
.attachMintingPolicy({
type: "Native",
script: Buffer.from(policy.script.to_bytes()).toString('hex')
})
.mintAssets(mintAssets)
.payToContract(this.validatorAddress, datum, assets)
.complete();

const signedTx = (await tx.sign()).complete();

const txHash = await signedTx.submit();

assetNode.put({
txhash: txHash,
policyId: policy.policyId,
policyScript: policy.script,
address: walletAddr,
status: "locked"
})
node.set(assetNode)

console.log({ submittedTxHash: txHash })
}

Expand Down Expand Up @@ -203,20 +228,37 @@ export class LibraryValidator {
policyScript: policy.script,
address: walletAddr
}

const fullName = policy.policyId + '.' + asset.assetName
const gun = Gun(['https://gun-server-1.glitch.me'])
const node = gun
.get("CardaSounds")
.get("testnet")
.get("assets")

const assetNode = gun.get(fullName)
var isBusy = false

assetNode.on((data) => {
console.log(data)
if (data.status === "busy") {
console.log("this NFT is busy")
isBusy = true
// return
}
})
const datum = new LibraryDatum({
lockTokenPolicy: lockTokenBurn.policyId,
lockTokenName: Buffer.from(lockTokenBurn.assetName, 'ascii').toString('hex'),
lovelacePrice: BigInt(adaPrice * 1000000)
}).asPlutusDataHexString()

const redeemer = new LibraryRedeemer(LibraryAction.Unlock).asPlutusDataHexString()
const redeemer = new LibraryRedeemer(LibraryAction.Unlock).asPlutusDataHexString()
// let walletUtxos = await Lucid.utxosAt(walletAddr)
// console.log('walletUtxos')
// console.log(walletUtxos)
let utxos = await Lucid.utxosAt(this.validatorAddress)//, asset.policyId + asset.assetName)
console.log({utxos: utxos})
if(!utxos)
console.log({ utxos: utxos })
if (!utxos)
throw "no validator utxos with an asset"
utxos = [utxos[3]]
utxos = utxos.map((utxo) => {
Expand All @@ -229,22 +271,33 @@ export class LibraryValidator {
[lockTokenBurn.policyId + Buffer.from(lockTokenBurn.assetName, 'ascii').toString('hex')]: BigInt(-1)
}

const tx = await Tx.new()
.attachMintingPolicy({
type: "Native",
script: Buffer.from(policy.script.to_bytes()).toString('hex')
})
.mintAssets(mintAssets)
.collectFrom(utxos, redeemer)
.attachSpendingValidator(this.spendingValidator)
.addSigner(walletAddr)
.complete();
if (!isBusy) {

const signedTx = (await tx.sign()).complete();
const tx = await Tx.new()
.attachMintingPolicy({
type: "Native",
script: Buffer.from(policy.script.to_bytes()).toString('hex')
})
.mintAssets(mintAssets)
.collectFrom(utxos, redeemer)
.attachSpendingValidator(this.spendingValidator)
.addSigner(walletAddr)
.complete();

const txHash = await signedTx.submit();
const signedTx = (await tx.sign()).complete();

const txHash = await signedTx.submit();
assetNode.put({
txhash: txHash,
policyId: policy.policyId,
policyScript: policy.script,
address: walletAddr,
status: "busy"
})
node.set(assetNode)
console.log({ submittedTxHash: txHash })
}

console.log({ submittedTxHash: txHash })
}

use = async (
Expand Down Expand Up @@ -277,6 +330,25 @@ export class LibraryValidator {
address: walletAddr
}

const fullName = policy.policyId + '.' + asset.assetName
const gun = Gun(['https://gun-server-1.glitch.me'])
const node = gun
.get("CardaSounds")
.get("testnet")
.get("assets")

const assetNode = gun.get(fullName)
var isBusy = false

assetNode.on((data) => {
console.log(data)
if (data.status === "busy") {
console.log("this NFT is busy")
isBusy = true
// return
}
})

const datum = new LibraryDatum({
lockTokenPolicy: lockTokenBurn.policyId,
lockTokenName: Buffer.from(lockTokenBurn.assetName, 'ascii').toString('hex'),
Expand All @@ -286,8 +358,8 @@ export class LibraryValidator {
const redeemer = new LibraryRedeemer(LibraryAction.Use).asPlutusDataHexString()

let utxos = await Lucid.utxosAt(this.validatorAddress)///, asset.policyId + asset.assetName)
console.log({utxos: utxos})
if(!utxos) {
console.log({ utxos: utxos })
if (!utxos) {
throw "no validator utxos with an asset"
}
utxos = [utxos[2]].map((utxo) => {
Expand All @@ -300,18 +372,27 @@ export class LibraryValidator {
[asset.policyId + asset.assetName]: BigInt(1),
['lovelace']: BigInt(6500000)
}
if (!isBusy) {

const tx = await Tx.new()
.collectFrom(utxos, redeemer)
.attachSpendingValidator(this.spendingValidator)
.payToContract(this.validatorAddress, datum, assets)
.addSigner(walletAddr)
.complete();

const signedTx = (await tx.sign()).complete();
const tx = await Tx.new()
.collectFrom(utxos, redeemer)
.attachSpendingValidator(this.spendingValidator)
.payToContract(this.validatorAddress, datum, assets)
.addSigner(walletAddr)
.complete();

const txHash = await signedTx.submit();
const signedTx = (await tx.sign()).complete();

console.log({ submittedTxHash: txHash })
const txHash = await signedTx.submit();
assetNode.put({
txhash: txHash,
policyId: policy.policyId,
policyScript: policy.script,
address: walletAddr,
status: "busy"
})
node.set(assetNode)
console.log({ submittedTxHash: txHash })
}
}
}
2 changes: 1 addition & 1 deletion apps/web/components/TestButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function TestButton() {
validator,
validatorAddressTestnet
)
plutusValidator.lock({assetName: "csnft19", policyId: "74f43bdf645aaeb25f39c6392cdb771ff4eb4da0c017cc183c490b8f"}, 2)
plutusValidator.lock({assetName: "985001", policyId: "06201e3bc4613cee82dfba9370b5e0470a1cb0c0c00715b8911b6a18"}, 2)

}

Expand Down
Loading