Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions examples/bidAuction.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
const { GraphQLClient, gql } = require('graphql-request');
const { signLimitOrder } = require('@sorare/crypto');
const crypto = require('crypto');
const yargs = require('yargs');
import { GraphQLClient, gql } from 'graphql-request';
import crypto from 'crypto';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

import {
authorizationRequestFragment,
buildApprovals,
} from '../authorizations';
} from './authorizations.js';

const { auctionId, token, privateKey, jwtAud } = yargs
const argv = yargs(hideBin(process.argv))
.command(
'bidAuctionWithEth',
'Make the minimum next bid on an english auction.'
)
.option('auctionId', {
description: 'The auction id.',
description: 'The auction id with "EnglishAuction:" prefix.',
type: 'string',
required: true,
demandOption: true,
})
.option('token', {
description: 'The JWT or OAuth token.',
type: 'string',
required: true,
demandOption: true,
})
.option('private-key', {
description: 'Your Starkware private key',
type: 'string',
required: true,
demandOption: true,
})
.option('jwt-aud', {
description: 'The JWT audience (required if using a JWT token).',
type: 'string',
})
.help()
.alias('help', 'h').argv;
.alias('help', 'h')
.parse();

const { auctionId: auctionUuid, token, privateKey, jwtAud } = argv;

const Config = gql`
query ConfigQuery {
Expand Down Expand Up @@ -84,40 +87,42 @@ const Bid = gql`

async function main() {
const graphQLClient = new GraphQLClient(
'https://api.sorare.dev/graphql',
'https://api.sorare.com/graphql',
{
headers: {
Authorization: `Bearer ${token}`,
'JWT-AUD': jwtAud,
// 'APIKEY': '<YourOptionalAPIKey>'
'APIKEY': token
},
}
);

const configData = await graphQLClient.request(Config);

const exchangeRateId = configData['config']['exchangeRate']['id'];
console.log('Using exchange rate id', exchangeRateId);

const englishAuctionData = await graphQLClient.request(EnglishAuction, {
auctionId: auctionId,
auctionId: auctionUuid.replace("EnglishAuction:", ""), // to search not use prefix
});
const bidAmountInWei = englishAuctionData['tokens']['auction']['minNextBid'];
console.log('Minimum next bid is', bidAmountInWei, 'wei');

const prepareBidInput = {
englishAuctionId: auctionId,
auctionId: auctionUuid, // to bid, use prefix
amount: bidAmountInWei,
settlementInfo: {
currency: 'WEI',
paymentMethod: 'WALLET',
exchangeRateId: exchangeRateId,
},
};


const prepareBidData = await graphQLClient.request(PrepareBid, {
input: prepareBidInput,
});

const prepareBid = prepareBidData['prepareBid'];
if (prepareBid['errors'].length > 0) {
if (prepareBid['errors'] && prepareBid['errors'].length > 0) {
prepareBid['errors'].forEach(error => {
console.error(error['message']);
});
Expand All @@ -129,7 +134,7 @@ async function main() {

const bidInput = {
approvals,
auctionId: `EnglishAuction:${auctionId}`,
auctionId: auctionUuid,
amount: bidAmountInWei,
settlementInfo: {
currency: 'WEI',
Expand All @@ -143,14 +148,15 @@ async function main() {
console.log(bidData);

const bid = bidData['bid'];
if (bid['errors'].length > 0) {
if (bid['errors'] && bid['errors'].length > 0) {
bid['errors'].forEach(error => {
console.error(error['message']);
});
process.exit(2);
}

console.log('Success!');
}

main().catch(error => console.error(error));
main().catch(error => {
console.error(error)
});