@nina-protocol/nina-db 0.0.102 → 0.0.104

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -37,8 +37,9 @@ export default class Account extends Model {
37
37
  this.verifications = verifications;
38
38
  }
39
39
  delete this.id;
40
- const followers = await Subscription.query().where('to', this.publicKey).range(0, 0);
41
- this.followers = followers.total;
40
+ // Use count() instead of range(0,0) to avoid temporary object creation
41
+ const followersCount = await Subscription.query().where('to', this.publicKey).count('* as count').first();
42
+ this.followers = parseInt(followersCount.count);
42
43
  };
43
44
  static relationMappings = () => ({
44
45
  published: {
@@ -31,8 +31,9 @@ export default class Hub extends Model {
31
31
  delete this.authorityId;
32
32
  delete this.id;
33
33
  stripHtmlIfNeeded(this.data, 'description');
34
- const followers = await Subscription.query().where('to', this.publicKey).range(0, 0);
35
- this.followers = followers.total;
34
+ // Use count() instead of range(0,0) to avoid temporary object creation
35
+ const followersCount = await Subscription.query().where('to', this.publicKey).count('* as count').first();
36
+ this.followers = parseInt(followersCount.count);
36
37
  }
37
38
  static relationMappings = () => ({
38
39
  authority: {
@@ -1,5 +1,4 @@
1
- import * as anchorSerum from '@project-serum/anchor';
2
- import * as anchorCoral from '@coral-xyz/anchor';
1
+ import anchor from '@project-serum/anchor';
3
2
  import { Metaplex } from '@metaplex-foundation/js';
4
3
  import { Model } from 'objection';
5
4
  import { stripHtmlIfNeeded, tweetNewRelease } from '../utils/index.js';
@@ -11,7 +10,6 @@ import Tag from './Tag.js';
11
10
  import axios from 'axios';
12
11
  import promiseRetry from 'promise-retry';
13
12
  import { customAlphabet } from 'nanoid';
14
- import { getTokenMetadata } from '@solana/spl-token';
15
13
  const ensureHttps = (uri) => {
16
14
  if (!uri.startsWith('http://') && !uri.startsWith('https://')) {
17
15
  return `https://${uri}`;
@@ -54,25 +52,21 @@ export default class Release extends Model {
54
52
  archived: { type: 'boolean' },
55
53
  },
56
54
  };
57
- static findOrCreate = async (publicKey, hubPublicKey = null, programId = process.env.NINA_PROGRAM_V2_ID) => {
55
+ static findOrCreate = async (publicKey, hubPublicKey = null) => {
58
56
  try {
59
- console.log('Release.findOrCreate', publicKey, programId);
60
57
  let release = await Release.query().findOne({ publicKey });
61
58
  if (release) {
62
- console.log('release found', release);
63
59
  return release;
64
60
  }
65
- let anchor = programId === process.env.NINA_PROGRAM_V2_ID ? anchorCoral : anchorSerum;
66
61
  const connection = new anchor.web3.Connection(process.env.SOLANA_CLUSTER_URL);
67
62
  const provider = new anchor.AnchorProvider(connection, {}, { commitment: 'confirmed' });
68
- const program = await anchor.Program.at(programId, provider);
69
- const programModelName = programId === process.env.NINA_PROGRAM_V2_ID ? 'releaseV2' : 'release';
63
+ const program = await anchor.Program.at(process.env.NINA_PROGRAM_ID, provider);
70
64
  const metaplex = new Metaplex(connection);
71
65
  let attempts = 0;
72
66
  const releaseAccount = await promiseRetry(async (retry) => {
73
67
  try {
74
68
  attempts += 1;
75
- const result = await program.account[programModelName].fetch(new anchor.web3.PublicKey(publicKey), 'confirmed');
69
+ const result = await program.account.release.fetch(new anchor.web3.PublicKey(publicKey), 'confirmed');
76
70
  return result;
77
71
  }
78
72
  catch (error) {
@@ -84,13 +78,7 @@ export default class Release extends Model {
84
78
  minTimeout: 500,
85
79
  maxTimeout: 1500,
86
80
  });
87
- let metadataAccount;
88
- if (programId === process.env.NINA_PROGRAM_V2_ID) {
89
- metadataAccount = await getTokenMetadata(connection, releaseAccount.mint, 'confirmed');
90
- }
91
- else {
92
- metadataAccount = (await metaplex.nfts().findAllByMintList({ mints: [releaseAccount.releaseMint] }, { commitment: 'confirmed' }))[0];
93
- }
81
+ let metadataAccount = (await metaplex.nfts().findAllByMintList({ mints: [releaseAccount.releaseMint] }, { commitment: 'confirmed' }))[0];
94
82
  if (!metadataAccount) {
95
83
  throw new Error('No metadata account found for release - is not a complete release');
96
84
  }
@@ -105,13 +93,12 @@ export default class Release extends Model {
105
93
  let publisher = await Account.findOrCreate(releaseAccount.authority.toBase58());
106
94
  release = await this.createRelease({
107
95
  publicKey,
108
- mint: programId === process.env.NINA_PROGRAM_V2_ID ? releaseAccount.mint.toBase58() : releaseAccount.releaseMint.toBase58(),
96
+ mint: releaseAccount.releaseMint.toBase58(),
109
97
  metadata: json,
110
- datetime: programId === process.env.NINA_PROGRAM_V2_ID ? new Date().toISOString() : new Date(releaseAccount.releaseDatetime.toNumber() * 1000).toISOString(),
98
+ datetime: new Date(releaseAccount.releaseDatetime.toNumber() * 1000).toISOString(),
111
99
  slug,
112
100
  publisherId: publisher.id,
113
- releaseAccount,
114
- programId,
101
+ releaseAccount
115
102
  });
116
103
  if (hubPublicKey) {
117
104
  const hub = await Hub.query().findOne({ publicKey: hubPublicKey });
@@ -127,7 +114,7 @@ export default class Release extends Model {
127
114
  return null;
128
115
  }
129
116
  };
130
- static createRelease = async ({ publicKey, mint, metadata, datetime, publisherId, releaseAccount, programId }) => {
117
+ static createRelease = async ({ publicKey, mint, metadata, datetime, publisherId, releaseAccount }) => {
131
118
  const slug = await this.generateSlug(metadata);
132
119
  const price = releaseAccount.account?.price?.toNumber() || releaseAccount?.price?.toNumber() || 0;
133
120
  const paymentMint = releaseAccount.account?.paymentMint.toBase58() || releaseAccount?.paymentMint.toBase58();
@@ -140,8 +127,7 @@ export default class Release extends Model {
140
127
  publisherId,
141
128
  price: `${price}`,
142
129
  paymentMint,
143
- archived: false,
144
- programId,
130
+ archived: false
145
131
  });
146
132
  if (metadata.properties.tags) {
147
133
  for await (let tag of metadata.properties.tags) {
@@ -149,9 +135,7 @@ export default class Release extends Model {
149
135
  await Release.relatedQuery('tags').for(release.id).relate(tagRecord.id).onConflict(['tagId', 'releaseId']).ignore();
150
136
  }
151
137
  }
152
- if (programId === process.env.NINA_PROGRAM_ID) {
153
- await this.processRevenueShares(releaseAccount, release);
154
- }
138
+ await this.processRevenueShares(releaseAccount, release);
155
139
  tweetNewRelease(metadata, publisherId, slug);
156
140
  return release;
157
141
  };
@@ -20,8 +20,6 @@ class Transaction extends Model {
20
20
  type: {
21
21
  type: 'string',
22
22
  enum: [
23
- 'ReleaseInitV2',
24
- 'ReleaseUpdate',
25
23
  'ExchangeAccept',
26
24
  'ExchangeCancel',
27
25
  'ExchangeInit',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nina-protocol/nina-db",
3
- "version": "0.0.102",
3
+ "version": "0.0.104",
4
4
  "description": "",
5
5
  "source": "src/index.js",
6
6
  "main": "dist/index.js",
@@ -23,8 +23,7 @@
23
23
  "@babel/plugin-proposal-object-rest-spread": "^7.20.7",
24
24
  "@babel/preset-es2015": "^7.0.0-beta.53",
25
25
  "@metaplex-foundation/js": "^0.18.1",
26
- "@coral-xyz/anchor": "^0.31.0",
27
- "@solana/spl-token": "^0.4.9",
26
+ "@project-serum/anchor": "^0.25.0",
28
27
  "axios": "^0.27.2",
29
28
  "knex": "^2.2.0",
30
29
  "nanoid": "^4.0.2",
@@ -47,6 +46,6 @@
47
46
  "rollup-plugin-babel": "^4.4.0",
48
47
  "rollup-plugin-commonjs": "^10.1.0",
49
48
  "rollup-plugin-terser": "^7.0.2",
50
- "typescript": "5.7.3"
49
+ "typescript": "4.3.5"
51
50
  }
52
51
  }
@@ -42,8 +42,9 @@ export default class Account extends Model {
42
42
  this.verifications = verifications;
43
43
  }
44
44
  delete this.id
45
- const followers = await Subscription.query().where('to', this.publicKey).range(0,0);
46
- this.followers = followers.total;
45
+ // Use count() instead of range(0,0) to avoid temporary object creation
46
+ const followersCount = await Subscription.query().where('to', this.publicKey).count('* as count').first();
47
+ this.followers = parseInt(followersCount.count);
47
48
  }
48
49
 
49
50
  static relationMappings = () => ({
package/src/models/Hub.js CHANGED
@@ -35,8 +35,9 @@ export default class Hub extends Model {
35
35
 
36
36
  stripHtmlIfNeeded(this.data, 'description');
37
37
 
38
- const followers = await Subscription.query().where('to', this.publicKey).range(0, 0);
39
- this.followers = followers.total;
38
+ // Use count() instead of range(0,0) to avoid temporary object creation
39
+ const followersCount = await Subscription.query().where('to', this.publicKey).count('* as count').first();
40
+ this.followers = parseInt(followersCount.count);
40
41
  }
41
42
 
42
43
  static relationMappings = () => ({
@@ -1,5 +1,4 @@
1
- import * as anchorSerum from '@project-serum/anchor';
2
- import * as anchorCoral from '@coral-xyz/anchor';
1
+ import anchor from '@project-serum/anchor';
3
2
  import { Metaplex } from '@metaplex-foundation/js';
4
3
  import { Model } from 'objection';
5
4
  import { stripHtmlIfNeeded, tweetNewRelease }from '../utils/index.js';
@@ -11,7 +10,6 @@ import Tag from './Tag.js';
11
10
  import axios from 'axios';
12
11
  import promiseRetry from 'promise-retry';
13
12
  import { customAlphabet } from 'nanoid';
14
- import { getTokenMetadata } from '@solana/spl-token';
15
13
 
16
14
  const ensureHttps = (uri) => {
17
15
  if (!uri.startsWith('http://') && !uri.startsWith('https://')) {
@@ -58,30 +56,27 @@ export default class Release extends Model {
58
56
  },
59
57
  }
60
58
 
61
- static findOrCreate = async (publicKey, hubPublicKey=null, programId=process.env.NINA_PROGRAM_V2_ID) => {
59
+ static findOrCreate = async (publicKey, hubPublicKey=null) => {
62
60
  try {
63
- console.log('Release.findOrCreate', publicKey, programId)
64
61
  let release = await Release.query().findOne({ publicKey });
65
62
  if (release) {
66
- console.log('release found', release)
67
63
  return release;
68
64
  }
69
65
 
70
- let anchor = programId === process.env.NINA_PROGRAM_V2_ID ? anchorCoral : anchorSerum;
71
66
  const connection = new anchor.web3.Connection(process.env.SOLANA_CLUSTER_URL);
72
67
  const provider = new anchor.AnchorProvider(connection, {}, {commitment: 'confirmed'})
73
68
  const program = await anchor.Program.at(
74
- programId,
69
+ process.env.NINA_PROGRAM_ID,
75
70
  provider,
76
71
  )
77
- const programModelName = programId === process.env.NINA_PROGRAM_V2_ID ? 'releaseV2' : 'release';
78
72
  const metaplex = new Metaplex(connection);
79
73
  let attempts = 0;
74
+
80
75
  const releaseAccount = await promiseRetry(
81
76
  async (retry) => {
82
77
  try {
83
78
  attempts += 1
84
- const result = await program.account[programModelName].fetch(new anchor.web3.PublicKey(publicKey), 'confirmed')
79
+ const result = await program.account.release.fetch(new anchor.web3.PublicKey(publicKey), 'confirmed')
85
80
  return result
86
81
  } catch (error) {
87
82
  console.log('error fetching release account', error)
@@ -94,12 +89,7 @@ export default class Release extends Model {
94
89
  }
95
90
  )
96
91
 
97
- let metadataAccount
98
- if (programId === process.env.NINA_PROGRAM_V2_ID) {
99
- metadataAccount = await getTokenMetadata(connection, releaseAccount.mint, 'confirmed')
100
- } else {
101
- metadataAccount = (await metaplex.nfts().findAllByMintList({mints: [releaseAccount.releaseMint]}, { commitment: 'confirmed' }))[0];
102
- }
92
+ let metadataAccount = (await metaplex.nfts().findAllByMintList({mints: [releaseAccount.releaseMint]}, { commitment: 'confirmed' }))[0];
103
93
  if (!metadataAccount) {
104
94
  throw new Error('No metadata account found for release - is not a complete release')
105
95
  }
@@ -114,13 +104,12 @@ export default class Release extends Model {
114
104
  let publisher = await Account.findOrCreate(releaseAccount.authority.toBase58());
115
105
  release = await this.createRelease({
116
106
  publicKey,
117
- mint: programId === process.env.NINA_PROGRAM_V2_ID ? releaseAccount.mint.toBase58() : releaseAccount.releaseMint.toBase58(),
107
+ mint: releaseAccount.releaseMint.toBase58(),
118
108
  metadata: json,
119
- datetime: programId === process.env.NINA_PROGRAM_V2_ID ? new Date().toISOString() : new Date(releaseAccount.releaseDatetime.toNumber() * 1000).toISOString(),
109
+ datetime: new Date(releaseAccount.releaseDatetime.toNumber() * 1000).toISOString(),
120
110
  slug,
121
111
  publisherId: publisher.id,
122
- releaseAccount,
123
- programId,
112
+ releaseAccount
124
113
  });
125
114
 
126
115
  if (hubPublicKey) {
@@ -139,7 +128,7 @@ export default class Release extends Model {
139
128
  }
140
129
  }
141
130
 
142
- static createRelease = async ({publicKey, mint, metadata, datetime, publisherId, releaseAccount, programId}) => {
131
+ static createRelease = async ({publicKey, mint, metadata, datetime, publisherId, releaseAccount}) => {
143
132
  const slug = await this.generateSlug(metadata);
144
133
  const price = releaseAccount.account?.price?.toNumber() || releaseAccount?.price?.toNumber() || 0;
145
134
  const paymentMint = releaseAccount.account?.paymentMint.toBase58() || releaseAccount?.paymentMint.toBase58();
@@ -152,8 +141,7 @@ export default class Release extends Model {
152
141
  publisherId,
153
142
  price: `${price}`,
154
143
  paymentMint,
155
- archived: false,
156
- programId,
144
+ archived: false
157
145
  })
158
146
  if (metadata.properties.tags) {
159
147
  for await (let tag of metadata.properties.tags) {
@@ -161,9 +149,7 @@ export default class Release extends Model {
161
149
  await Release.relatedQuery('tags').for(release.id).relate(tagRecord.id).onConflict(['tagId', 'releaseId']).ignore();
162
150
  }
163
151
  }
164
- if (programId === process.env.NINA_PROGRAM_ID) {
165
- await this.processRevenueShares(releaseAccount, release);
166
- }
152
+ await this.processRevenueShares(releaseAccount, release);
167
153
  tweetNewRelease(metadata, publisherId, slug);
168
154
  return release;
169
155
  }
@@ -21,8 +21,6 @@ class Transaction extends Model {
21
21
  type: {
22
22
  type: 'string',
23
23
  enum: [
24
- 'ReleaseInitV2',
25
- 'ReleaseUpdate',
26
24
  'ExchangeAccept',
27
25
  'ExchangeCancel',
28
26
  'ExchangeInit',
@@ -1,19 +0,0 @@
1
- /**
2
- * @param { import("knex").Knex } knex
3
- * @returns { Promise<void> }
4
- */
5
- export const up = function(knex) {
6
- return knex.schema.table('releases', table => {
7
- table.string('programId').notNullable().defaultTo(process.env.NINA_PROGRAM_ID);
8
- });
9
- };
10
-
11
- /**
12
- * @param { import("knex").Knex } knex
13
- * @returns { Promise<void> }
14
- */
15
- export const down = function(knex) {
16
- return knex.schema.table('releases', table => {
17
- table.dropColumn('programId');
18
- });
19
- };
@@ -1,19 +0,0 @@
1
- /**
2
- * @param { import("knex").Knex } knex
3
- * @returns { Promise<void> }
4
- */
5
- export const up = function(knex) {
6
- return knex.schema.table('transactions', table => {
7
- table.string('programId').notNullable().defaultTo(process.env.NINA_PROGRAM_ID);
8
- });
9
- };
10
-
11
- /**
12
- * @param { import("knex").Knex } knex
13
- * @returns { Promise<void> }
14
- */
15
- export const down = function(knex) {
16
- return knex.schema.table('transactions', table => {
17
- table.dropColumn('programId');
18
- });
19
- };