-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1fd95ff
commit 13c9b3a
Showing
13 changed files
with
928 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
packages/demo/components/pages/contracts/marketplace/buyAsset.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import Codeblock from '../../../ui/codeblock'; | ||
import Card from '../../../ui/card'; | ||
import SectionTwoCol from '../../../common/sectionTwoCol'; | ||
import Button from '../../../ui/button'; | ||
import { CardanoWallet, useWallet } from '@meshsdk/react'; | ||
import { useEffect, useState } from 'react'; | ||
import RunDemoResult from '../../../common/runDemoResult'; | ||
import { getMarketplace, asset, price } from './config'; | ||
import useLocalStorage from '../../../../hooks/useLocalStorage'; | ||
import Input from '../../../ui/input'; | ||
|
||
export default function MarketplaceBuyAsset() { | ||
return ( | ||
<> | ||
<SectionTwoCol | ||
sidebarTo="buyAsset" | ||
header="Buy Asset" | ||
leftFn={Left()} | ||
rightFn={Right()} | ||
/> | ||
</> | ||
); | ||
} | ||
|
||
function Left() { | ||
let code = `async marketplace.purchaseAsset(\n`; | ||
code += ` address: string,\n`; | ||
code += ` asset: string,\n`; | ||
code += ` price: number\n`; | ||
code += `)`; | ||
return ( | ||
<> | ||
<p> | ||
Purchase a listed asset from the marketplace. The seller will receive | ||
the listed price in ADA and the buyer will receive the asset. | ||
</p> | ||
<Codeblock data={code} isJson={false} /> | ||
<p> | ||
<code>address</code> is the seller's address. <code>asset</code> is the | ||
listed asset's <code>unit</code>. <code>price</code> is the listed price | ||
in Lovelace. | ||
</p> | ||
</> | ||
); | ||
} | ||
|
||
function Right() { | ||
const { connected, wallet } = useWallet(); | ||
const [loading, setLoading] = useState<boolean>(false); | ||
const [response, setResponse] = useState<null | any>(null); | ||
const [responseError, setResponseError] = useState<null | any>(null); | ||
const [userLocalStorage, setUserlocalStorage] = useLocalStorage( | ||
'meshMarketplaceDemo', | ||
{} | ||
); | ||
const [listPrice, updateListPrice] = useState<number>(price); | ||
const [sellerAddress, updateSellerAddress] = | ||
useState<string>('SELLER ADDRESS'); | ||
|
||
let code1 = ``; | ||
code1 += `const txHash = await marketplace.purchaseAsset(\n`; | ||
code1 += ` '${sellerAddress}',\n`; | ||
code1 += ` '${asset}',\n`; | ||
code1 += ` ${listPrice}\n`; | ||
code1 += `);\n`; | ||
|
||
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.purchaseAsset( | ||
sellerAddress, | ||
asset, | ||
listPrice | ||
); | ||
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" | ||
/> | ||
<Codeblock data={code1} isJson={false} /> | ||
{connected ? ( | ||
<> | ||
<Button | ||
onClick={() => rundemo()} | ||
style={ | ||
loading ? 'warning' : response !== null ? 'success' : 'light' | ||
} | ||
disabled={loading} | ||
> | ||
Purchase Listed Mesh Token | ||
</Button> | ||
<RunDemoResult response={response} /> | ||
</> | ||
) : ( | ||
<CardanoWallet /> | ||
)} | ||
<RunDemoResult response={responseError} label="Error" /> | ||
</Card> | ||
); | ||
} |
131 changes: 131 additions & 0 deletions
131
packages/demo/components/pages/contracts/marketplace/cancelListing.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import Codeblock from '../../../ui/codeblock'; | ||
import Card from '../../../ui/card'; | ||
import SectionTwoCol from '../../../common/sectionTwoCol'; | ||
import Button from '../../../ui/button'; | ||
import { CardanoWallet, useWallet } from '@meshsdk/react'; | ||
import { useEffect, useState } from 'react'; | ||
import RunDemoResult from '../../../common/runDemoResult'; | ||
import { getMarketplace, asset, price } from './config'; | ||
import useLocalStorage from '../../../../hooks/useLocalStorage'; | ||
import Input from '../../../ui/input'; | ||
|
||
export default function MarketplaceCancelAsset() { | ||
return ( | ||
<> | ||
<SectionTwoCol | ||
sidebarTo="cancelListing" | ||
header="Cancel Listing" | ||
leftFn={Left()} | ||
rightFn={Right()} | ||
/> | ||
</> | ||
); | ||
} | ||
|
||
function Left() { | ||
let code = `async marketplace.delistAsset(\n`; | ||
code += ` address: string,\n`; | ||
code += ` asset: string,\n`; | ||
code += ` price: number\n`; | ||
code += `)`; | ||
return ( | ||
<> | ||
<p> | ||
Cancel a listing on the marketplace. The seller can cancel the listing | ||
at any time. The seller will receive the listed asset back. | ||
</p> | ||
<p> | ||
<code>address</code> is the seller's address. <code>asset</code> is the | ||
listed asset's <code>unit</code>. <code>price</code> is the listed price | ||
in Lovelace. | ||
</p> | ||
<Codeblock data={code} isJson={false} /> | ||
</> | ||
); | ||
} | ||
|
||
function Right() { | ||
const { connected, wallet } = useWallet(); | ||
const [loading, setLoading] = useState<boolean>(false); | ||
const [response, setResponse] = useState<null | any>(null); | ||
const [responseError, setResponseError] = useState<null | any>(null); | ||
const [userLocalStorage, setUserlocalStorage] = useLocalStorage( | ||
'meshMarketplaceDemo', | ||
{} | ||
); | ||
|
||
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}`); | ||
} | ||
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 ? ( | ||
<> | ||
<Button | ||
onClick={() => rundemo()} | ||
style={ | ||
loading ? 'warning' : response !== null ? 'success' : 'light' | ||
} | ||
disabled={loading} | ||
> | ||
Cancel listing | ||
</Button> | ||
<RunDemoResult response={response} /> | ||
</> | ||
) : ( | ||
<CardanoWallet /> | ||
)} | ||
<RunDemoResult response={responseError} label="Error" /> | ||
</Card> | ||
); | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/demo/components/pages/contracts/marketplace/config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// 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; | ||
export const price = 10000000; | ||
|
||
export function getContract(wallet) { | ||
const blockchainProvider = new BlockfrostProvider( | ||
process.env.NEXT_PUBLIC_BLOCKFROST_API_KEY_PREPROD! | ||
); | ||
|
||
const meshTxBuilder = new MeshTxBuilder({ | ||
fetcher: blockchainProvider, | ||
submitter: blockchainProvider, | ||
}); | ||
|
||
const contract = new MeshMarketplaceContract( | ||
{ | ||
mesh: meshTxBuilder, | ||
fetcher: blockchainProvider, | ||
wallet: wallet, | ||
networkId: 0, | ||
}, | ||
'addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0uafhxhu32dys6pvn6wlw8dav6cmp4pmtv7cc3yel9uu0nq93swx9', | ||
200 | ||
); | ||
|
||
return contract; | ||
} |
Oops, something went wrong.