Skip to content

Commit

Permalink
[api] Fix #1602 - Format bignumber strings for /purchases endpoint (#…
Browse files Browse the repository at this point in the history
…1619)

* fix #1602

* npm version prerelease
  • Loading branch information
Atmosfearful committed Oct 2, 2023
1 parent 2ec09dc commit 346fcd9
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion carbonmark-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@klimadao/carbonmark-api",
"version": "2.0.0-2",
"version": "2.0.0-3",
"description": "An API for exploring Carbonmark project data, prices and activity.",
"main": "app.ts",
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions carbonmark-api/src/models/Purchase.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export const PurchaseModel = Type.Object({
],
}),
amount: Type.String({
description: "Stringified 18 decimal BigNumber",
examples: ["1000000000000000000"],
description: "Quantity of credits purchased",
examples: ["1.0"],
}),
listing: Type.Object({
id: Type.String({
Expand Down Expand Up @@ -38,8 +38,8 @@ export const PurchaseModel = Type.Object({
}),
}),
price: Type.String({
description: "Stringified 6 decimal BigNumber",
examples: ["1000000"],
description: "Total purchase price (USDC)",
examples: ["5.0"],
}),
});

Expand Down
5 changes: 4 additions & 1 deletion carbonmark-api/src/routes/purchases/[id]/get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Static } from "@sinclair/typebox";
import { utils } from "ethers";
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { isNil } from "lodash";
import { Purchase } from "../../../models/Purchase.model";
Expand Down Expand Up @@ -35,7 +36,9 @@ const handler = async (
});

const response: Purchase = {
...purchase,
id: purchase.id,
amount: utils.formatUnits(purchase.amount, 18),
price: utils.formatUnits(purchase.price, 6),
listing: {
id: purchase.listing.id,
project: {
Expand Down
4 changes: 2 additions & 2 deletions carbonmark-api/test/fixtures/marketplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const projectWithListing = aProject({

/** marketplace.getPurchaseById() */
const purchase: GetPurchaseByIdQuery["purchase"] = {
amount: "1000000000000000000", // 1t
amount: "1000000000000000000", // 1 tonne
id: "0xfe2949e3644b88d2e00e5f84f6266c191dbb0379a18e00a18192116de7c5c779",
price: "1000000", // $1
price: "5000000", // 5 USDC
listing: {
id: "0x01",
project: {
Expand Down
4 changes: 3 additions & 1 deletion carbonmark-api/test/routes/purchases/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { DEV_URL, ERROR } from "../../test.constants";

const purchaseModelFixture: Purchase = {
...marketplace.purchase,
amount: "1.0",
price: "5.0",
listing: {
id: marketplace.purchase.listing.id,
project: {
Expand Down Expand Up @@ -43,7 +45,7 @@ describe("GET /purchases/:id", () => {
});

/** The happy path */
test("Success", async () => {
test("Returns and formats BigNumber values", async () => {
// Mock the response from the graph
nock(GRAPH_URLS["polygon"].marketplace)
.post("")
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 346fcd9

@vercel
Copy link

@vercel vercel bot commented on 346fcd9 Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

demo-integration – ./examples/nextjs-typescript-integration

demo-integration-klimadao.vercel.app
demo-integration-git-staging-klimadao.vercel.app
integration-demo.carbonmark.com
klimadao-sable.vercel.app

Please sign in to comment.