-
Notifications
You must be signed in to change notification settings - Fork 49
/
createSingleSaleOffer.js
154 lines (141 loc) · 3.76 KB
/
createSingleSaleOffer.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const { GraphQLClient, gql } = require('graphql-request');
const { signAuthorizationRequest } = require('@sorare/crypto');
const crypto = require('crypto');
const yargs = require('yargs');
import {
authorizationRequestFragment,
buildApprovals,
} from '../authorizations';
const {
sendAssetId,
settlementCurrencies,
receiveCurrency,
receiveAmount,
token,
privateKey,
jwtAud,
} = yargs
.command('createSingleSaleOffer', 'Create a single sale offer.')
.option('send-asset-id', {
description: 'The assetId to send.',
type: 'string',
required: true,
})
.option('settlement-currencies', {
description:
'The currencies in which the offer can settle, any combination of WEI and your fiat wallet currency',
type: 'array',
required: true,
})
.option('receive-currency', {
description: 'One of WEI, EUR, USD, GBP',
type: 'string',
required: true,
})
.option('receive-amount', {
description:
'The amount to receive in the currency smallest denomination, cents for FIAT and wei for ETH',
type: 'string',
required: true,
})
.option('token', {
description: 'The JWT or OAuth token.',
type: 'string',
required: true,
})
.option('private-key', {
description: 'Your Sorare private key',
type: 'string',
required: true,
})
.option('jwt-aud', {
description: 'The JWT audience (required if using a JWT token).',
type: 'string',
})
.help()
.alias('help', 'h').argv;
const PrepareOffer = gql`
mutation PrepareOffer($input: prepareOfferInput!) {
prepareOffer(input: $input) {
authorizations {
...AuthorizationRequestFragment
}
errors {
message
}
}
}
${authorizationRequestFragment}
`;
const CreateSingleSaleOffer = gql`
mutation CreateSingleSaleOffer($input: createSingleSaleOfferInput!) {
createSingleSaleOffer(input: $input) {
tokenOffer {
id
startDate
endDate
}
errors {
message
}
}
}
`;
async function main() {
const graphQLClient = new GraphQLClient(
'https://api.sorare.com/graphql',
{
headers: {
Authorization: `Bearer ${token}`,
'JWT-AUD': jwtAud,
// 'APIKEY': '<YourOptionalAPIKey>'
},
}
);
const prepareOfferInput = {
type: 'SINGLE_SALE_OFFER',
sendAssetIds: [sendAssetId],
receiveAssetIds: [],
settlementCurrencies,
receiveAmount: {
amount: receiveAmount,
currency: receiveCurrency,
},
clientMutationId: crypto.randomBytes(8).join(''),
};
const prepareOfferData = await graphQLClient.request(PrepareOffer, {
input: prepareOfferInput,
});
const prepareOffer = prepareOfferData['prepareOffer'];
if (prepareOffer['errors'].length > 0) {
prepareOffer['errors'].forEach(error => {
console.error(error['message']);
});
process.exit(2);
}
const authorizations = prepareOffer['authorizations'];
const approvals = buildApprovals(privateKey, authorizations);
const createSingleSaleOfferInput = {
approvals,
dealId: crypto.randomBytes(8).join(''),
assetId: sendAssetId,
settlementCurrencies,
receiveAmount: { amount: receiveAmount, currency: receiveCurrency },
clientMutationId: crypto.randomBytes(8).join(''),
};
const createSingleSaleOfferData = await graphQLClient.request(
CreateSingleSaleOffer,
{ input: createSingleSaleOfferInput }
);
console.log(createSingleSaleOfferData);
const createSingleSaleOffer =
createSingleSaleOfferData['createSingleSaleOffer'];
if (createSingleSaleOffer['errors'].length > 0) {
createSingleSaleOffer['errors'].forEach(error => {
console.error(error['message']);
});
process.exit(2);
}
console.log('Success!');
}
main().catch(error => console.error(error));