Skip to content

Commit

Permalink
Merge pull request #2800 from build-5/examples/auction
Browse files Browse the repository at this point in the history
Auction examples
  • Loading branch information
adamunchained authored Feb 12, 2024
2 parents cc70203 + e9c2e6a commit 799ffe9
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/docs/how-to/auction/create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: Create Auction
tags:
- how-to
- auction
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import DeepLink from '../../_admonitions/_deep_link.md'

Auction APIs allows you to run generic auction without or with an asset. Asset can be NFT.

<Tabs>
<TabItem value="otr" label="OTR">
To create auction, you must call [`AuctionOtrDataset`](../../reference-api/classes/AuctionOtrDataset.md#create) on `dataset(Dataset.AUCTION)`.
[`create`](../../reference-api/classes/AuctionOtrDataset.md#create) takes an object of type [`AuctionCreateTangleRequest`](../../reference-api/interfaces/AuctionCreateTangleRequest.md) as parameter.

```tsx file=../../../../packages/sdk/examples/auction/otr/create.ts#L4-L47
```

<DeepLink/>
</TabItem>
<TabItem value="https" label="HTTPS">
To create auction, you must call [`create`](../../reference-api/classes/AuctionDataset.md#create) on `dataset(Dataset.AUCTION)`.
[`create`](../../reference-api/classes/NftDataset.md#create) takes an object of type [`AuctionCreateRequest`](../../reference-api/interfaces/AuctionCreateRequest.md) as parameter.

```tsx file=../../../../packages/sdk/examples/auction/https/create.ts#L6-L63
```
</TabItem>
</Tabs>
3 changes: 3 additions & 0 deletions docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const sidebars = {
'how-to/project/create-project',
],
},
{
'Auction API': ['how-to/auction/create'],
},
{
'DAO Management': [
{
Expand Down
63 changes: 63 additions & 0 deletions packages/sdk/examples/auction/https/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Dataset, Network } from '@build-5/interfaces';
import { Build5, SoonaverseApiKey, https } from '@build-5/sdk';
import { address } from '../../utils/secret';
import { walletSign } from '../../utils/utils';

async function main() {
const origin = Build5.TEST;
const userSign = await walletSign(address.bech32, address);

try {
// To create a generic auction we send an https request with the needed params
let auction = await https(origin)
.project(SoonaverseApiKey[origin])
.dataset(Dataset.AUCTION)
.create({
address: address.bech32,
signature: userSign.signature,
publicKey: {
hex: userSign.publicKey,
network: Network.RMS,
},
body: {
auctionFloorPrice: 1000000,
auctionFrom: new Date(),
auctionLength: 8.64e7, // 1 day in milliseconds
maxBids: 1,
minimalBidIncrement: 1000000,
network: Network.RMS,
space: 'build5spaceid',
},
});

// Once auction is created we can bid on it using the https request
// The response will be a transaction object.
// Use the targetAddress on this transaction object to send funds and bid on the auction
const transction = await https(origin)
.project(SoonaverseApiKey[origin])
.dataset(Dataset.AUCTION)
.bid({
address: address.bech32,
signature: userSign.signature,
publicKey: {
hex: userSign.publicKey,
network: Network.RMS,
},
body: { auction: auction.uid },
});
console.log('Target address ', transction.payload.targetAddress);

// Once auction is no longer active we can re fetch it to see the winning bid and bidder
auction = await https(origin)
.project(SoonaverseApiKey[origin])
.dataset(Dataset.AUCTION)
.id(auction.uid)
.get();
console.log('Highest bid ', auction.auctionHighestBid);
console.log('Highest bidder ', auction.auctionHighestBidder);
} catch (error) {
console.error('Error: ', error);
}
}

main().then(() => process.exit());
47 changes: 47 additions & 0 deletions packages/sdk/examples/auction/otr/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Dataset, Network } from '@build-5/interfaces';
import { Build5, Build5OtrAddress, SoonaverseApiKey, https, otr } from '@build-5/sdk';

async function main() {
const origin = Build5.TEST;
// @ts-ignore
const otrAddress = Build5OtrAddress[origin];

try {
// To create a generic auction we send an otr request with the needed params
const otrCreateRequest = otr(otrAddress).dataset(Dataset.AUCTION).create({
auctionFloorPrice: 1000000,
auctionFrom: new Date(),
auctionLength: 8.64e7, // 1 day in milliseconds
maxBids: 1,
minimalBidIncrement: 1000000,
network: Network.RMS,
space: 'build5spaceid',
});

const fireflyDeeplink = otrCreateRequest.getFireflyDeepLink();
console.log(fireflyDeeplink);

// Once the request is sent the auction will be created and the funds will be credited back
// The response output's metadata will contain an auction filed with the auction uid.
// We can use this id to bid on the auction
const auctionUid = 'auction id retrieved from tangle';
const otrBidRequest = otr(otrAddress).dataset(Dataset.AUCTION).bid({
auction: auctionUid,
});
// Use this deepling to send funds and bid on the auction
console.log(otrBidRequest.getFireflyDeepLink());

// Once auction is no longer active we can fetch it to see the winning bid and bidder
const auction = await https(origin)
.project(SoonaverseApiKey[origin])
.dataset(Dataset.AUCTION)
.id(auctionUid)
.get();
console.log('Highest bid ', auction.auctionHighestBid);
console.log('Highest bidder ', auction.auctionHighestBidder);
} catch (error) {
console.error('Error: ', error);
}
}

main().then(() => process.exit());

0 comments on commit 799ffe9

Please sign in to comment.