@bosonprotocol/core-sdk 1.12.0-alpha.0 → 1.12.0-alpha.10

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 (147) hide show
  1. package/dist/cjs/accounts/handler.d.ts +43 -1
  2. package/dist/cjs/accounts/handler.d.ts.map +1 -1
  3. package/dist/cjs/accounts/handler.js +79 -1
  4. package/dist/cjs/accounts/handler.js.map +1 -1
  5. package/dist/cjs/accounts/index.d.ts +2 -0
  6. package/dist/cjs/accounts/index.d.ts.map +1 -1
  7. package/dist/cjs/accounts/interface.d.ts +26 -3
  8. package/dist/cjs/accounts/interface.d.ts.map +1 -1
  9. package/dist/cjs/accounts/interface.js +72 -4
  10. package/dist/cjs/accounts/interface.js.map +1 -1
  11. package/dist/cjs/accounts/subgraph.d.ts +4 -1
  12. package/dist/cjs/accounts/subgraph.d.ts.map +1 -1
  13. package/dist/cjs/accounts/subgraph.js +17 -1
  14. package/dist/cjs/accounts/subgraph.js.map +1 -1
  15. package/dist/cjs/accounts/types.d.ts +28 -1
  16. package/dist/cjs/accounts/types.d.ts.map +1 -1
  17. package/dist/cjs/core-sdk.d.ts +130 -7
  18. package/dist/cjs/core-sdk.d.ts.map +1 -1
  19. package/dist/cjs/core-sdk.js +335 -13
  20. package/dist/cjs/core-sdk.js.map +1 -1
  21. package/dist/cjs/disputes/handler.d.ts +61 -0
  22. package/dist/cjs/disputes/handler.d.ts.map +1 -0
  23. package/dist/cjs/disputes/handler.js +104 -0
  24. package/dist/cjs/disputes/handler.js.map +1 -0
  25. package/dist/cjs/disputes/index.d.ts +4 -0
  26. package/dist/cjs/disputes/index.d.ts.map +1 -0
  27. package/dist/cjs/disputes/index.js +30 -0
  28. package/dist/cjs/disputes/index.js.map +1 -0
  29. package/dist/cjs/disputes/interface.d.ts +30 -0
  30. package/dist/cjs/disputes/interface.d.ts.map +1 -0
  31. package/dist/cjs/disputes/interface.js +74 -0
  32. package/dist/cjs/disputes/interface.js.map +1 -0
  33. package/dist/cjs/disputes/subgraph.d.ts +6 -0
  34. package/dist/cjs/disputes/subgraph.d.ts.map +1 -0
  35. package/dist/cjs/disputes/subgraph.js +30 -0
  36. package/dist/cjs/disputes/subgraph.js.map +1 -0
  37. package/dist/cjs/offers/index.d.ts +1 -2
  38. package/dist/cjs/offers/index.d.ts.map +1 -1
  39. package/dist/cjs/offers/index.js +1 -0
  40. package/dist/cjs/offers/index.js.map +1 -1
  41. package/dist/cjs/offers/interface.d.ts +1 -0
  42. package/dist/cjs/offers/interface.d.ts.map +1 -1
  43. package/dist/cjs/offers/interface.js +2 -1
  44. package/dist/cjs/offers/interface.js.map +1 -1
  45. package/dist/cjs/offers/renderContractualAgreement.d.ts +41 -0
  46. package/dist/cjs/offers/renderContractualAgreement.d.ts.map +1 -0
  47. package/dist/cjs/offers/renderContractualAgreement.js +156 -0
  48. package/dist/cjs/offers/renderContractualAgreement.js.map +1 -0
  49. package/dist/cjs/offers/subgraph.d.ts.map +1 -1
  50. package/dist/cjs/offers/subgraph.js.map +1 -1
  51. package/dist/cjs/orchestration/interface.d.ts.map +1 -1
  52. package/dist/cjs/orchestration/interface.js +9 -2
  53. package/dist/cjs/orchestration/interface.js.map +1 -1
  54. package/dist/cjs/subgraph.d.ts +5892 -322
  55. package/dist/cjs/subgraph.d.ts.map +1 -1
  56. package/dist/cjs/subgraph.js +431 -169
  57. package/dist/cjs/subgraph.js.map +1 -1
  58. package/dist/cjs/utils/graphql.d.ts +38 -0
  59. package/dist/cjs/utils/graphql.d.ts.map +1 -1
  60. package/dist/cjs/utils/tokenInfoManager.d.ts +16 -0
  61. package/dist/cjs/utils/tokenInfoManager.d.ts.map +1 -0
  62. package/dist/cjs/utils/tokenInfoManager.js +97 -0
  63. package/dist/cjs/utils/tokenInfoManager.js.map +1 -0
  64. package/dist/esm/accounts/handler.d.ts +43 -1
  65. package/dist/esm/accounts/handler.d.ts.map +1 -1
  66. package/dist/esm/accounts/handler.js +62 -1
  67. package/dist/esm/accounts/handler.js.map +1 -1
  68. package/dist/esm/accounts/index.d.ts +2 -0
  69. package/dist/esm/accounts/index.d.ts.map +1 -1
  70. package/dist/esm/accounts/interface.d.ts +26 -3
  71. package/dist/esm/accounts/interface.d.ts.map +1 -1
  72. package/dist/esm/accounts/interface.js +57 -4
  73. package/dist/esm/accounts/interface.js.map +1 -1
  74. package/dist/esm/accounts/subgraph.d.ts +4 -1
  75. package/dist/esm/accounts/subgraph.d.ts.map +1 -1
  76. package/dist/esm/accounts/subgraph.js +13 -0
  77. package/dist/esm/accounts/subgraph.js.map +1 -1
  78. package/dist/esm/accounts/types.d.ts +28 -1
  79. package/dist/esm/accounts/types.d.ts.map +1 -1
  80. package/dist/esm/core-sdk.d.ts +130 -7
  81. package/dist/esm/core-sdk.d.ts.map +1 -1
  82. package/dist/esm/core-sdk.js +289 -13
  83. package/dist/esm/core-sdk.js.map +1 -1
  84. package/dist/esm/disputes/handler.d.ts +61 -0
  85. package/dist/esm/disputes/handler.d.ts.map +1 -0
  86. package/dist/esm/disputes/handler.js +62 -0
  87. package/dist/esm/disputes/handler.js.map +1 -0
  88. package/dist/esm/disputes/index.d.ts +4 -0
  89. package/dist/esm/disputes/index.d.ts.map +1 -0
  90. package/dist/esm/disputes/index.js +4 -0
  91. package/dist/esm/disputes/index.js.map +1 -0
  92. package/dist/esm/disputes/interface.d.ts +30 -0
  93. package/dist/esm/disputes/interface.d.ts.map +1 -0
  94. package/dist/esm/disputes/interface.js +61 -0
  95. package/dist/esm/disputes/interface.js.map +1 -0
  96. package/dist/esm/disputes/subgraph.d.ts +6 -0
  97. package/dist/esm/disputes/subgraph.d.ts.map +1 -0
  98. package/dist/esm/disputes/subgraph.js +15 -0
  99. package/dist/esm/disputes/subgraph.js.map +1 -0
  100. package/dist/esm/offers/index.d.ts +1 -2
  101. package/dist/esm/offers/index.d.ts.map +1 -1
  102. package/dist/esm/offers/index.js +1 -0
  103. package/dist/esm/offers/index.js.map +1 -1
  104. package/dist/esm/offers/interface.d.ts +1 -0
  105. package/dist/esm/offers/interface.d.ts.map +1 -1
  106. package/dist/esm/offers/interface.js +2 -1
  107. package/dist/esm/offers/interface.js.map +1 -1
  108. package/dist/esm/offers/renderContractualAgreement.d.ts +41 -0
  109. package/dist/esm/offers/renderContractualAgreement.d.ts.map +1 -0
  110. package/dist/esm/offers/renderContractualAgreement.js +132 -0
  111. package/dist/esm/offers/renderContractualAgreement.js.map +1 -0
  112. package/dist/esm/offers/subgraph.d.ts.map +1 -1
  113. package/dist/esm/offers/subgraph.js.map +1 -1
  114. package/dist/esm/orchestration/interface.d.ts.map +1 -1
  115. package/dist/esm/orchestration/interface.js +9 -2
  116. package/dist/esm/orchestration/interface.js.map +1 -1
  117. package/dist/esm/subgraph.d.ts +5892 -322
  118. package/dist/esm/subgraph.d.ts.map +1 -1
  119. package/dist/esm/subgraph.js +429 -167
  120. package/dist/esm/subgraph.js.map +1 -1
  121. package/dist/esm/utils/graphql.d.ts +38 -0
  122. package/dist/esm/utils/graphql.d.ts.map +1 -1
  123. package/dist/esm/utils/tokenInfoManager.d.ts +16 -0
  124. package/dist/esm/utils/tokenInfoManager.d.ts.map +1 -0
  125. package/dist/esm/utils/tokenInfoManager.js +83 -0
  126. package/dist/esm/utils/tokenInfoManager.js.map +1 -0
  127. package/package.json +6 -4
  128. package/src/accounts/handler.ts +137 -4
  129. package/src/accounts/interface.ts +111 -8
  130. package/src/accounts/queries.graphql +87 -0
  131. package/src/accounts/subgraph.ts +34 -2
  132. package/src/accounts/types.ts +34 -1
  133. package/src/core-sdk.ts +420 -18
  134. package/src/disputes/handler.ts +132 -0
  135. package/src/disputes/index.ts +3 -0
  136. package/src/disputes/interface.ts +90 -0
  137. package/src/disputes/queries.graphql +56 -0
  138. package/src/disputes/subgraph.ts +34 -0
  139. package/src/metadata/product-v1.graphql +3 -0
  140. package/src/offers/index.ts +1 -0
  141. package/src/offers/interface.ts +3 -1
  142. package/src/offers/queries.graphql +37 -0
  143. package/src/offers/renderContractualAgreement.ts +203 -0
  144. package/src/offers/subgraph.ts +0 -1
  145. package/src/orchestration/interface.ts +9 -2
  146. package/src/subgraph.ts +6097 -293
  147. package/src/utils/tokenInfoManager.ts +97 -0
@@ -0,0 +1,132 @@
1
+ import { Web3LibAdapter, TransactionResponse } from "@bosonprotocol/common";
2
+ import { BigNumberish } from "@ethersproject/bignumber";
3
+ import { BytesLike } from "@ethersproject/bytes";
4
+ import {
5
+ encodeRaiseDispute,
6
+ encodeDecideDispute,
7
+ encodeEscalateDispute,
8
+ encodeExpireDispute,
9
+ encodeExpireDisputeBatch,
10
+ encodeExpireEscalatedDispute,
11
+ encodeExtendDisputeTimeout,
12
+ encodeRefuseEscalatedDispute,
13
+ encodeResolveDispute,
14
+ encodeRetractDispute
15
+ } from "./interface";
16
+
17
+ export async function raiseDispute(args: {
18
+ exchangeId: BigNumberish;
19
+ complaint: string;
20
+ contractAddress: string;
21
+ web3Lib: Web3LibAdapter;
22
+ }): Promise<TransactionResponse> {
23
+ return args.web3Lib.sendTransaction({
24
+ to: args.contractAddress,
25
+ data: encodeRaiseDispute(args)
26
+ });
27
+ }
28
+
29
+ export async function retractDispute(args: {
30
+ exchangeId: BigNumberish;
31
+ contractAddress: string;
32
+ web3Lib: Web3LibAdapter;
33
+ }): Promise<TransactionResponse> {
34
+ return args.web3Lib.sendTransaction({
35
+ to: args.contractAddress,
36
+ data: encodeRetractDispute(args.exchangeId)
37
+ });
38
+ }
39
+
40
+ export async function extendDisputeTimeout(args: {
41
+ exchangeId: BigNumberish;
42
+ newDisputeTimeout: BigNumberish;
43
+ contractAddress: string;
44
+ web3Lib: Web3LibAdapter;
45
+ }): Promise<TransactionResponse> {
46
+ return args.web3Lib.sendTransaction({
47
+ to: args.contractAddress,
48
+ data: encodeExtendDisputeTimeout(args)
49
+ });
50
+ }
51
+
52
+ export async function expireDispute(args: {
53
+ exchangeId: BigNumberish;
54
+ contractAddress: string;
55
+ web3Lib: Web3LibAdapter;
56
+ }): Promise<TransactionResponse> {
57
+ return args.web3Lib.sendTransaction({
58
+ to: args.contractAddress,
59
+ data: encodeExpireDispute(args.exchangeId)
60
+ });
61
+ }
62
+
63
+ export async function expireDisputeBatch(args: {
64
+ exchangeIds: BigNumberish[];
65
+ contractAddress: string;
66
+ web3Lib: Web3LibAdapter;
67
+ }): Promise<TransactionResponse> {
68
+ return args.web3Lib.sendTransaction({
69
+ to: args.contractAddress,
70
+ data: encodeExpireDisputeBatch(args.exchangeIds)
71
+ });
72
+ }
73
+
74
+ export async function resolveDispute(args: {
75
+ exchangeId: BigNumberish;
76
+ buyerPercent: BigNumberish;
77
+ sigR: BytesLike;
78
+ sigS: BytesLike;
79
+ sigV: BigNumberish;
80
+ contractAddress: string;
81
+ web3Lib: Web3LibAdapter;
82
+ }): Promise<TransactionResponse> {
83
+ return args.web3Lib.sendTransaction({
84
+ to: args.contractAddress,
85
+ data: encodeResolveDispute(args)
86
+ });
87
+ }
88
+
89
+ export async function escalateDispute(args: {
90
+ exchangeId: BigNumberish;
91
+ contractAddress: string;
92
+ web3Lib: Web3LibAdapter;
93
+ }): Promise<TransactionResponse> {
94
+ return args.web3Lib.sendTransaction({
95
+ to: args.contractAddress,
96
+ data: encodeEscalateDispute(args.exchangeId)
97
+ });
98
+ }
99
+
100
+ export async function decideDispute(args: {
101
+ exchangeId: BigNumberish;
102
+ buyerPercent: BigNumberish;
103
+ contractAddress: string;
104
+ web3Lib: Web3LibAdapter;
105
+ }): Promise<TransactionResponse> {
106
+ return args.web3Lib.sendTransaction({
107
+ to: args.contractAddress,
108
+ data: encodeDecideDispute(args)
109
+ });
110
+ }
111
+
112
+ export async function refuseEscalatedDispute(args: {
113
+ exchangeId: BigNumberish;
114
+ contractAddress: string;
115
+ web3Lib: Web3LibAdapter;
116
+ }): Promise<TransactionResponse> {
117
+ return args.web3Lib.sendTransaction({
118
+ to: args.contractAddress,
119
+ data: encodeRefuseEscalatedDispute(args.exchangeId)
120
+ });
121
+ }
122
+
123
+ export async function expireEscalatedDispute(args: {
124
+ exchangeId: BigNumberish;
125
+ contractAddress: string;
126
+ web3Lib: Web3LibAdapter;
127
+ }): Promise<TransactionResponse> {
128
+ return args.web3Lib.sendTransaction({
129
+ to: args.contractAddress,
130
+ data: encodeExpireEscalatedDispute(args.exchangeId)
131
+ });
132
+ }
@@ -0,0 +1,3 @@
1
+ export * as iface from "./interface";
2
+ export * as handler from "./handler";
3
+ export * as subgraph from "./subgraph";
@@ -0,0 +1,90 @@
1
+ import { abis } from "@bosonprotocol/common";
2
+ import { Interface } from "@ethersproject/abi";
3
+ import { BigNumberish } from "@ethersproject/bignumber";
4
+ import { BytesLike } from "@ethersproject/bytes";
5
+
6
+ export const bosonDisputeHandlerIface = new Interface(
7
+ abis.IBosonDisputeHandlerABI
8
+ );
9
+
10
+ export function encodeDecideDispute(args: {
11
+ exchangeId: BigNumberish;
12
+ buyerPercent: BigNumberish;
13
+ }) {
14
+ return bosonDisputeHandlerIface.encodeFunctionData("decideDispute", [
15
+ args.exchangeId,
16
+ args.buyerPercent
17
+ ]);
18
+ }
19
+
20
+ export function encodeEscalateDispute(exchangeId: BigNumberish) {
21
+ return bosonDisputeHandlerIface.encodeFunctionData("escalateDispute", [
22
+ exchangeId
23
+ ]);
24
+ }
25
+
26
+ export function encodeExpireDispute(exchangeId: BigNumberish) {
27
+ return bosonDisputeHandlerIface.encodeFunctionData("expireDispute", [
28
+ exchangeId
29
+ ]);
30
+ }
31
+
32
+ export function encodeExpireDisputeBatch(exchangeIds: BigNumberish[]) {
33
+ return bosonDisputeHandlerIface.encodeFunctionData("expireDispute", [
34
+ exchangeIds
35
+ ]);
36
+ }
37
+
38
+ export function encodeExpireEscalatedDispute(exchangeId: BigNumberish) {
39
+ return bosonDisputeHandlerIface.encodeFunctionData("expireEscalatedDispute", [
40
+ exchangeId
41
+ ]);
42
+ }
43
+
44
+ export function encodeExtendDisputeTimeout(args: {
45
+ exchangeId: BigNumberish;
46
+ newDisputeTimeout: BigNumberish;
47
+ }) {
48
+ return bosonDisputeHandlerIface.encodeFunctionData("extendDisputeTimeout", [
49
+ args.exchangeId,
50
+ args.newDisputeTimeout
51
+ ]);
52
+ }
53
+
54
+ export function encodeRaiseDispute(args: {
55
+ exchangeId: BigNumberish;
56
+ complaint: string;
57
+ }) {
58
+ return bosonDisputeHandlerIface.encodeFunctionData("raiseDispute", [
59
+ args.exchangeId,
60
+ args.complaint
61
+ ]);
62
+ }
63
+
64
+ export function encodeRefuseEscalatedDispute(exchangeId: BigNumberish) {
65
+ return bosonDisputeHandlerIface.encodeFunctionData("refuseEscalatedDispute", [
66
+ exchangeId
67
+ ]);
68
+ }
69
+
70
+ export function encodeResolveDispute(args: {
71
+ exchangeId: BigNumberish;
72
+ buyerPercent: BigNumberish;
73
+ sigR: BytesLike;
74
+ sigS: BytesLike;
75
+ sigV: BigNumberish;
76
+ }) {
77
+ return bosonDisputeHandlerIface.encodeFunctionData("resolveDispute", [
78
+ args.exchangeId,
79
+ args.buyerPercent,
80
+ args.sigR,
81
+ args.sigS,
82
+ args.sigV
83
+ ]);
84
+ }
85
+
86
+ export function encodeRetractDispute(exchangeId: BigNumberish) {
87
+ return bosonDisputeHandlerIface.encodeFunctionData("retractDispute", [
88
+ exchangeId
89
+ ]);
90
+ }
@@ -0,0 +1,56 @@
1
+ query getDisputeByIdQuery(
2
+ $disputeId: ID!
3
+ $offersSkip: Int
4
+ $offersFirst: Int
5
+ $offersOrderBy: Offer_orderBy
6
+ $offersOrderDirection: OrderDirection
7
+ $offersFilter: Offer_filter
8
+ $includeOffers: Boolean = false
9
+ ) {
10
+ dispute(id: $disputeId) {
11
+ ...DisputeFields
12
+ }
13
+ }
14
+
15
+ query getDisputesQuery(
16
+ $disputesSkip: Int
17
+ $disputesFirst: Int
18
+ $disputesOrderBy: Dispute_orderBy
19
+ $disputesOrderDirection: OrderDirection
20
+ $disputesFilter: Dispute_filter
21
+ ) {
22
+ disputes(
23
+ skip: $disputesSkip
24
+ first: $disputesFirst
25
+ orderBy: $disputesOrderBy
26
+ orderDirection: $disputesOrderDirection
27
+ where: $disputesFilter
28
+ ) {
29
+ ...DisputeFields
30
+ }
31
+ }
32
+
33
+ fragment DisputeFields on Dispute {
34
+ ...BaseDisputeFields
35
+ }
36
+
37
+ fragment BaseDisputeFields on Dispute {
38
+ id
39
+ exchangeId
40
+ complaint
41
+ state
42
+ buyerPercent
43
+ disputedDate
44
+ escalatedDate
45
+ finalizedDate
46
+ timeout
47
+ exchange {
48
+ ...BaseExchangeFields
49
+ }
50
+ seller {
51
+ ...BaseSellerFields
52
+ }
53
+ buyer {
54
+ ...BaseBuyerFields
55
+ }
56
+ }
@@ -0,0 +1,34 @@
1
+ import { getSubgraphSdk } from "../utils/graphql";
2
+ import {
3
+ DisputeFieldsFragment,
4
+ GetDisputesQueryQueryVariables,
5
+ GetDisputeByIdQueryQueryVariables
6
+ } from "../subgraph";
7
+ import { BigNumberish } from "@ethersproject/bignumber";
8
+
9
+ export type SingleDisputeQueryVariables = Omit<
10
+ GetDisputeByIdQueryQueryVariables,
11
+ "disputeId"
12
+ >;
13
+
14
+ export async function getDisputeById(
15
+ subgraphUrl: string,
16
+ disputeId: BigNumberish,
17
+ queryVars: SingleDisputeQueryVariables = {}
18
+ ): Promise<DisputeFieldsFragment> {
19
+ const sdk = getSubgraphSdk(subgraphUrl);
20
+ const { dispute } = await sdk.getDisputeByIdQuery({
21
+ disputeId: disputeId.toString(),
22
+ ...queryVars
23
+ });
24
+ return dispute;
25
+ }
26
+
27
+ export async function getDisputes(
28
+ subgraphUrl: string,
29
+ queryVars: GetDisputesQueryQueryVariables = {}
30
+ ): Promise<DisputeFieldsFragment[]> {
31
+ const sdk = getSubgraphSdk(subgraphUrl);
32
+ const { disputes = [] } = await sdk.getDisputesQuery(queryVars);
33
+ return disputes;
34
+ }
@@ -107,6 +107,9 @@ fragment BaseProductV1MetadataEntityFields on ProductV1MetadataEntity {
107
107
  productV1Seller {
108
108
  ...BaseProductV1SellerFields
109
109
  }
110
+ exchangePolicy {
111
+ ...BaseProductV1ExchangePolicyFields
112
+ }
110
113
  }
111
114
 
112
115
  fragment BaseProductV1ProductFields on ProductV1Product {
@@ -6,6 +6,7 @@ export * as iface from "./interface";
6
6
  export * as storage from "./storage";
7
7
  export * from "./types";
8
8
  export * from "./getOfferStatus";
9
+ export * from "./renderContractualAgreement";
9
10
 
10
11
  export const validation = {
11
12
  createOfferArgsSchema: utils.validation.createOfferArgsSchema
@@ -25,13 +25,15 @@ export function createOfferArgsToStructs(
25
25
  Partial<OfferStruct>,
26
26
  Partial<OfferDatesStruct>,
27
27
  Partial<OfferDurationsStruct>,
28
+ BigNumberish,
28
29
  BigNumberish
29
30
  ] {
30
31
  return [
31
32
  argsToOfferStruct(args),
32
33
  argsToOfferDatesStruct(args),
33
34
  argsToOfferDurationsStruct(args),
34
- args.disputeResolverId
35
+ args.disputeResolverId,
36
+ args.agentId
35
37
  ];
36
38
  }
37
39
 
@@ -55,6 +55,8 @@ fragment BaseOfferFields on Offer {
55
55
  price
56
56
  sellerDeposit
57
57
  protocolFee
58
+ agentFee
59
+ agentId
58
60
  buyerCancelPenalty
59
61
  quantityAvailable
60
62
  quantityInitial
@@ -75,11 +77,46 @@ fragment BaseOfferFields on Offer {
75
77
  exchangeToken {
76
78
  ...BaseExchangeTokenFields
77
79
  }
80
+ disputeResolver {
81
+ ...BaseDisputeResolverFields
82
+ }
83
+ disputeResolutionTerms {
84
+ ...BaseDisputeResolutionTermsEntityFields
85
+ }
78
86
  metadata {
79
87
  name
80
88
  description
81
89
  externalUrl
82
90
  schemaUrl
83
91
  type
92
+ ... on ProductV1MetadataEntity {
93
+ image
94
+ attributes {
95
+ traitType
96
+ value
97
+ displayType
98
+ }
99
+ createdAt
100
+ voided
101
+ validFromDate
102
+ validUntilDate
103
+ quantityAvailable
104
+ uuid
105
+ product {
106
+ ...BaseProductV1ProductFields
107
+ }
108
+ variations {
109
+ ...BaseProductV1VariationFields
110
+ }
111
+ productV1Seller {
112
+ ...BaseProductV1SellerFields
113
+ }
114
+ exchangePolicy {
115
+ ...BaseProductV1ExchangePolicyFields
116
+ }
117
+ shipping {
118
+ ...BaseProductV1ShippingOptionFields
119
+ }
120
+ }
84
121
  }
85
122
  }
@@ -0,0 +1,203 @@
1
+ import * as yup from "yup";
2
+ import { ITokenInfo } from "./../utils/tokenInfoManager";
3
+ import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
4
+ import { offers, subgraph } from "..";
5
+ import { utils } from "@bosonprotocol/common";
6
+ import Mustache from "mustache";
7
+ import { formatUnits } from "@ethersproject/units";
8
+ import { productV1 } from "@bosonprotocol/metadata";
9
+
10
+ export type TemplateRenderingData = offers.CreateOfferArgs & {
11
+ priceValue: string; // Convert in decimals value
12
+ sellerDepositValue: string; // Convert in decimals value
13
+ buyerCancelPenaltyValue: string; // Convert in decimals value
14
+ agentFeeValue: string; // Convert in decimals value
15
+ exchangeTokenSymbol: string;
16
+ sellerContactMethod: string;
17
+ disputeResolverContactMethod: string;
18
+ toISOString: () => void;
19
+ msecToDay: () => void;
20
+ };
21
+
22
+ export class InvalidOfferDataError extends Error {
23
+ constructor(public missingProperties: string[]) {
24
+ super(
25
+ `InvalidOfferData - missing properties: [${missingProperties.join(",")}]`
26
+ );
27
+ }
28
+ }
29
+
30
+ export const baseOfferDataSchema: yup.SchemaOf<BaseOfferData> = yup.object({
31
+ price: yup.mixed().required(),
32
+ sellerDeposit: yup.mixed().required(),
33
+ agentId: yup.mixed().required(),
34
+ buyerCancelPenalty: yup.mixed().required(),
35
+ quantityAvailable: yup.mixed().required(),
36
+ validFromDateInMS: yup.mixed().required(),
37
+ validUntilDateInMS: yup.mixed().required(),
38
+ voucherRedeemableFromDateInMS: yup.mixed().required(),
39
+ voucherRedeemableUntilDateInMS: yup.mixed().required(),
40
+ fulfillmentPeriodDurationInMS: yup.mixed().required(),
41
+ resolutionPeriodDurationInMS: yup.mixed().required(),
42
+ exchangeToken: yup.string().required(),
43
+ disputeResolverId: yup.mixed().required(),
44
+ metadataUri: yup.string().required(),
45
+ metadataHash: yup.string().required()
46
+ });
47
+
48
+ export type BaseOfferData = {
49
+ price: BigNumberish;
50
+ sellerDeposit: BigNumberish;
51
+ agentId: BigNumberish;
52
+ buyerCancelPenalty: BigNumberish;
53
+ quantityAvailable: BigNumberish;
54
+ validFromDateInMS: BigNumberish;
55
+ validUntilDateInMS: BigNumberish;
56
+ voucherRedeemableFromDateInMS: BigNumberish;
57
+ voucherRedeemableUntilDateInMS: BigNumberish;
58
+ fulfillmentPeriodDurationInMS: BigNumberish;
59
+ resolutionPeriodDurationInMS: BigNumberish;
60
+ exchangeToken: string;
61
+ disputeResolverId: BigNumberish;
62
+ metadataUri: string;
63
+ metadataHash: string;
64
+ };
65
+
66
+ function checkOfferDataIsValid(
67
+ offerData: unknown,
68
+ throwIfInvalid = false
69
+ ): boolean {
70
+ if (offerData === undefined || offerData === null) {
71
+ throw new Error("InvalidOfferData - undefined");
72
+ }
73
+ if (typeof offerData !== "object") {
74
+ throw new Error("InvalidOfferData - expecting an object");
75
+ }
76
+ try {
77
+ baseOfferDataSchema.validateSync(offerData, { abortEarly: false });
78
+ } catch (e) {
79
+ const missingProperties = [];
80
+ const getMissingProp = (error) => {
81
+ return error.match(/(.*) is a required field/)[1] || error;
82
+ };
83
+ if (throwIfInvalid) {
84
+ if (e.errors) {
85
+ e.errors.forEach((error: string) => {
86
+ missingProperties.push(getMissingProp(error));
87
+ });
88
+ } else {
89
+ missingProperties.push(getMissingProp(e));
90
+ }
91
+ throw new InvalidOfferDataError(missingProperties);
92
+ }
93
+ return false;
94
+ }
95
+ return true;
96
+ }
97
+
98
+ function convertExistingOfferData(
99
+ offerDataSubGraph: subgraph.OfferFieldsFragment
100
+ ): { offerData: offers.CreateOfferArgs; tokenInfo: ITokenInfo } {
101
+ return {
102
+ offerData: {
103
+ ...offerDataSubGraph,
104
+ validFromDateInMS: offerDataSubGraph.validFromDate,
105
+ validUntilDateInMS: offerDataSubGraph.validUntilDate,
106
+ voucherRedeemableFromDateInMS:
107
+ offerDataSubGraph.voucherRedeemableFromDate,
108
+ voucherRedeemableUntilDateInMS:
109
+ offerDataSubGraph.voucherRedeemableUntilDate,
110
+ fulfillmentPeriodDurationInMS:
111
+ offerDataSubGraph.fulfillmentPeriodDuration,
112
+ resolutionPeriodDurationInMS: offerDataSubGraph.resolutionPeriodDuration,
113
+ exchangeToken: offerDataSubGraph.exchangeToken.address
114
+ },
115
+ tokenInfo: {
116
+ ...offerDataSubGraph.exchangeToken,
117
+ decimals: parseInt(offerDataSubGraph.exchangeToken.decimals)
118
+ }
119
+ };
120
+ }
121
+
122
+ export async function prepareRenderingData(
123
+ offerData: offers.CreateOfferArgs,
124
+ tokenInfo: ITokenInfo
125
+ ): Promise<TemplateRenderingData> {
126
+ return {
127
+ ...offerData,
128
+ priceValue: formatUnits(offerData.price, tokenInfo.decimals),
129
+ exchangeTokenSymbol: tokenInfo.symbol,
130
+ sellerDepositValue: formatUnits(
131
+ offerData.sellerDeposit,
132
+ tokenInfo.decimals
133
+ ),
134
+ agentFeeValue: "0", // TODO: get the agentFee of the specified offerData.agentId
135
+ buyerCancelPenaltyValue: formatUnits(
136
+ offerData.buyerCancelPenalty,
137
+ tokenInfo.decimals
138
+ ),
139
+ sellerContactMethod: "TBD", // TODO: what is the sellerContactMethod?
140
+ disputeResolverContactMethod: "TBD", // TODO: what is the disputeResolverContactMethod?
141
+ toISOString: function () {
142
+ return function (num, render) {
143
+ return new Date(parseInt(render(num))).toISOString();
144
+ };
145
+ },
146
+ msecToDay: function () {
147
+ return function (num, render) {
148
+ return BigNumber.from(render(num))
149
+ .div(utils.timestamp.MSEC_PER_DAY)
150
+ .toString();
151
+ };
152
+ }
153
+ };
154
+ }
155
+
156
+ // inject a template + all offer details
157
+ export async function renderContractualAgreement(
158
+ template: string,
159
+ offerData: offers.CreateOfferArgs,
160
+ tokenInfo: ITokenInfo
161
+ ): Promise<string> {
162
+ // Check the passed offerData is matching the required type
163
+ checkOfferDataIsValid(offerData, true);
164
+ const preparedData = await prepareRenderingData(offerData, tokenInfo);
165
+ return Mustache.render(template, preparedData);
166
+ }
167
+
168
+ export async function renderContractualAgreementForOffer(
169
+ existingOfferData: subgraph.OfferFieldsFragment
170
+ ): Promise<string> {
171
+ if (!existingOfferData) {
172
+ throw new Error(`offerData is undefined`);
173
+ }
174
+ if (!existingOfferData.metadata) {
175
+ throw new Error(`Offer Metadata is undefined`);
176
+ }
177
+ if (existingOfferData.metadata.type !== subgraph.MetadataType.ProductV1) {
178
+ throw new Error(
179
+ `Invalid Offer Metadata: Type is not supported: '${existingOfferData.metadata.type}'`
180
+ );
181
+ }
182
+ if (
183
+ !(existingOfferData.metadata as productV1.ProductV1Metadata).exchangePolicy
184
+ ) {
185
+ throw new Error(`Invalid Offer Metadata: exchangePolicy is not defined`);
186
+ }
187
+ if (
188
+ !(existingOfferData.metadata as productV1.ProductV1Metadata).exchangePolicy
189
+ .template
190
+ ) {
191
+ throw new Error(
192
+ `Invalid Offer Metadata: exchangePolicy.template is not defined`
193
+ );
194
+ }
195
+ const template = (existingOfferData.metadata as productV1.ProductV1Metadata)
196
+ .exchangePolicy.template;
197
+ const convertedOfferArgs = convertExistingOfferData(existingOfferData);
198
+ return renderContractualAgreement(
199
+ template,
200
+ convertedOfferArgs.offerData,
201
+ convertedOfferArgs.tokenInfo
202
+ );
203
+ }
@@ -21,7 +21,6 @@ export async function getOfferById(
21
21
  offerId: offerId.toString(),
22
22
  ...queryVars
23
23
  });
24
-
25
24
  return offer;
26
25
  }
27
26
 
@@ -14,12 +14,19 @@ export function encodeCreateSellerAndOffer(
14
14
  seller: CreateSellerArgs,
15
15
  offer: CreateOfferArgs
16
16
  ) {
17
+ const sellerArgs = createSellerArgsToStruct(seller);
18
+ const offerArgs = createOfferArgsToStructs(offer);
17
19
  return bosonOrchestrationHandlerIface.encodeFunctionData(
18
20
  "createSellerAndOffer",
19
21
  [
20
- createSellerArgsToStruct(seller),
22
+ sellerArgs.sellerStruct,
21
23
  seller.contractUri,
22
- ...createOfferArgsToStructs(offer)
24
+ offerArgs[0], // offer
25
+ offerArgs[1], // offerDates
26
+ offerArgs[2], // offerDurations
27
+ offerArgs[3], // disputeResolverId
28
+ sellerArgs.authTokenStruct,
29
+ offerArgs[4] // agentId
23
30
  ]
24
31
  );
25
32
  }