Skip to content

Commit

Permalink
Remove pin json to pin file
Browse files Browse the repository at this point in the history
  • Loading branch information
chiliec committed Mar 13, 2024
1 parent 7cf0557 commit c5bbd5c
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/bot/features/dice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Composer } from "grammy";
import type { Context } from "#root/bot/context.js";
import { logHandle } from "#root/bot/helpers/logging.js";
import { placeInLine } from "#root/bot/models/user";
import { placeInLine } from "#root/bot/models/user.js";

function timeUnitsBetween(startDate: Date, endDate: Date) {
let delta = Math.abs(endDate.getTime() - startDate.getTime()) / 1000;
Expand Down
20 changes: 14 additions & 6 deletions src/bot/features/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { SelectImageButton, photoKeyboard } from "#root/bot/keyboards/photo.js";
import { NftCollection } from "#root/bot/models/nft-collection.js";
import { openWallet, waitSeqno } from "#root/bot/helpers/ton.js";
import { NftItem, nftMintParameters } from "#root/bot/models/nft-item.js";
import { pinFileToIPFS, pinJSONToIPFS } from "#root/bot/helpers/ipfs.js";
import { pinImageURLToIPFS, pinJSONToIPFS } from "#root/bot/helpers/ipfs.js";
import { generate } from "#root/bot/helpers/generation.js";
import { randomAttributes } from "#root/bot/helpers/attributes.js";
import { findUserById } from "#root/bot/models/user.js";
Expand Down Expand Up @@ -109,7 +109,7 @@ feature.callbackQuery(
return ctx.reply("Empty image");
}
const nextItemIndex = await NftCollection.fetchNextItemIndex();
const ipfsImageHash = await pinFileToIPFS(
const ipfsImageHash = await pinImageURLToIPFS(
nextItemIndex,
selectedUser.image ?? "",
);
Expand Down Expand Up @@ -151,13 +151,21 @@ feature.callbackQuery(
commonContentUrl: `ipfs://${selectedUser.nftJson}`,
};
ctx.logger.info(parameters);

selectedUser.minted = true;
await selectedUser.save();

const seqno = await item.deploy(wallet, parameters);
await waitSeqno(seqno, wallet);
const nft = await NftCollection.getNftAddressByIndex(nextItemIndex);
await ctx.reply(
`https://${config.TESTNET ? "testnet." : ""}getgems.io/collection/${config.COLLECTION_ADDRESS}/${nft.toString()}`,
{ link_preview_options: { is_disabled: true } },
);

const nftUrl = `https://${config.TESTNET ? "testnet." : ""}getgems.io/collection/${config.COLLECTION_ADDRESS}/${nft.toString()}`;
selectedUser.nftUrl = nftUrl;
await selectedUser.save();

await ctx.reply(nftUrl, {
link_preview_options: { is_disabled: true },
});
break;
}
default: {
Expand Down
4 changes: 2 additions & 2 deletions src/bot/features/start.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Composer } from "grammy";
import type { Context } from "#root/bot/context.js";
import { logHandle } from "#root/bot/helpers/logging.js";
import { VoteModel, isUserAlreadyVoted } from "#root/bot/models/vote";
import { findUserById } from "#root/bot/models/user";
import { findUserById } from "#root/bot/models/user.js";
import { VoteModel, isUserAlreadyVoted } from "#root/bot/models/vote.js";

const composer = new Composer<Context>();

Expand Down
11 changes: 4 additions & 7 deletions src/bot/helpers/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ export async function saveImageFromUrl(
return saveImage(index, newFileName, buffer);
}

export function saveJSON(index: number, json: object) {
const fp = folderPath(index);
fs.writeFile(
path.join(fp, `${index}.json`),
JSON.stringify(json),
(_error) => {},
);
export function saveJSON(index: number, json: object): string {
const jsonPath = path.join(folderPath(index), `${index}.json`);
fs.writeFile(jsonPath, JSON.stringify(json), (_error) => {});
return jsonPath;
}
2 changes: 1 addition & 1 deletion src/bot/helpers/generation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetch from "node-fetch";
import FormData from "form-data";
import fs from "node:fs";
import { config } from "#root/config";
import { config } from "#root/config.js";

export async function generate(
filePath: string,
Expand Down
23 changes: 9 additions & 14 deletions src/bot/helpers/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import pinataSDK from "@pinata/sdk";
import { Readable } from "node:stream";
import { saveImage, saveJSON } from "./files";

export async function pinFileToIPFS(
// eslint-disable-next-line new-cap
const pinata = new pinataSDK({
pinataApiKey: config.PINATA_API_KEY,
pinataSecretApiKey: config.PINATA_API_SECRET,
});

export async function pinImageURLToIPFS(
index: number,
imageURL: string,
): Promise<string> {
// eslint-disable-next-line new-cap
const pinata = new pinataSDK({
pinataApiKey: config.PINATA_API_KEY,
pinataSecretApiKey: config.PINATA_API_SECRET,
});

const image = await fetch(imageURL);
const arrayBuffer = await image.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
Expand All @@ -33,13 +33,8 @@ export async function pinJSONToIPFS(
index: number,
json: object,
): Promise<string> {
// eslint-disable-next-line new-cap
const pinata = new pinataSDK({
pinataApiKey: config.PINATA_API_KEY,
pinataSecretApiKey: config.PINATA_API_SECRET,
});
saveJSON(index, json);
const response = await pinata.pinJSONToIPFS(json, {
const jsonPath = saveJSON(index, json);
const response = await pinata.pinFromFS(jsonPath, {
pinataMetadata: { name: `${index}.json` },
});
return response.IpfsHash;
Expand Down
12 changes: 9 additions & 3 deletions src/bot/helpers/ton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Transaction,
WalletContractV4,
} from "@ton/ton";
import { config } from "#root/config";
import { config } from "#root/config.js";

export type OpenedWallet = {
contract: OpenedContract<WalletContractV4>;
Expand All @@ -29,9 +29,15 @@ export const tonClient = new TonClient({

export async function getTransactions(
address: Address,
afterLT: string,
lt: string,
hash: string,
): Promise<Transaction[]> {
return tonClient.getTransactions(address, { lt: afterLT, limit: 100 });
return tonClient.getTransactions(address, {
lt,
hash,
limit: 100,
archival: true,
});
}

export async function openWallet(mnemonic: string[]) {
Expand Down
8 changes: 2 additions & 6 deletions src/bot/keyboards/queue-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ import { photoKeyboard } from "#root/bot/keyboards/photo.js";

export function photoCaption(user: User) {
return `[${user.name}](tg://user?id=${user.id})
Comment: \`${user.description?.slice(0, 1000) ?? ""}\`
Description: \`${user.nftDescription ?? ""}\`
Image: ${user.nftImage ? `[ipfs](https://ipfs.io/ipfs/${user.nftImage})` : ""}
JSON: ${user.nftJson ? `[ipfs](https://ipfs.io/ipfs/${user.nftJson})` : ""}
${user.nftImage ? `[Image](https://ipfs.io/ipfs/${user.nftImage}) | ` : ""} ${user.nftJson ? `[JSON](https://ipfs.io/ipfs/${user.nftJson})` : ""}
Minted: ${user.minted ? "✅" : "❌"} ${user.nftUrl ?? ""}
`;
}

Expand Down
2 changes: 1 addition & 1 deletion src/bot/models/nft-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
OpenedWallet,
tonClient,
} from "#root/bot/helpers/ton.js";
import { config } from "#root/config";
import { config } from "#root/config.js";

export type collectionData = {
ownerAddress: Address;
Expand Down
2 changes: 1 addition & 1 deletion src/bot/models/nft-item.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Address, beginCell, Cell, internal, SendMode } from "@ton/core";
import { OpenedWallet } from "#root/bot/helpers/ton.js";
import { config } from "#root/config";
import { config } from "#root/config.js";

export type nftMintParameters = {
queryId: number;
Expand Down
3 changes: 3 additions & 0 deletions src/bot/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export class User extends TimeStamps {

@prop({ type: String })
nftJson?: string;

@prop({ type: String })
nftUrl?: string;
}

const UserModel = getModelForClass(User);
Expand Down
2 changes: 1 addition & 1 deletion src/bot/models/vote.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { modelOptions, prop, getModelForClass } from "@typegoose/typegoose";
import { TimeStamps } from "@typegoose/typegoose/lib/defaultClasses";
import { TimeStamps } from "@typegoose/typegoose/lib/defaultClasses.js";

@modelOptions({
schemaOptions: { timestamps: true },
Expand Down
16 changes: 9 additions & 7 deletions src/bot/subscription.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { i18n } from "#root/bot/i18n.js";
/* eslint-disable class-methods-use-this */
import { logger } from "#root/logger";
import { logger } from "#root/logger.js";
import { Address, fromNano } from "@ton/core";
import { config } from "#root/config";
import TonWeb from "tonweb";
import { config } from "#root/config.js";
import { Api, Bot, RawApi } from "grammy";
import { TransactionModel, getLastestTransaction } from "./models/transaction";
import { findUserByAddress, placeInLine } from "./models/user";
import { Context } from "./context";
import TonWeb from "tonweb";
import { Context } from "#root/bot/context.js";
import { findUserByAddress, placeInLine } from "#root/bot/models/user.js";
import {
TransactionModel,
getLastestTransaction,
} from "#root/bot/models/transaction.js";

export class Subscription {
bot: Bot<Context, Api<RawApi>>;
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { config } from "#root/config.js";
import { logger } from "#root/logger.js";
import { createServer } from "#root/server/index.js";
import mongoose from "mongoose";
import { Subscription } from "#root/bot/subscription";
import { Subscription } from "#root/bot/subscription.js";

try {
await mongoose.connect(config.MONGO);
Expand Down

0 comments on commit c5bbd5c

Please sign in to comment.