@zero-tech/zauction-sdk 0.2.10 → 0.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/package.json +3 -2
  2. package/src/actions/acceptBid.ts +167 -0
  3. package/src/actions/approveDomainTransfer.ts +25 -0
  4. package/src/actions/approveSpender.ts +27 -0
  5. package/src/actions/buyNow.ts +109 -0
  6. package/src/actions/cancelBid.ts +37 -0
  7. package/src/actions/getBuyNowListing.ts +65 -0
  8. package/src/actions/getPaymentTokenAllowance.ts +30 -0
  9. package/src/actions/index.ts +11 -0
  10. package/src/actions/isZAuctionApproved.ts +20 -0
  11. package/src/actions/placeBid.ts +102 -0
  12. package/src/actions/setBuyNowPrice.ts +56 -0
  13. package/src/actions/setNetworkPaymentToken.ts +46 -0
  14. package/src/api/actions/encodeBid.ts +44 -0
  15. package/src/api/actions/encodeCancelMessage.ts +24 -0
  16. package/src/api/actions/helpers.ts +61 -0
  17. package/src/api/actions/index.ts +6 -0
  18. package/src/api/actions/listBidsForAccount.ts +24 -0
  19. package/src/api/actions/listBidsForTokens.ts +39 -0
  20. package/src/api/actions/submitBid.ts +28 -0
  21. package/src/api/actions/submitCancelMessage.ts +30 -0
  22. package/src/api/client.ts +78 -0
  23. package/src/api/index.ts +1 -0
  24. package/src/api/types.ts +47 -0
  25. package/src/contracts/index.ts +53 -0
  26. package/src/contracts/types/IERC20.d.ts +294 -0
  27. package/src/contracts/types/IERC721.d.ts +472 -0
  28. package/src/contracts/types/IZNSHub.d.ts +578 -0
  29. package/src/contracts/types/ZAuction.d.ts +1487 -0
  30. package/src/contracts/types/ZAuctionV1.d.ts +440 -0
  31. package/src/contracts/types/commons.ts +36 -0
  32. package/src/contracts/types/factories/IERC20__factory.ts +203 -0
  33. package/src/contracts/types/factories/IERC721__factory.ts +308 -0
  34. package/src/contracts/types/factories/IZNSHub__factory.ts +311 -0
  35. package/src/contracts/types/factories/ZAuctionV1__factory.ts +326 -0
  36. package/src/contracts/types/factories/ZAuction__factory.ts +1033 -0
  37. package/src/contracts/types/index.ts +14 -0
  38. package/src/index.ts +468 -0
  39. package/src/subgraph/actions/index.ts +4 -0
  40. package/src/subgraph/actions/listAllBuyNowListings.ts +69 -0
  41. package/src/subgraph/actions/listAllSales.ts +57 -0
  42. package/src/subgraph/actions/listBuyNowSales.ts +44 -0
  43. package/src/subgraph/actions/listSales.ts +33 -0
  44. package/src/subgraph/client.ts +62 -0
  45. package/src/subgraph/helpers/index.ts +39 -0
  46. package/src/subgraph/index.ts +1 -0
  47. package/src/subgraph/queries.ts +75 -0
  48. package/src/subgraph/types.ts +58 -0
  49. package/src/types.ts +181 -0
  50. package/src/utilities/index.ts +2 -0
  51. package/src/utilities/logging.ts +27 -0
@@ -0,0 +1,46 @@
1
+ import { ethers } from "ethers";
2
+ import { getZAuctionContract, getZnsHubContract } from "../contracts";
3
+ import { Config } from "../types";
4
+ import { getLogger } from "../utilities";
5
+
6
+ const logger = getLogger("sdk:actions:setNetworkPaymentToken");
7
+
8
+ export const setNetworkPaymentToken = async (
9
+ networkId: string,
10
+ paymentToken: string,
11
+ signer: ethers.Signer,
12
+ config: Config
13
+ ): Promise<ethers.ContractTransaction> => {
14
+ logger.trace(
15
+ `Calling to set network payment token for ${networkId} with ${paymentToken}`
16
+ );
17
+ const contract = await getZAuctionContract(
18
+ config.web3Provider,
19
+ config.zAuctionAddress
20
+ );
21
+ const hub = await getZnsHubContract(
22
+ config.web3Provider,
23
+ config.znsHubAddress
24
+ );
25
+ const parent = await hub.parentOf(networkId);
26
+
27
+ if (!parent.eq(ethers.constants.HashZero)) {
28
+ throw Error("Can only set network payment tokens on network domains");
29
+ }
30
+ const zAuctionOwner = await contract.owner();
31
+ const signerAddress = await signer.getAddress();
32
+
33
+ if (signerAddress !== zAuctionOwner) {
34
+ throw Error(
35
+ "Cannot set a network's token for zAuction if you are not the owner"
36
+ );
37
+ }
38
+
39
+ const tx = await contract
40
+ .connect(signer)
41
+ .setNetworkToken(networkId, paymentToken);
42
+ logger.trace(
43
+ `Call to setNetwnetwork payment token for ${networkId} with ${paymentToken}`
44
+ );
45
+ return tx;
46
+ };
@@ -0,0 +1,44 @@
1
+ import { BidMessage, BidParameters, SignableBid } from "../types";
2
+ import { makeApiCall } from "./helpers";
3
+ import { getLogger } from "../../utilities";
4
+
5
+ const logger = getLogger("api:actions:encodeBid");
6
+
7
+ interface EncodeBidDto {
8
+ payload: string;
9
+ bidNonce: string;
10
+ nftId: string;
11
+ }
12
+
13
+ export const encodeBid = async (
14
+ apiUrl: string,
15
+ bidParams: BidParameters
16
+ ): Promise<SignableBid> => {
17
+ const uri = `${apiUrl}/bid`;
18
+
19
+ logger.trace(
20
+ `Encoding bid at ${uri} by ${bidParams.bidder} for amount ${bidParams.amount} of ERC20 token ${bidParams.bidToken}`
21
+ );
22
+
23
+ const response = await makeApiCall<EncodeBidDto>(uri, "POST", {
24
+ bidAmount: bidParams.amount,
25
+ tokenId: bidParams.tokenId,
26
+ contractAddress: bidParams.contract,
27
+ minimumBid: "0",
28
+ startBlock: bidParams.startBlock,
29
+ expireBlock: bidParams.expireBlock,
30
+ bidToken: bidParams.bidToken,
31
+ });
32
+
33
+ const bidToSign: SignableBid = {
34
+ bid: {
35
+ bidNonce: response.bidNonce,
36
+ ...bidParams,
37
+ } as BidMessage,
38
+ message: response.payload,
39
+ };
40
+ logger.trace(
41
+ `Created bid to sign ${bidToSign.message} with bid nonce ${bidToSign.bid.bidNonce}`
42
+ );
43
+ return bidToSign;
44
+ };
@@ -0,0 +1,24 @@
1
+ import { makeApiCall } from "./helpers";
2
+ import { getLogger } from "../../utilities";
3
+
4
+ const logger = getLogger("api:actions:encodeCancelMessage");
5
+
6
+ interface EncodeCancelBidDto {
7
+ hashedCancelMessage: string;
8
+ }
9
+
10
+ export const encodeCancelMessage = async (
11
+ apiUrl: string,
12
+ signedBidMessage: string
13
+ ): Promise<string> => {
14
+ const uri = `${apiUrl}/bid/cancel/encode`;
15
+ logger.trace(
16
+ `Calling to encode a cancel message at ${uri} for bid with a signed message of ${signedBidMessage}`
17
+ );
18
+ const response = await makeApiCall<EncodeCancelBidDto>(uri, "POST", {
19
+ bidMessageSignature: signedBidMessage,
20
+ });
21
+
22
+ logger.trace(`Created message ${response.hashedCancelMessage} for signing`);
23
+ return response.hashedCancelMessage;
24
+ };
@@ -0,0 +1,61 @@
1
+ import fetch from "cross-fetch";
2
+ import { Bid } from "../../types";
3
+ import { BidDto } from "../types";
4
+
5
+ export const makeApiCall = async <T>(
6
+ url: string,
7
+ method: "GET" | "POST",
8
+ body?: string | Record<string, unknown>,
9
+ softFail?: boolean,
10
+ softFailMessage?: string
11
+ ): Promise<T> => {
12
+ const headers: Record<string, string> = {};
13
+
14
+ if (body) {
15
+ if (typeof body !== "string") {
16
+ body = JSON.stringify(body);
17
+ headers["Content-Type"] = "application/json";
18
+ headers["Access-Control-Allow-Origin"] = "*";
19
+ }
20
+ }
21
+
22
+ const res = await fetch(url, {
23
+ method,
24
+ body,
25
+ headers,
26
+ });
27
+
28
+ if (res.status !== 200 && !softFail) {
29
+ throw Error(`Request failed with code ${res.status}: ${await res.text()}`);
30
+ }
31
+
32
+ if (softFail) {
33
+ const returnedBody = await res.json();
34
+ returnedBody["softFailMessage"] = softFailMessage;
35
+ return returnedBody as T;
36
+ } else {
37
+ const returnedBody = await res.json();
38
+ return returnedBody as T;
39
+ }
40
+ };
41
+
42
+ export const convertBidDtoToBid = (
43
+ bid: BidDto,
44
+ wildTokenAddress: string
45
+ ): Bid => {
46
+ const localBid: Bid = {
47
+ bidNonce: bid.bidNonce,
48
+ bidder: bid.account,
49
+ bidToken: bid.bidToken ?? wildTokenAddress,
50
+ contract: bid.contractAddress,
51
+ tokenId: bid.tokenId,
52
+ amount: bid.bidAmount,
53
+ startBlock: bid.startBlock,
54
+ expireBlock: bid.expireBlock,
55
+ signedMessage: bid.signedMessage,
56
+ timestamp: bid.date.toString(),
57
+ version: bid.version,
58
+ };
59
+
60
+ return localBid;
61
+ };
@@ -0,0 +1,6 @@
1
+ export * from "./listBidsForTokens";
2
+ export * from "./listBidsForAccount";
3
+ export * from "./encodeBid";
4
+ export * from "./submitBid";
5
+ export * from "./encodeCancelMessage";
6
+ export * from "./submitCancelMessage";
@@ -0,0 +1,24 @@
1
+ import { Bid } from "../../types";
2
+ import { BidDto } from "../types";
3
+ import { convertBidDtoToBid, makeApiCall } from "./helpers";
4
+ import { getLogger } from "../../utilities";
5
+
6
+ const logger = getLogger("api:actions:listBidsForAccount");
7
+
8
+ type AccountBidsDto = BidDto[];
9
+
10
+ export const listBidsForAccount = async (
11
+ apiUrl: string,
12
+ account: string,
13
+ wildTokenAddress: string
14
+ ): Promise<Bid[]> => {
15
+ const uri = `${apiUrl}/bids/accounts/${account}`;
16
+ logger.trace(`Calling ${uri} to get bids for account ${account}`);
17
+ const response = await makeApiCall<AccountBidsDto>(uri, "GET");
18
+
19
+ const bids: Bid[] = response.map((e) =>
20
+ convertBidDtoToBid(e, wildTokenAddress)
21
+ );
22
+ logger.trace(`Found ${bids.length} bids created by $${account}`);
23
+ return bids;
24
+ };
@@ -0,0 +1,39 @@
1
+ import { TokenBidCollection, TokenBidFilter } from "../../types";
2
+ import { BidDto } from "../types";
3
+ import { convertBidDtoToBid, makeApiCall } from "./helpers";
4
+
5
+ import { getLogger } from "../../utilities";
6
+
7
+ const logger = getLogger("api:actions:listBidsForTokens");
8
+
9
+ interface BidsBulkDto {
10
+ [tokenId: string]: BidDto[];
11
+ }
12
+
13
+ export const listBidsForTokens = async (
14
+ apiUrl: string,
15
+ wildTokenAddress: string,
16
+ tokenIds: string[],
17
+ filter?: TokenBidFilter
18
+ ): Promise<TokenBidCollection> => {
19
+ const bidCollection: TokenBidCollection = {};
20
+ let uri = `${apiUrl}/bids/list`;
21
+ uri = filter ? uri.concat(`?filter=${filter}`) : uri;
22
+ logger.trace(`Calling ${uri} to get bids for ${tokenIds.length} domains`);
23
+ const response = await makeApiCall<BidsBulkDto>(uri, "POST", {
24
+ tokenIds,
25
+ });
26
+
27
+ let totalBidsForDomains = 0;
28
+ for (const [tokenId, bids] of Object.entries(response)) {
29
+ bidCollection[tokenId] = bids.map((e) =>
30
+ convertBidDtoToBid(e, wildTokenAddress)
31
+ );
32
+ totalBidsForDomains += bids.length;
33
+ }
34
+ logger.trace(
35
+ `Found ${totalBidsForDomains} bids for $${tokenIds.length} domains`
36
+ );
37
+
38
+ return bidCollection;
39
+ };
@@ -0,0 +1,28 @@
1
+ import { SignedBid } from "../types";
2
+ import { makeApiCall } from "./helpers";
3
+ import { getLogger } from "../../utilities";
4
+
5
+ const logger = getLogger("api:actions:submitBid");
6
+
7
+ export const submitBid = async (
8
+ apiUrl: string,
9
+ signedBid: SignedBid
10
+ ): Promise<void> => {
11
+ const uri = `${apiUrl}/bids`;
12
+ logger.trace(
13
+ `Calling ${uri} to submit bid with bid nonce ${signedBid.bid.bidNonce}}`
14
+ );
15
+
16
+ await makeApiCall(uri, "POST", {
17
+ account: signedBid.bid.bidder,
18
+ bidNonce: signedBid.bid.bidNonce,
19
+ tokenId: signedBid.bid.tokenId,
20
+ contractAddress: signedBid.bid.contract,
21
+ bidAmount: signedBid.bid.amount,
22
+ signedMessage: signedBid.signedMessage,
23
+ minimumBid: "0",
24
+ startBlock: signedBid.bid.startBlock,
25
+ expireBlock: signedBid.bid.expireBlock,
26
+ bidToken: signedBid.bid.bidToken,
27
+ });
28
+ };
@@ -0,0 +1,30 @@
1
+ import { makeApiCall } from "./helpers";
2
+ import { getLogger } from "../../utilities";
3
+
4
+ const logger = getLogger("api:actions:submitBid");
5
+
6
+ export const submitCancelMessage = async (
7
+ apiUrl: string,
8
+ signedCancelMessage: string,
9
+ signedBidMessage: string
10
+ ): Promise<void> => {
11
+ const uri = `${apiUrl}/bid/cancel`;
12
+ logger.trace(
13
+ `Calling ${uri} to submit cancelled bid with signed message${signedCancelMessage}}`
14
+ );
15
+
16
+ const softFailMessage =
17
+ "Already cancelled in the API but allowing execution to continue to cancel in the contract as well";
18
+
19
+ // Soft fail if the bid isn't found in the API
20
+ await makeApiCall(
21
+ uri,
22
+ "POST",
23
+ {
24
+ cancelMessageSignature: signedCancelMessage,
25
+ bidMessageSignature: signedBidMessage,
26
+ },
27
+ true,
28
+ softFailMessage
29
+ );
30
+ };
@@ -0,0 +1,78 @@
1
+ import { TokenBidCollection, TokenBidFilter } from "../types";
2
+ import * as actions from "./actions";
3
+ import { Bid, BidParameters, SignableBid, SignedBid } from "./types";
4
+ import { getLogger } from "../utilities";
5
+
6
+ export interface ApiClient {
7
+ encodeBid: (bidParams: BidParameters) => Promise<SignableBid>;
8
+ submitBid: (signedBid: SignedBid) => Promise<void>;
9
+ encodeCancelBid: (signedBidMessage: string) => Promise<string>;
10
+ submitCancelBid: (
11
+ cancelMessageSignature: string,
12
+ bidMessageSignature: string
13
+ ) => Promise<void>;
14
+ listBidsForTokens: (
15
+ tokenIds: string[],
16
+ wildTokenAddress: string,
17
+ filter?: TokenBidFilter
18
+ ) => Promise<TokenBidCollection>;
19
+ listBidsByAccount: (
20
+ account: string,
21
+ wildTokenAddress: string
22
+ ) => Promise<Bid[]>;
23
+ }
24
+
25
+ export const createClient = (apiUri: string): ApiClient => {
26
+ const logger = getLogger("api:client");
27
+ const apiClient: ApiClient = {
28
+ encodeBid: async (bidParams: BidParameters): Promise<SignableBid> => {
29
+ logger.debug(`Encode bid with params ${bidParams}`);
30
+ return actions.encodeBid(apiUri, bidParams);
31
+ },
32
+ submitBid: (signedBid: SignedBid): Promise<void> => {
33
+ logger.debug(`Submit bid with signed message ${signedBid.signedMessage}`);
34
+ return actions.submitBid(apiUri, signedBid);
35
+ },
36
+ encodeCancelBid: async (signedBidMessage: string): Promise<string> => {
37
+ logger.debug(
38
+ `Encode to cancel a bid with signed message ${signedBidMessage}}`
39
+ );
40
+ return actions.encodeCancelMessage(apiUri, signedBidMessage);
41
+ },
42
+ submitCancelBid: async (
43
+ signedCancelMessage: string,
44
+ signedBidMessage: string
45
+ ): Promise<void> => {
46
+ logger.debug(
47
+ `Submit cancelled bid with signed message ${signedCancelMessage}`
48
+ );
49
+ actions.submitCancelMessage(
50
+ apiUri,
51
+ signedCancelMessage,
52
+ signedBidMessage
53
+ );
54
+ },
55
+ listBidsForTokens: (
56
+ tokenIds: string[],
57
+ wildTokenAddress: string,
58
+ filter?: TokenBidFilter
59
+ ): Promise<TokenBidCollection> => {
60
+ logger.debug(`List bids for domains ${tokenIds}`);
61
+ return actions.listBidsForTokens(
62
+ apiUri,
63
+ wildTokenAddress,
64
+ tokenIds,
65
+ filter
66
+ );
67
+ },
68
+ listBidsByAccount: (
69
+ account: string,
70
+ wildTokenAddress: string
71
+ ): Promise<Bid[]> => {
72
+ logger.debug(`List bids by account ${account}`);
73
+ return actions.listBidsForAccount(apiUri, account, wildTokenAddress);
74
+ },
75
+ };
76
+
77
+ return apiClient;
78
+ };
@@ -0,0 +1 @@
1
+ export * from "./client";
@@ -0,0 +1,47 @@
1
+ export interface BidParamsDto {
2
+ account: string;
3
+ bidNonce: string;
4
+ bidAmount: string;
5
+ minimumBid: string;
6
+ contractAddress: string;
7
+ startBlock: string;
8
+ expireBlock: string;
9
+ tokenId: string;
10
+ bidToken?: string;
11
+ }
12
+
13
+ export interface BidDto extends BidParamsDto {
14
+ date: number;
15
+ signedMessage: string;
16
+ version: "1.0" | "2.0";
17
+ }
18
+
19
+ export interface BidParameters {
20
+ bidder: string;
21
+ contract: string;
22
+ tokenId: string;
23
+ amount: string;
24
+ startBlock: string;
25
+ expireBlock: string;
26
+ bidToken: string;
27
+ }
28
+
29
+ export interface BidMessage extends BidParameters {
30
+ bidNonce: string;
31
+ }
32
+
33
+ export interface SignableBid {
34
+ bid: BidMessage;
35
+ message: string;
36
+ }
37
+
38
+ export interface SignedBid {
39
+ bid: BidMessage;
40
+ signedMessage: string;
41
+ }
42
+
43
+ export interface Bid extends BidMessage {
44
+ signedMessage: string;
45
+ timestamp: string;
46
+ version: "1.0" | "2.0";
47
+ }
@@ -0,0 +1,53 @@
1
+ import { ethers } from "ethers";
2
+ import {
3
+ IERC20,
4
+ IERC20__factory,
5
+ IERC721,
6
+ IERC721__factory,
7
+ IZNSHub,
8
+ IZNSHub__factory,
9
+ ZAuction,
10
+ ZAuctionV1,
11
+ ZAuctionV1__factory,
12
+ ZAuction__factory,
13
+ } from "./types";
14
+
15
+ export const getZAuctionContract = async (
16
+ web3Provider: ethers.providers.Provider | ethers.Signer,
17
+ address: string
18
+ ): Promise<ZAuction> => {
19
+ const contract = ZAuction__factory.connect(address, web3Provider);
20
+ return contract;
21
+ };
22
+
23
+ export const getZAuctionV1Contract = async (
24
+ web3Provider: ethers.providers.Provider | ethers.Signer,
25
+ address: string // to change on new deployment
26
+ ): Promise<ZAuctionV1> => {
27
+ const contract = ZAuctionV1__factory.connect(address, web3Provider);
28
+ return contract;
29
+ };
30
+
31
+ export const getERC20Contract = async (
32
+ web3Provider: ethers.providers.Provider | ethers.Signer,
33
+ address: string
34
+ ): Promise<IERC20> => {
35
+ const contract = IERC20__factory.connect(address, web3Provider);
36
+ return contract;
37
+ };
38
+
39
+ export const getERC721Contract = async (
40
+ web3Provider: ethers.providers.Provider | ethers.Signer,
41
+ address: string
42
+ ): Promise<IERC721> => {
43
+ const contract = IERC721__factory.connect(address, web3Provider);
44
+ return contract;
45
+ };
46
+
47
+ export const getZnsHubContract = async (
48
+ web3Provider: ethers.providers.Provider | ethers.Signer,
49
+ address: string
50
+ ): Promise<IZNSHub> => {
51
+ const contract = IZNSHub__factory.connect(address, web3Provider);
52
+ return contract;
53
+ };