Anastasia-Labs / direct-offer-offchain

https://www.npmjs.com/package/@anastasia-labs/direct-offer-offchain
MIT License
0 stars 0 forks source link

Cannot fetch offers - getOfferUTxOs #8

Open HinsonSIDAN opened 8 months ago

HinsonSIDAN commented 8 months ago

Calling this instance with implementation on here: https://github.com/maestro-org/midi/blob/feature/direct-swap-single-asset-staking-apis/src/controllers/directSwap.controller/getAllOffers.controller.ts

Getting the error message of do not know how to serialize a bigint

nikhils9 commented 8 months ago

Can you provide the read permission for this repo?

nikhils9 commented 8 months ago

We generally solve this common serialization issue with bigint using a replacer function https://github.com/Anastasia-Labs/direct-offer-offchain/blob/main/src/core/utils/utils.ts#L187 In conjunction with JSON.stringify(offers, replacer)

HinsonSIDAN commented 8 months ago

Thanks for your swift reply! I can share the code snippet maybe, pretty much it is just directly calling the function, do i miss anything?

class GetAllOffersController implements Controller {
    public path = '/contracts/directSwap/getOffers'
    public router = Router()
    // placeholder for when/if we want to store these in a database
    // private getAllOffers = getAllOffersModel;

    constructor() {
        this.initializeRoutes()
    }

    private initializeRoutes() {
        this.router.get(this.path, this.createGetAllOffers)
    }

    private createGetAllOffers = async (
        request: Request,
        response: Response
    ) => {
        const acceptOfferConfig: FetchOfferConfig = {
            scripts: {
                spending: contracts.directOfferSpending.cborHex,
                staking: contracts.directOfferStaking.cborHex,
            },
        }

        try {
            const allOffers = await getOfferUTxOs(
                lucidSession,
                acceptOfferConfig
            )
            response.json(allOffers)
            return
        } catch (error) {
            response.status(400)
            const errMessageClean = error.message.toLowerCase()
            response.send(errMessageClean)
            console.log(error.message)
            return
        }
    }
}
nikhils9 commented 8 months ago

I believe the issue is occurring at response.json(allOffers) Like I mentioned above its a serialization issue with bigint when you are trying to convert the allOffers into a json string maybe. If you serialize it with a replacer function like we have one for JSON.stringify(offers, replacer) (https://github.com/Anastasia-Labs/direct-offer-offchain/blob/main/src/core/utils/utils.ts#L187 ) it should work.