Skip to content

Commit

Permalink
feat: marketplace testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jinglescode committed May 3, 2024
1 parent 02c7c01 commit b82ba4b
Show file tree
Hide file tree
Showing 8 changed files with 1,124 additions and 1,187 deletions.
70 changes: 44 additions & 26 deletions packages/contracts/src/marketplace/offchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
UTxO,
Unit,
keepRelevant,
largestFirst,
parseAssetUnit,
} from '@meshsdk/core';

Expand Down Expand Up @@ -128,32 +129,18 @@ export class MeshMarketplaceContract extends MeshTxInitiator {
const { utxos, walletAddress, collateral } =
await this.getWalletInfoForTx();

console.log(4, 'utxos', utxos);
const inputDatum = parseDatumCbor<MarketplaceDatum>(
marketplaceUtxo.output.plutusData!
);
const inputLovelace = marketplaceUtxo.output.amount.find(
(a) => a.unit === 'lovelace'
)!.quantity;

const ownerAddress = this.ownerAddress;
const ownerToReceive = [
{
unit: 'lovelace',
quantity: Math.ceil(
(inputDatum.fields[1].int * this.feePercentageBasisPoint) / 10000
).toString(),
},
];
const sellerAddress = parsePlutusAddressObjToBech32(inputDatum.fields[0]);
const sellerToReceive = [
{
unit: 'lovelace',
quantity: (inputDatum.fields[1].int + Number(inputLovelace)).toString(),
},
];

await this.mesh
const listingPrice = inputDatum.fields[1].int.toString();
const selectedUtxos = largestFirst(listingPrice, utxos, true);

// const inputLovelace = marketplaceUtxo.output.amount.find(
// (a) => a.unit === 'lovelace'
// )!.quantity;

const tx = this.mesh
.spendingPlutusScriptV2()
.txIn(
marketplaceUtxo.input.txHash,
Expand All @@ -164,17 +151,48 @@ export class MeshMarketplaceContract extends MeshTxInitiator {
.spendingReferenceTxInInlineDatumPresent()
.spendingReferenceTxInRedeemerValue(mConStr0([]))
.txInScript(this.scriptCbor)
.txOut(ownerAddress, ownerToReceive)
.txOut(sellerAddress, sellerToReceive)
.changeAddress(walletAddress)
.txInCollateral(
collateral.input.txHash,
collateral.input.outputIndex,
collateral.output.amount,
collateral.output.address
)
.selectUtxosFrom(utxos)
.complete();
.selectUtxosFrom(selectedUtxos);

let ownerToReceiveLovelace =
(inputDatum.fields[1].int * this.feePercentageBasisPoint) / 10000;
if (this.feePercentageBasisPoint > 0 && ownerToReceiveLovelace < 1000000) {
ownerToReceiveLovelace = 1000000;
}

if (ownerToReceiveLovelace > 0) {
console.log(7, 'ownerToReceiveLovelace', ownerToReceiveLovelace);
const ownerAddress = this.ownerAddress;
const ownerToReceive = [
{
unit: 'lovelace',
quantity: Math.ceil(ownerToReceiveLovelace).toString(),
},
];
tx.txOut(ownerAddress, ownerToReceive);
}

const sellerToReceiveLovelace =
inputDatum.fields[1].int - ownerToReceiveLovelace;

if (sellerToReceiveLovelace > 0) {
console.log(8, 'sellerToReceiveLovelace', sellerToReceiveLovelace);
const sellerAddress = parsePlutusAddressObjToBech32(inputDatum.fields[0]);
const sellerToReceive = [
{
unit: 'lovelace',
quantity: sellerToReceiveLovelace.toString(),
},
];
tx.txOut(sellerAddress, sellerToReceive);
}
await tx.complete();

return this.mesh.txHex;
};
Expand Down
18 changes: 3 additions & 15 deletions packages/demo/components/pages/contracts/marketplace/buyAsset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function Right() {
try {
const contract = getContract(wallet);

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

if (!utxo) {
Expand All @@ -91,10 +91,10 @@ function Right() {
}

const tx = await contract.purchaseAsset(utxo);
const signedTx = await wallet.signTx(tx);
const signedTx = await wallet.signTx(tx, true);
const txHash = await wallet.submitTx(signedTx);
console.log(4, txHash);
// setResponse(txHash);
setResponse(txHash);
} catch (error) {
setResponseError(`${error}`);
}
Expand All @@ -103,18 +103,6 @@ function Right() {

return (
<Card>
<Input
value={sellerAddress}
onChange={(e) => updateSellerAddress(e.target.value)}
placeholder="Seller address"
label="Seller address"
/>
<Input
value={listPrice}
onChange={(e) => updateListPrice(e.target.value)}
placeholder="Listed price in Lovelace"
label="Listed price in Lovelace"
/>
<Codeblock data={code1} isJson={false} />
{connected ? (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,64 +50,40 @@ 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);
const [sellerAddress, updateSellerAddress] =
useState<string>('SELLER ADDRESS');

let code1 = ``;
code1 += `const txHash = await marketplace.delistAsset(\n`;
code1 += ` '${sellerAddress}',\n`;
code1 += ` '${asset}',\n`;
code1 += ` ${listPrice}\n`;
code1 += `);`;

useEffect(() => {
if (userLocalStorage.listPrice) {
updateListPrice(userLocalStorage.listPrice);
}
if (userLocalStorage.sellerAddress) {
updateSellerAddress(userLocalStorage.sellerAddress);
}
}, []);

async function rundemo() {
setLoading(true);
setResponse(null);
setResponseError(null);

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

const utxo = await contract.getUtxoByTxHash(userLocalStorage);

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

const tx = await contract.delistAsset(utxo);
const signedTx = await wallet.signTx(tx, true);
const txHash = await wallet.submitTx(signedTx);
setResponse(txHash);
} catch (error) {
setResponseError(`${error}`);
}
setLoading(false);
}

return (
<Card>
<Input
value={sellerAddress}
onChange={(e) => updateSellerAddress(e.target.value)}
placeholder="Seller address"
label="Seller address"
/>
<Input
value={listPrice}
onChange={(e) => updateListPrice(e.target.value)}
placeholder="Listing price in Lovelace"
label="Listing price in Lovelace"
/>

<Codeblock data={code1} isJson={false} />
{connected ? (
<>
Expand Down
25 changes: 2 additions & 23 deletions packages/demo/components/pages/contracts/marketplace/common.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
// import { BasicMarketplace } from '@meshsdk/contracts';
// import { BlockfrostProvider } from '@meshsdk/core';

import { MeshMarketplaceContract } from '@meshsdk/contracts';
import { BlockfrostProvider, MeshTxBuilder } from '@meshsdk/core';

// export function getMarketplace(wallet) {
// const blockchainProvider = new BlockfrostProvider(
// process.env.NEXT_PUBLIC_BLOCKFROST_API_KEY_PREPROD!
// );

// const marketplace = new BasicMarketplace({
// fetcher: blockchainProvider,
// initiator: wallet,
// network: 'preprod',
// signer: wallet,
// submitter: blockchainProvider,
// percentage: 25000, // 2.5%
// owner: 'addr_test1vpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0c7e4cxr',
// });

// return marketplace;
// }

const policyId = 'd9312da562da182b02322fd8acb536f37eb9d29fba7c49dc17255527';
const assetId = '4d657368546f6b656e';
export const asset = policyId + assetId;
Expand All @@ -44,8 +23,8 @@ export function getContract(wallet) {
wallet: wallet,
networkId: 0,
},
'addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0uafhxhu32dys6pvn6wlw8dav6cmp4pmtv7cc3yel9uu0nq93swx9',
200
'addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv06fwlvuacpyv59g3a3w2fhk7daa8aepvacnpamyhyyxrgnscrfpsa',
0 //100
);

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

return (
<>
<p>
Expand All @@ -54,71 +54,48 @@ 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);
const [sellerAddress, updateSellerAddress] =
useState<string>('SELLER ADDRESS');
const [newListPrice, updateNewListPrice] = useState<number>(20000000);

let code1 = ``;
code1 += `const txHash = await marketplace.relistAsset(\n`;
code1 += ` '${sellerAddress}',\n`;
code1 += ` '${asset}',\n`;
code1 += ` ${listPrice},\n`;
code1 += ` ${newListPrice}\n`;
code1 += `);\n`;

useEffect(() => {
if (userLocalStorage.listPrice) {
updateListPrice(userLocalStorage.listPrice);
updateNewListPrice(userLocalStorage.listPrice + 10000000);
}
if (userLocalStorage.sellerAddress) {
updateSellerAddress(userLocalStorage.sellerAddress);
}
}, []);
// code1 += `const txHash = await marketplace.relistAsset(\n`;
// code1 += ` '${sellerAddress}',\n`;
// code1 += ` '${asset}',\n`;
// code1 += ` ${listPrice},\n`;
// code1 += ` ${newListPrice}\n`;
// code1 += `);\n`;

async function rundemo() {
setLoading(true);
setResponse(null);
setResponseError(null);

// try {
// const marketplace = getMarketplace(wallet);
// const txHash = await marketplace.relistAsset(
// sellerAddress,
// asset,
// listPrice,
// newListPrice
// );
// setResponse(txHash);
try {
const contract = getContract(wallet);

const utxo = await contract.getUtxoByTxHash(userLocalStorage);

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

// setUserlocalStorage({
// sellerAddress: sellerAddress,
// listPrice: newListPrice,
// });
// } catch (error) {
// setResponseError(`${error}`);
// }
const tx = await contract.relistAsset(utxo, newListPrice);
const signedTx = await wallet.signTx(tx, true);
const txHash = await wallet.submitTx(signedTx);
setUserlocalStorage(txHash);
setResponse(txHash);
} catch (error) {
setResponseError(`${error}`);
}
setLoading(false);
}

return (
<Card>
<Input
value={sellerAddress}
onChange={(e) => updateSellerAddress(e.target.value)}
placeholder="Seller address"
label="Seller address"
/>
<Input
value={listPrice}
onChange={(e) => updateListPrice(e.target.value)}
placeholder="Listed price in Lovelace"
label="Listed price in Lovelace"
/>
<Input
value={newListPrice}
onChange={(e) => updateNewListPrice(e.target.value)}
Expand Down

0 comments on commit b82ba4b

Please sign in to comment.