Skip to content

Commit

Permalink
fix: wip marketplace
Browse files Browse the repository at this point in the history
  • Loading branch information
jinglescode committed May 3, 2024
1 parent 11776df commit 02c7c01
Show file tree
Hide file tree
Showing 6 changed files with 1,067 additions and 1,045 deletions.
16 changes: 14 additions & 2 deletions packages/contracts/src/marketplace/offchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ import {
mConStr0,
} from '@meshsdk/mesh-csl';
import blueprint from './aiken-workspace/plutus.json';
import { UTxO, parseAssetUnit } from '@meshsdk/core';
import {
Quantity,
UTxO,
Unit,
keepRelevant,
parseAssetUnit,
} from '@meshsdk/core';

export type MarketplaceDatum = ConStr0<
[PubKeyAddress, Integer, CurrencySymbol, TokenName]
Expand Down Expand Up @@ -65,6 +71,11 @@ export class MeshMarketplaceContract extends MeshTxInitiator {

listAsset = async (asset: string, price: number) => {
const { utxos, walletAddress } = await this.getWalletInfoForTx();

const assetMap = new Map<Unit, Quantity>();
assetMap.set(asset, '1');
const selectedUtxos = keepRelevant(assetMap, utxos);

const scriptAddr = v2ScriptToBech32(
this.scriptCbor,
undefined,
Expand All @@ -78,7 +89,7 @@ export class MeshMarketplaceContract extends MeshTxInitiator {
.txOut(scriptAddr, tokenForSale)
.txOutInlineDatumValue(outputDatum, 'JSON')
.changeAddress(walletAddress)
.selectUtxosFrom(utxos)
.selectUtxosFrom(selectedUtxos)
.complete();

return this.mesh.txHex;
Expand Down Expand Up @@ -117,6 +128,7 @@ export class MeshMarketplaceContract extends MeshTxInitiator {
const { utxos, walletAddress, collateral } =
await this.getWalletInfoForTx();

console.log(4, 'utxos', utxos);
const inputDatum = parseDatumCbor<MarketplaceDatum>(
marketplaceUtxo.output.plutusData!
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function Right() {
try {
const contract = getContract(wallet);

const utxo = await contract.getUtxoByTxHash(userLocalStorage); // todo hinson: script address extracted from scriptCbor is not correct
const utxo = await contract.getUtxoByTxHash(userLocalStorage);

if (!utxo) {
setResponseError('Input utxo not found');
Expand Down
33 changes: 21 additions & 12 deletions packages/demo/components/pages/contracts/marketplace/buyAsset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function Right() {
const [response, setResponse] = useState<null | any>(null);
const [responseError, setResponseError] = useState<null | any>(null);
const [userLocalStorage, setUserlocalStorage] = useLocalStorage(
'meshMarketplaceDemo',
'mesh_marketplace_demo',
{}
);
const [listPrice, updateListPrice] = useState<number>(price);
Expand Down Expand Up @@ -78,17 +78,26 @@ function Right() {
setResponse(null);
setResponseError(null);

// try {
// const marketplace = getMarketplace(wallet);
// const txHash = await marketplace.purchaseAsset(
// sellerAddress,
// asset,
// listPrice
// );
// setResponse(txHash);
// } catch (error) {
// setResponseError(`${error}`);
// }
try {
const contract = getContract(wallet);

console.log(1, userLocalStorage);
const utxo = await contract.getUtxoByTxHash(userLocalStorage);

if (!utxo) {
setResponseError('Input utxo not found');
setLoading(false);
return;
}

const tx = await contract.purchaseAsset(utxo);
const signedTx = await wallet.signTx(tx);
const txHash = await wallet.submitTx(signedTx);
console.log(4, txHash);
// setResponse(txHash);
} catch (error) {
setResponseError(`${error}`);
}
setLoading(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ function Main() {
return (
<>
<MarketplaceListAsset />
{/* <MarketplaceBuyAsset />
<MarketplaceUpdateListing />
<MarketplaceBuyAsset />
{/* <MarketplaceUpdateListing />
<MarketplaceCancelAsset /> */}
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function Right() {
const txHash = await wallet.submitTx(signedTx);
setUserlocalStorage(txHash);
setResponse(txHash);
console.log(99)
} catch (error) {
setResponseError(`${error}`);
}
Expand Down

0 comments on commit 02c7c01

Please sign in to comment.