-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add document transfer, set-price, and purchase tutorials #114
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,55 @@ | ||
| import { setupDashClient } from '../setupDashClient.mjs'; | ||
|
|
||
| const { sdk, keyManager } = await setupDashClient(); | ||
| const { identity, identityKey, signer } = await keyManager.getAuth(); | ||
|
|
||
| // Purchase works with any document type whose contract enables `tradeMode`. | ||
| // Here the purchased document is the `domain` behind a DPNS name. | ||
| const DPNS_CONTRACT_ID = 'GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec'; | ||
| const NAME_LABEL = process.env.NAME_LABEL || 'alice'; | ||
|
|
||
| try { | ||
| // Fetch immediately before purchase so the revision, owner, and price are | ||
| // current. Platform rejects the purchase if the listing changes meanwhile. | ||
| const documents = await sdk.documents.query({ | ||
| dataContractId: DPNS_CONTRACT_ID, | ||
| documentTypeName: 'domain', | ||
| where: [ | ||
| ['normalizedParentDomainName', '==', 'dash'], | ||
| ['normalizedLabel', '==', NAME_LABEL.toLowerCase()], | ||
| ], | ||
| }); | ||
| const document = [...documents.values()][0]; | ||
|
|
||
| if (!document) { | ||
| throw new Error(`Name "${NAME_LABEL}.dash" was not found`); | ||
| } | ||
|
|
||
| // Buying your own listing is rejected by the platform | ||
| if (document.ownerId.toString() === identity.id.toString()) { | ||
| throw new Error(`"${NAME_LABEL}.dash" is already owned by ${identity.id}`); | ||
| } | ||
|
|
||
| // Read the native bigint directly. toJSON() cannot safely represent every | ||
| // possible unsigned 64-bit credit value. | ||
| const price = document.properties?.['$price']; | ||
| if (typeof price !== 'bigint' || price <= 0n) { | ||
| throw new Error(`Name "${NAME_LABEL}.dash" is not currently for sale`); | ||
| } | ||
|
|
||
| document.revision = BigInt(document.revision ?? 0) + 1n; | ||
|
|
||
| await sdk.documents.purchase({ | ||
| document, | ||
| buyerId: identity.id, | ||
| price, | ||
| identityKey, | ||
| signer, | ||
| }); | ||
|
|
||
| console.log( | ||
| `Document for "${NAME_LABEL}.dash" purchased for ${price} credits.`, | ||
| ); | ||
| } catch (e) { | ||
| console.error('Something went wrong:\n', e.message); | ||
| } |
This file contains hidden or 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 { setupDashClient } from '../setupDashClient.mjs'; | ||
|
|
||
| const { sdk, keyManager } = await setupDashClient(); | ||
| const { identity, identityKey, signer } = await keyManager.getAuth(); | ||
|
|
||
| // Pricing works with any document type whose contract enables `tradeMode`. | ||
| // This tutorial lists the `domain` document behind a DPNS name. | ||
| const DPNS_CONTRACT_ID = 'GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec'; | ||
| const NAME_LABEL = process.env.NAME_LABEL || 'alice'; | ||
|
|
||
| // Price in credits. A price of 0 removes the document from sale. | ||
| const PRICE = BigInt(process.env.DOCUMENT_PRICE || 100_000_000); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| try { | ||
| const documents = await sdk.documents.query({ | ||
| dataContractId: DPNS_CONTRACT_ID, | ||
| documentTypeName: 'domain', | ||
| where: [ | ||
| ['normalizedParentDomainName', '==', 'dash'], | ||
| ['normalizedLabel', '==', NAME_LABEL.toLowerCase()], | ||
| ], | ||
| }); | ||
| const document = [...documents.values()][0]; | ||
|
|
||
| if (!document) { | ||
| throw new Error(`Name "${NAME_LABEL}.dash" was not found`); | ||
| } | ||
|
|
||
| // Only the current owner can change a document's sale price | ||
| if (document.ownerId.toString() !== identity.id.toString()) { | ||
| throw new Error( | ||
| `"${NAME_LABEL}.dash" is owned by ${document.ownerId}, not ${identity.id}`, | ||
| ); | ||
| } | ||
|
|
||
| document.revision = BigInt(document.revision ?? 0) + 1n; | ||
|
|
||
| await sdk.documents.setPrice({ | ||
| document, | ||
| price: PRICE, | ||
| identityKey, | ||
| signer, | ||
| }); | ||
|
|
||
| console.log( | ||
| PRICE === 0n | ||
| ? `Document for "${NAME_LABEL}.dash" removed from sale.` | ||
| : `Document for "${NAME_LABEL}.dash" listed for ${PRICE} credits.`, | ||
| ); | ||
| } catch (e) { | ||
| console.error('Something went wrong:\n', e.message); | ||
| } | ||
This file contains hidden or 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,55 @@ | ||
| import { setupDashClient } from '../setupDashClient.mjs'; | ||
|
|
||
| const { sdk, keyManager } = await setupDashClient(); | ||
| const { identity, identityKey, signer } = await keyManager.getAuth(); | ||
|
|
||
| // Transfer works with any document type whose contract enables `transferable`. | ||
| // This tutorial uses a DPNS name because names are familiar transferable documents. | ||
| const DPNS_CONTRACT_ID = 'GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec'; | ||
| const NAME_LABEL = process.env.NAME_LABEL || 'alice'; | ||
| const DOCUMENT_RECIPIENT_ID = | ||
| process.env.DOCUMENT_RECIPIENT_ID || 'YOUR_DOCUMENT_RECIPIENT_ID'; | ||
|
|
||
| try { | ||
| // Check configuration before spending a network round-trip on the query | ||
| if (DOCUMENT_RECIPIENT_ID === 'YOUR_DOCUMENT_RECIPIENT_ID') { | ||
| throw new Error('Set DOCUMENT_RECIPIENT_ID to the new owner identity ID'); | ||
| } | ||
|
|
||
| const documents = await sdk.documents.query({ | ||
| dataContractId: DPNS_CONTRACT_ID, | ||
| documentTypeName: 'domain', | ||
| where: [ | ||
| ['normalizedParentDomainName', '==', 'dash'], | ||
| ['normalizedLabel', '==', NAME_LABEL.toLowerCase()], | ||
| ], | ||
| }); | ||
| const document = [...documents.values()][0]; | ||
|
|
||
| if (!document) { | ||
| throw new Error(`Name "${NAME_LABEL}.dash" was not found`); | ||
| } | ||
|
|
||
| // Only the current owner can transfer a document | ||
| if (document.ownerId.toString() !== identity.id.toString()) { | ||
| throw new Error( | ||
| `"${NAME_LABEL}.dash" is owned by ${document.ownerId}, not ${identity.id}`, | ||
| ); | ||
| } | ||
|
|
||
| document.revision = BigInt(document.revision ?? 0) + 1n; | ||
|
|
||
| // A successful transfer also clears any active sale price | ||
| await sdk.documents.transfer({ | ||
| document, | ||
| recipientId: DOCUMENT_RECIPIENT_ID, | ||
| identityKey, | ||
| signer, | ||
| }); | ||
|
|
||
| console.log( | ||
| `Document for "${NAME_LABEL}.dash" transferred to ${DOCUMENT_RECIPIENT_ID}.`, | ||
| ); | ||
| } catch (e) { | ||
| console.error('Something went wrong:\n', e.message); | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.