-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.ts
128 lines (119 loc) · 3.68 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { ApolloClient, NormalizedCacheObject } from "@apollo/client";
import { Wallet, zeroAddress } from "@/blockchain";
import {
CollectionSignedOfferInput,
ListOffersQueryVariables,
SingleNftSignedOfferInput,
} from "@/generated/graphql";
import { apolloClient } from "@/graphql/client";
import { getSdkApollo } from "@/graphql/sdk";
import { RenegotiationOffer } from "./model";
export type Props = {
apiClient?: ApolloClient<NormalizedCacheObject>;
wallet: Wallet;
};
export class Api {
api: ReturnType<typeof getSdkApollo>;
generateSingleNftOfferHash;
generateCollectionOfferHash;
generateRenegotiationOfferHash;
listListings;
nftIdBySlugTokenId;
nftIdByContractAddressAndTokenId;
collectionIdBySlug;
collectionsIdByContractAddress;
listNft;
unlistNft;
hideOffer;
hideRenegotiationOffer;
unhideOffer;
unhideRenegotiationOffer;
constructor({ apiClient, wallet }: Props) {
const gqlClient = apiClient ?? apolloClient(wallet);
this.api = getSdkApollo(gqlClient);
this.generateSingleNftOfferHash = this.api.generateSingleNftOfferHash;
this.generateCollectionOfferHash = this.api.generateCollectionOfferHash;
this.generateRenegotiationOfferHash =
this.api.generateRenegotiationOfferHash;
this.listListings = this.api.listListings;
this.nftIdBySlugTokenId = this.api.nftIdBySlugTokenId;
this.nftIdByContractAddressAndTokenId =
this.api.nftIdByContractAddressAndTokenId;
this.collectionIdBySlug = this.api.collectionIdBySlug;
this.collectionsIdByContractAddress =
this.api.collectionsIdByContractAddress;
this.listNft = this.api.listNft;
this.unlistNft = this.api.unlistNft;
this.hideOffer = this.api.hideOffer;
this.hideRenegotiationOffer = this.api.hideRenegotiationOffer;
this.unhideOffer = this.api.unhideOffer;
this.unhideRenegotiationOffer = this.api.unhideRenegotiationOffer;
}
async saveSingleNftOffer(offerInput: SingleNftSignedOfferInput) {
const offer = {
...offerInput,
borrowerAddress: offerInput.borrowerAddress || zeroAddress,
};
const response = await this.api.saveSingleNftOffer({ offer });
const nftCollateralAddress =
response.offer.nft.collection?.contractData?.contractAddress ||
zeroAddress;
return {
id: response.offer.id,
nftCollateralAddress,
nftCollateralTokenId: response.offer.nft.tokenId,
...offerInput,
};
}
async saveCollectionOffer(offerInput: CollectionSignedOfferInput) {
const offer = {
...offerInput,
borrowerAddress: offerInput.borrowerAddress || zeroAddress,
};
const response = await this.api.saveCollectionOffer({ offer });
const nftCollateralAddress =
response.offer.collection?.contractData?.contractAddress || zeroAddress;
return {
id: response.offer.id,
nftCollateralAddress,
nftCollateralTokenId: 0n,
...offerInput,
};
}
async saveRefinanceOffer(offerInput: RenegotiationOffer) {
const response = await this.api.saveRenegotiationOffer({
offer: offerInput,
});
return {
id: response.offer.id,
...offerInput,
};
}
async listOffers(props: ListOffersQueryVariables) {
const {
result: { edges, pageInfo },
} = await this.api.listOffers({
...props,
});
const offers = edges.map((edge) => {
const {
__typename,
lenderAddress,
borrowerAddress,
signerAddress,
...node
} = edge.node;
return {
type: __typename,
lender: lenderAddress,
borrower: borrowerAddress,
signer: signerAddress,
...node,
};
});
return {
cursor: pageInfo.endCursor,
offers,
};
}
}