Compare commits

..
Author SHA1 Message Date
github-actions[bot]andgithub-actions[bot] 4b10719047 Version Packages (#345)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-10-24 16:18:44 -07:00
Joaquim Verges 989cf7347c [React, SDK] Small fixes for useOffers and makeOffers (#346) 2022-10-24 23:09:32 +00:00
Joaquim Verges 72227b2e16 [SDK, React] Expose new useOffers and useMakeOffer hooks (#343) 2022-10-24 15:28:34 -07:00
19 changed files with 225 additions and 43 deletions
+2
View File
@@ -1,5 +1,7 @@
# @thirdweb-dev/auth
## 2.0.22
## 2.0.21
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@thirdweb-dev/auth",
"version": "2.0.21",
"version": "2.0.22",
"exports": {
"./next": {
"module": "./next/dist/thirdweb-dev-auth-next.esm.js",
+7
View File
@@ -1,5 +1,12 @@
# thirdweb
## 0.8.8
### Patch Changes
- Updated dependencies [[`72227b2`](https://github.com/thirdweb-dev/js/commit/72227b2e166a3a68bbb41cf2b389322f5b7547a2)]:
- @thirdweb-dev/sdk@3.4.1
## 0.8.7
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "thirdweb",
"main": "dist/cli/index.js",
"version": "0.8.7",
"version": "0.8.8",
"repository": "https://github.com/thirdweb-dev/js/tree/main/packages/cli",
"author": "thirdweb eng <[email protected]>",
"license": "Apache-2.0",
+6
View File
@@ -1,5 +1,11 @@
# @thirdweb-dev/react
## 3.4.1
### Patch Changes
- [#343](https://github.com/thirdweb-dev/js/pull/343) [`72227b2`](https://github.com/thirdweb-dev/js/commit/72227b2e166a3a68bbb41cf2b389322f5b7547a2) Thanks [@joaquim-verges](https://github.com/joaquim-verges)! - Expose new useOffers and useMakeOffer hooks
## 3.4.0
### Minor Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@thirdweb-dev/react",
"version": "3.4.0",
"version": "3.4.1",
"repository": "https://github.com/thirdweb-dev/js/tree/main/packages/react",
"author": "thirdweb eng <[email protected]>",
"license": "Apache-2.0",
@@ -3,13 +3,14 @@ import {
requiredParamInvariant,
} from "../../../core/query-utils/required-param";
import { useSDKChainId } from "../../providers/base";
import { BuyNowParams, MakeBidParams } from "../../types";
import { BuyNowParams, MakeBidParams, MakeOfferParams } from "../../types";
import {
cacheKeys,
invalidateContractAndBalances,
} from "../../utils/cache-keys";
import { useQueryWithNetwork } from "../query-utils/useQueryWithNetwork";
import { useAddress } from "../wallet";
import { useContractEvents } from "./contracts";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import type {
AuctionListing,
@@ -514,6 +515,88 @@ export function useMakeBid(contract: RequiredParam<Marketplace>) {
);
}
/**
* Use this to make an offer on direct or auction listing from your marketplace contract.
*
* @example
* ```jsx
* const Component = () => {
* const {
* mutate: makeOffer,
* isLoading,
* error,
* } = useMakeOffer(">>YourMarketplaceContractInstance<<");
*
* if (error) {
* console.error("failed to make a bid", error);
* }
*
* return (
* <button
* disabled={isLoading}
* onClick={() => makeOffer({ listingId: 1, pricePerToken: 0.5, quantity: 1 })}
* >
* Bid!
* </button>
* );
* };
* ```
*
* @param contract - an instance of a Marketplace contract
* @returns a mutation object that can be used to make a bid on an auction listing
* @beta
*/
export function useMakeOffer(contract: RequiredParam<Marketplace>) {
const activeChainId = useSDKChainId();
const contractAddress = contract?.getAddress();
const queryClient = useQueryClient();
const walletAddress = useAddress();
return useMutation(
async (data: MakeOfferParams) => {
invariant(walletAddress, "no wallet connected, cannot make bid");
requiredParamInvariant(contract, "No Contract instance provided");
return await contract.makeOffer(
data.listingId,
data.pricePerToken,
data.quantity,
);
},
{
onSettled: () =>
invalidateContractAndBalances(
queryClient,
contractAddress,
activeChainId,
),
},
);
}
/**
* Get all the offers for a listing
*
* @remarks Fetch all the offers for a specified direct or auction listing.
* @example
* ```javascript
* const { data: offers, isLoading, error } = useOffers(<YourMarketplaceContractInstance>, <listingId>);
* ```
*
* @param contract - an instance of a Marketplace contract
* @param listingId - the id of the listing to fetch offers for
*/
export function useOffers(
contract: RequiredParam<Marketplace>,
listingId: RequiredParam<BigNumberish>,
) {
const result = useContractEvents(contract, "NewOffer");
return {
...result,
data: result.data
?.filter((ev) => ev.data.listingId.eq(listingId))
?.map((ev) => ev.data),
};
}
/**
* Use this to buy out an auction listing from your marketplace contract.
*
+5
View File
@@ -212,6 +212,11 @@ export type ClaimNFTReturnType =
// MARKETPLACE //
export type MakeBidParams = { listingId: BigNumberish; bid: Price };
export type MakeOfferParams = {
listingId: BigNumberish;
pricePerToken: Price;
quantity?: Amount;
};
export type BuyNowParams<TListingType = ListingType> =
TListingType extends ListingType.Direct
+6
View File
@@ -1,5 +1,11 @@
# @thirdweb-dev/sdk
## 3.4.1
### Patch Changes
- [#343](https://github.com/thirdweb-dev/js/pull/343) [`72227b2`](https://github.com/thirdweb-dev/js/commit/72227b2e166a3a68bbb41cf2b389322f5b7547a2) Thanks [@joaquim-verges](https://github.com/joaquim-verges)! - Expose general marketplace.makeOffer() function
## 3.4.0
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@thirdweb-dev/sdk",
"version": "3.4.0",
"version": "3.4.1",
"description": "The main thirdweb SDK.",
"repository": "https://github.com/thirdweb-dev/js/tree/main/packages/sdk",
"license": "Apache-2.0",
+1 -2
View File
@@ -24,13 +24,12 @@ export const NATIVE_TOKENS: Record<
symbol: "WETH",
},
},
[ChainId.Goerli]: {
name: "Görli Ether",
symbol: "GOR",
decimals: 18,
wrapped: {
address: "0x0bb7509324ce409f7bbc4b701f932eaca9736ab7",
address: "0xb4fbf271143f4fbf7b91a5ded31805e42b2208d6",
name: "Wrapped Ether",
symbol: "WETH",
},
@@ -1,7 +1,12 @@
import { DEFAULT_QUERY_ALL_COUNT } from "../../../core/schema/QueryParams";
import { ListingNotFoundError } from "../../common";
import { isNativeToken } from "../../common/currency";
import { mapOffer } from "../../common/marketplace";
import { getRoleHash } from "../../common/role";
import {
NATIVE_TOKENS,
SUPPORTED_CHAIN_ID,
} from "../../constants";
import { ContractEncoder } from "../../core/classes/contract-encoder";
import { ContractEvents } from "../../core/classes/contract-events";
import { ContractInterceptor } from "../../core/classes/contract-interceptor";
@@ -17,6 +22,7 @@ import { NetworkOrSignerOrProvider, TransactionResult } from "../../core/types";
import { ListingType } from "../../enums";
import { MarketplaceContractSchema } from "../../schema/contracts/marketplace";
import { SDKOptions } from "../../schema/sdk-options";
import { Price } from "../../types/currency";
import { AuctionListing, DirectListing, Offer } from "../../types/marketplace";
import { MarketplaceFilter } from "../../types/marketplace/MarketPlaceFilter";
import { UnmappedOffer } from "../../types/marketplace/UnmappedOffer";
@@ -324,22 +330,22 @@ export class Marketplace implements UpdateableNetwork {
// get only the events for this listing id
const listingEvents = events.filter((e) => e.data.listingId.eq(listingId));
// derive the offers from the events
const offers = await Promise.all(
return await Promise.all(
listingEvents.map(async (e): Promise<Offer> => {
const offer = await mapOffer(
return await mapOffer(
this.contractWrapper.getProvider(),
BigNumber.from(listingId),
{
quantityWanted: e.data.quantityWanted,
pricePerToken: e.data.totalOfferAmount.div(e.data.quantityWanted),
pricePerToken: e.data.quantityWanted.gt(0)
? e.data.totalOfferAmount.div(e.data.quantityWanted)
: e.data.totalOfferAmount,
currency: e.data.currency,
offeror: e.data.offeror,
} as UnmappedOffer,
);
return offer;
}),
);
return offers;
}
/** ******************************
@@ -391,6 +397,60 @@ export class Marketplace implements UpdateableNetwork {
}
}
/**
* Make an offer for a Direct or Auction Listing
*
* @remarks Make an offer on a direct or auction listing
*
* @example
* ```javascript
* // The listing ID of the asset you want to offer on
* const listingId = 0;
* // The price you are willing to offer per token
* const pricePerToken = 0.5;
* // The quantity of tokens you want to receive for this offer
* const quantity = 1;
*
* await contract.makeOffer(
* listingId,
* pricePerToken,
* quantity,
* );
* ```
*/
public async makeOffer(
listingId: BigNumberish,
pricePerToken: Price,
quantity?: BigNumberish,
): Promise<TransactionResult> {
const listing = await this.contractWrapper.readContract.listings(listingId);
if (listing.listingId.toString() !== listingId.toString()) {
throw new ListingNotFoundError(this.getAddress(), listingId.toString());
}
const chainId = await this.contractWrapper.getChainID();
switch (listing.listingType) {
case ListingType.Direct: {
invariant(
quantity,
"quantity is required when making an offer on a direct listing",
);
return await this.direct.makeOffer(
listingId,
quantity,
isNativeToken(listing.currency)
? NATIVE_TOKENS[chainId as SUPPORTED_CHAIN_ID].wrapped.address
: listing.currency,
pricePerToken,
);
}
case ListingType.Auction: {
return await this.auction.makeBid(listingId, pricePerToken);
}
default:
throw Error(`Unknown listing type: ${listing.listingType}`);
}
}
/**
* Set the Auction bid buffer
* @remarks A percentage (e.g. 5%) in basis points (5% = 500, 100% = 10000). A new bid is considered to be a winning bid only if its bid amount is at least the bid buffer (e.g. 5%) greater than the previous winning bid. This prevents buyers from making very slightly higher bids to win the auctioned items.
+18 -28
View File
@@ -200,7 +200,7 @@ describe("Marketplace Contract", async () => {
});
const id = tx.id;
sdk.updateSignerOrProvider(samWallet);
await marketplaceContract.auction.makeBid(id, 0.1);
await marketplaceContract.makeOffer(id, 0.1);
});
it("Should list with native currency address", async () => {
@@ -429,7 +429,7 @@ describe("Marketplace Contract", async () => {
"The buyer should start with no tokens",
);
await marketplaceContract.direct.makeOffer(
await marketplaceContract.makeOffer(
directListingId,
10,
tokenAddress,
@@ -482,12 +482,7 @@ describe("Marketplace Contract", async () => {
"0",
"The buyer should start with no tokens",
);
await marketplaceContract.direct.makeOffer(
directListingId,
1,
customTokenContract.getAddress(),
0.05,
);
await marketplaceContract.makeOffer(directListingId, 0.05, 1);
await marketplaceContract.buyoutListing(directListingId, 1);
const balance = await dummyNftContract.balanceOf(bobWallet.address);
assert.equal(
@@ -517,12 +512,7 @@ describe("Marketplace Contract", async () => {
it("should allow offers to be made on direct listings", async () => {
sdk.updateSignerOrProvider(bobWallet);
await marketplaceContract.direct.makeOffer(
directListingId,
1,
tokenAddress,
"1",
);
await marketplaceContract.makeOffer(directListingId, 1, "1");
const offer = (await marketplaceContract.direct.getActiveOffer(
directListingId,
@@ -537,7 +527,7 @@ describe("Marketplace Contract", async () => {
assert.equal(offer.listingId.toString(), directListingId.toString());
sdk.updateSignerOrProvider(samWallet);
await marketplaceContract.direct.makeOffer(
await marketplaceContract.makeOffer(
directListingId,
1,
tokenAddress,
@@ -567,8 +557,8 @@ describe("Marketplace Contract", async () => {
it("should allow bids by the same person", async () => {
await sdk.updateSignerOrProvider(bobWallet);
await marketplaceContract.auction.makeBid(auctionListingId, 0.06);
await marketplaceContract.auction.makeBid(auctionListingId, 0.08);
await marketplaceContract.makeOffer(auctionListingId, 0.06);
await marketplaceContract.makeOffer(auctionListingId, 0.08);
const winningBid = (await marketplaceContract.auction.getWinningBid(
auctionListingId,
@@ -583,7 +573,7 @@ describe("Marketplace Contract", async () => {
it("should allow bids to be made on auction listings", async () => {
await sdk.updateSignerOrProvider(bobWallet);
await marketplaceContract.auction.makeBid(auctionListingId, 0.06);
await marketplaceContract.makeOffer(auctionListingId, 0.06);
let winningBid = (await marketplaceContract.auction.getWinningBid(
auctionListingId,
@@ -601,7 +591,7 @@ describe("Marketplace Contract", async () => {
// Make a higher winning bid
await sdk.updateSignerOrProvider(samWallet);
await marketplaceContract.auction.makeBid(auctionListingId, 0.09);
await marketplaceContract.makeOffer(auctionListingId, 0.09);
winningBid = (await marketplaceContract.auction.getWinningBid(
auctionListingId,
@@ -620,7 +610,7 @@ describe("Marketplace Contract", async () => {
it("should return all offers for a listing when queried", async () => {
// make an offer as bob
sdk.updateSignerOrProvider(bobWallet);
await marketplaceContract.direct.makeOffer(
await marketplaceContract.makeOffer(
directListingId,
1,
tokenAddress,
@@ -629,7 +619,7 @@ describe("Marketplace Contract", async () => {
// make an offer as sam
sdk.updateSignerOrProvider(samWallet);
await marketplaceContract.direct.makeOffer(
await marketplaceContract.makeOffer(
directListingId,
1,
tokenAddress,
@@ -725,7 +715,7 @@ describe("Marketplace Contract", async () => {
"0",
"The buyer should start with no tokens",
);
await marketplaceContract.auction.makeBid(auctionListingId, "20");
await marketplaceContract.makeOffer(auctionListingId, "20");
const balance = await dummyNftContract.balanceOf(bobWallet.address);
assert.equal(
@@ -748,7 +738,7 @@ describe("Marketplace Contract", async () => {
"0",
"The buyer should start with no tokens",
);
await marketplaceContract.auction.makeBid(auctionListingId, "2");
await marketplaceContract.makeOffer(auctionListingId, "2");
const winningBid = (await marketplaceContract.auction.getWinningBid(
auctionListingId,
@@ -782,9 +772,9 @@ describe("Marketplace Contract", async () => {
"0",
"The buyer should start with no tokens",
);
await marketplaceContract.auction.makeBid(auctionListingId, "2");
await marketplaceContract.makeOffer(auctionListingId, "2");
try {
await marketplaceContract.auction.makeBid(auctionListingId, "2.01");
await marketplaceContract.makeOffer(auctionListingId, "2.01");
// eslint-disable-next-line no-empty
} catch (err) {}
});
@@ -860,7 +850,7 @@ describe("Marketplace Contract", async () => {
});
it("should throw an error when trying to close an auction that already started (with bids)", async () => {
await marketplaceContract.auction.makeBid(auctionListingId, 0.06);
await marketplaceContract.makeOffer(auctionListingId, 0.06);
try {
await marketplaceContract.auction.cancelListing(auctionListingId);
assert.fail("should have thrown an error");
@@ -933,7 +923,7 @@ describe("Marketplace Contract", async () => {
await sdk.updateSignerOrProvider(bobWallet);
await marketplaceContract.auction.makeBid(listingId, 2);
await marketplaceContract.makeOffer(listingId, 2);
await fastForwardTime(60 * 60 * 24);
@@ -1185,7 +1175,7 @@ describe("Marketplace Contract", async () => {
// place a bid
sdk.updateSignerOrProvider(samWallet);
await marketplaceContract.auction.makeBid(listingId, 0.06);
await marketplaceContract.makeOffer(listingId, 0.06);
const minimumNextBid =
await marketplaceContract.auction.getMinimumNextBid(listingId);
+8
View File
@@ -1,5 +1,13 @@
# e2e-cra-5
## 0.1.2
### Patch Changes
- Updated dependencies [[`72227b2`](https://github.com/thirdweb-dev/js/commit/72227b2e166a3a68bbb41cf2b389322f5b7547a2), [`72227b2`](https://github.com/thirdweb-dev/js/commit/72227b2e166a3a68bbb41cf2b389322f5b7547a2)]:
- @thirdweb-dev/react@3.4.1
- @thirdweb-dev/sdk@3.4.1
## 0.1.1
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "e2e-cra-5",
"version": "0.1.1",
"version": "0.1.2",
"private": true,
"dependencies": {
"@thirdweb-dev/react": "*",
+8
View File
@@ -1,5 +1,13 @@
# e2e-next-12
## 0.1.2
### Patch Changes
- Updated dependencies [[`72227b2`](https://github.com/thirdweb-dev/js/commit/72227b2e166a3a68bbb41cf2b389322f5b7547a2), [`72227b2`](https://github.com/thirdweb-dev/js/commit/72227b2e166a3a68bbb41cf2b389322f5b7547a2)]:
- @thirdweb-dev/react@3.4.1
- @thirdweb-dev/sdk@3.4.1
## 0.1.1
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "e2e-next-12",
"version": "0.1.1",
"version": "0.1.2",
"private": true,
"scripts": {
"build": "next build",
+8
View File
@@ -1,5 +1,13 @@
# e2e-vite-3
## 0.0.2
### Patch Changes
- Updated dependencies [[`72227b2`](https://github.com/thirdweb-dev/js/commit/72227b2e166a3a68bbb41cf2b389322f5b7547a2), [`72227b2`](https://github.com/thirdweb-dev/js/commit/72227b2e166a3a68bbb41cf2b389322f5b7547a2)]:
- @thirdweb-dev/react@3.4.1
- @thirdweb-dev/sdk@3.4.1
## 0.0.1
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "e2e-vite-3",
"private": true,
"version": "0.0.1",
"version": "0.0.2",
"type": "module",
"scripts": {
"build": "vite build",