Skip to content

Commit

Permalink
feat: add 1155 support to minting backend (#1837)
Browse files Browse the repository at this point in the history
  • Loading branch information
shineli1984 authored May 30, 2024
1 parent b327d6a commit 4943ddf
Show file tree
Hide file tree
Showing 86 changed files with 42 additions and 11 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ jest.*.js
**sample-app**/
**playground**/
samples/apps/passport/
samples/**/*

# put module specific ignore paths here
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"rollup.config.*",
"node_modules/",
"dist/",
"samples/",
"**sample-app**/",
"**playground**/"
],
Expand Down
4 changes: 4 additions & 0 deletions packages/minting-backend/sdk/src/minting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ export const submitMintingRequests = async (
reference_id: row.asset_id,
owner_address: row.owner_address,
metadata: row.metadata,
token_id: row.token_id,
amount: row.amount ? `${row.amount}` : null,
})),
},
};
Expand Down Expand Up @@ -209,6 +211,7 @@ export type MintRequestEvent = {
} | null;
created_at: string;
updated_at: string;
amount?: number;
}
};

Expand Down Expand Up @@ -264,5 +267,6 @@ export const processMint = async (
metadataId: event.data.metadata_id,
imtblZkevmMintRequestUpdatedId: event.event_id,
error: event.data.error ? JSON.stringify(event.data.error) : null,
amount: event.data.amount || null,
});
};
18 changes: 12 additions & 6 deletions packages/minting-backend/sdk/src/persistence/pg/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ export const mintingPersistence = (client: Pool): MintingPersistence => {
recordMint: async (request: CreateMintRequest) => {
const r = await client.query(
`
INSERT INTO im_assets (asset_id, contract_address, owner_address, metadata)
VALUES ($1, $2, $3, $4) ON CONFLICT (asset_id, contract_address) DO NOTHING;
INSERT INTO im_assets (asset_id, contract_address, owner_address, metadata, amount, token_id)
VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (asset_id, contract_address) DO NOTHING;
`,
[request.asset_id, request.contract_address, request.owner_address, request.metadata]
[
request.asset_id, request.contract_address, request.owner_address,
request.metadata, request.amount, request.token_id
]
);
if (r.rowCount === 0) {
throw new Error('Duplicated mint');
Expand Down Expand Up @@ -41,8 +44,9 @@ export const mintingPersistence = (client: Pool): MintingPersistence => {
minting_status,
metadata_id,
last_imtbl_zkevm_mint_request_updated_id,
error
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) ON CONFLICT (asset_id, contract_address)
error,
amount
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) ON CONFLICT (asset_id, contract_address)
DO UPDATE SET
owner_address = $3,
token_id = $4,
Expand All @@ -62,7 +66,9 @@ export const mintingPersistence = (client: Pool): MintingPersistence => {
submittedMintRequest.status,
submittedMintRequest.metadataId,
submittedMintRequest.imtblZkevmMintRequestUpdatedId,
submittedMintRequest.error]);
submittedMintRequest.error,
submittedMintRequest.amount
]);
},
markAsConflict: async (assetIds: string[], contractAddress: string) => {
await client.query(`
Expand Down
1 change: 1 addition & 0 deletions packages/minting-backend/sdk/src/persistence/pg/seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CREATE TABLE IF NOT EXISTS im_assets (
metadata_id UUID NULL,
tried_count INT DEFAULT 0,
last_imtbl_zkevm_mint_request_updated_id UUID NULL,
amount INT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ model ImAssets {
metadataId String? // Previously @db.Uuid in Postgres
triedCount Int @default(0)
lastImtblZkevmMintRequestUpdatedId String? // Previously @db.Uuid in Postgres
amount Int?
createdAt DateTime @default(now()) // Stored as TEXT
updatedAt DateTime @default(now()) // Stored as TEXT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const mintingPersistence = (client: any): MintingPersistence => {
contractAddress: request.contract_address,
ownerAddress: request.owner_address,
metadata: JSON.stringify(request.metadata), // Serialize JSON metadata
amount: request.amount || null,
tokenId: request.token_id || null,
},
});

Expand Down Expand Up @@ -69,6 +71,8 @@ export const mintingPersistence = (client: any): MintingPersistence => {
metadata: asset.metadata ? JSON.parse(asset.metadata) : null,
owner_address: asset.ownerAddress,
tried_count: asset.triedCount,
amount: asset.amount || null,
token_id: asset.tokenId || null,
}));
},
updateMintingStatusToSubmitted: async (ids: string[]) => {
Expand Down Expand Up @@ -114,6 +118,7 @@ export const mintingPersistence = (client: any): MintingPersistence => {
metadataId: submittedMintRequest.metadataId,
lastImtblZkevmMintRequestUpdatedId: submittedMintRequest.imtblZkevmMintRequestUpdatedId,
error: submittedMintRequest.error,
amount: submittedMintRequest.amount || null
}
});
} else if (!existingAsset) {
Expand All @@ -128,6 +133,7 @@ export const mintingPersistence = (client: any): MintingPersistence => {
metadataId: submittedMintRequest.metadataId,
lastImtblZkevmMintRequestUpdatedId: submittedMintRequest.imtblZkevmMintRequestUpdatedId,
error: submittedMintRequest.error,
amount: submittedMintRequest.amount || null
}
});
}
Expand Down Expand Up @@ -216,7 +222,8 @@ export const mintingPersistence = (client: any): MintingPersistence => {
metadata: asset.metadata ? JSON.parse(asset.metadata) : null,
owner_address: asset.ownerAddress,
tried_count: asset.triedCount,
wallet_address: asset.ownerAddress
wallet_address: asset.ownerAddress,
amount: asset.amount || null
};
}
};
Expand Down
5 changes: 5 additions & 0 deletions packages/minting-backend/sdk/src/persistence/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export type CreateMintRequest = {
asset_id: string; // the web2 game item id
metadata: any;
owner_address: string;
amount?: number; // for ERC1155 only
token_id?: string; // token id is only required for ERC1155
};

export type MintRequest = {
Expand All @@ -14,6 +16,8 @@ export type MintRequest = {
metadata: any;
owner_address: string;
tried_count: number;
amount?: number;
token_id?: string;
};

export type SubmittedMintRequest = {
Expand All @@ -25,6 +29,7 @@ export type SubmittedMintRequest = {
metadataId: string;
imtblZkevmMintRequestUpdatedId: string;
error: any | null;
amount: number | null;
};

export interface MintingPersistence {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"license": "ISC",
"dependencies": {
"@fastify/cors": "^9.0.1",
"@imtbl/sdk": "1.36.1-alpha",
"@imtbl/sdk": "alpha",
"@prisma/client": "^5.12.1",
"@types/jsonwebtoken": "^9.0.6",
"aws-sdk": "^2.1603.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const EIP1193ContextProvider = ({children}: EIP1193ContextProvider) => {

useEffect(() => {
if(provider && provider.provider) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(provider.provider as any)?.on('accountsChanged', (accounts: string[]) => {
setWalletAddress(accounts.length > 0 ? accounts[0].toLowerCase() : "");
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Fastify, { FastifyReply, FastifyRequest } from 'fastify'
import { mintingBackend, config } from '@imtbl/sdk';
import { Pool } from 'pg';
import { v4 as uuidv4 } from 'uuid';
import { v4 as uuidv4, stringify, parse } from 'uuid';

const fastify = Fastify({
logger: true
Expand Down Expand Up @@ -36,6 +36,7 @@ interface MintRequest {
fastify.post('/mint', async (request: FastifyRequest<{ Body: MintRequest }>, reply: FastifyReply) => {
const collectionAddress = process.env.COLLECTION_ADDRESS as string;
const assetId = uuidv4();
const assetIdBuffer = parse(assetId);
const mintTo = request.body.mintTo;
const metadata = {
name: 'My NFT',
Expand All @@ -47,7 +48,9 @@ fastify.post('/mint', async (request: FastifyRequest<{ Body: MintRequest }>, rep
asset_id: assetId,
contract_address: collectionAddress,
owner_address: mintTo,
metadata
metadata,
// amount: 5, // for 1155 only
// token_id: Buffer.from(assetIdBuffer).readUint32BE(0).toString(10), // required for 1155 only. 721 can omit this.
});
reply.send({});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@imtbl/sdk": "1.36.1-alpha",
"@imtbl/sdk": "alpha",
"@types/pg": "^8.11.6",
"@types/uuid": "^9.0.8",
"fastify": "^4.27.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CREATE TABLE IF NOT EXISTS im_assets (
metadata_id UUID NULL,
tried_count INT DEFAULT 0,
last_imtbl_zkevm_mint_request_updated_id UUID NULL,
amount INT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Expand Down

0 comments on commit 4943ddf

Please sign in to comment.