-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathacceptSingleSaleOffer.js
140 lines (125 loc) · 3.29 KB
/
acceptSingleSaleOffer.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
const { GraphQLClient, gql } = require('graphql-request');
const { signLimitOrder } = require('@sorare/crypto');
const crypto = require('crypto');
const yargs = require('yargs');
import {
authorizationRequestFragment,
buildApprovals,
} from '../authorizations';
const { offerId, token, jwtAud, privateKey } = yargs
.command('acceptSingleSaleOffer', 'Accept a single sale offer.')
.option('offer-id', {
description: 'The Offer ID of the offer to accept.',
type: 'string',
required: true,
})
.option('token', {
description: 'The JWT or OAuth token.',
type: 'string',
required: true,
})
.option('private-key', {
description: 'Your Starkware 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 Config = gql`
query ConfigQuery {
config {
exchangeRate {
id
}
}
}
`;
const PrepareAcceptOffer = gql`
mutation PrepareAcceptOffer($input: prepareAcceptOfferInput!) {
prepareAcceptOffer(input: $input) {
authorizations {
...AuthorizationRequestFragment
}
errors {
message
}
}
}
${authorizationRequestFragment}
`;
const AcceptSingleSaleOffer = gql`
mutation AcceptSingleSaleOffer($input: acceptOfferInput!) {
acceptOffer(input: $input) {
tokenOffer {
id
}
errors {
message
}
}
}
`;
async function main() {
const graphQLClient = new GraphQLClient(
'https://api.sorare.com/graphql',
{
headers: {
Authorization: `Bearer ${token}`,
'JWT-AUD': jwtAud,
// 'APIKEY': '<YourOptionalAPIKey>'
},
}
);
const configData = await graphQLClient.request(Config);
const exchangeRateId = configData['config']['exchangeRate']['id'];
console.log('Using exchange rate id', exchangeRateId);
const prepareAcceptOfferInput = {
offerId: `SingleSaleOffer:${offerId}`,
settlementInfo: {
currency: 'WEI',
paymentMethod: 'WALLET',
exchangeRateId: exchangeRateId,
},
};
const prepareAcceptOfferData = await graphQLClient.request(
PrepareAcceptOffer,
{
input: prepareAcceptOfferInput,
}
);
const prepareAcceptOffer = prepareAcceptOfferData['prepareAcceptOffer'];
if (prepareAcceptOffer['errors'].length > 0) {
prepareAcceptOffer['errors'].forEach(error => {
console.error(error['message']);
});
process.exit(2);
}
const authorizations = prepareAcceptOffer['authorizations'];
const approvals = buildApprovals(privateKey, authorizations);
const acceptOfferInput = {
approvals,
offerId: `SingleSaleOffer:${offerId}`,
settlementInfo: {
currency: 'WEI',
paymentMethod: 'WALLET',
exchangeRateId: exchangeRateId,
},
clientMutationId: crypto.randomBytes(8).join(''),
};
const acceptOfferData = await graphQLClient.request(AcceptSingleSaleOffer, {
input: acceptOfferInput,
});
const acceptOffer = acceptOfferData['acceptOffer'];
if (acceptOffer['errors'].length > 0) {
acceptOffer['errors'].forEach(error => {
console.error(error['message']);
});
process.exit(2);
}
console.log('Success!');
}
main().catch(error => console.error(error));