mintbase-sdk 1.0.0

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.
Files changed (3) hide show
  1. package/README.md +67 -0
  2. package/index.js +199 -0
  3. package/package.json +29 -0
package/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # mintbase-sdk
2
+
3
+ A lightweight npm package for interacting with Mintbase and the NEAR blockchain — mint NFTs, transfer tokens, and query account state with ease.
4
+
5
+ ## Installation
6
+
7
+ npm install mintbase-sdk
8
+
9
+ ## API & Usage
10
+
11
+ ### `createWallet(accountId: string): Promise<MintbaseWallet>`
12
+
13
+ import { createWallet } from 'mintbase-sdk';
14
+
15
+ const wallet = await createWallet('alice.near');
16
+
17
+ ---
18
+
19
+ ### `getAccountState(accountId: string): Promise<NearRpcResult>`
20
+
21
+ import { getAccountState } from 'mintbase-sdk';
22
+
23
+ const state = await getAccountState('alice.near');
24
+
25
+ ---
26
+
27
+ ### `mintNFT(params): Promise<NearRpcResult>`
28
+
29
+ import { mintNFT } from 'mintbase-sdk';
30
+
31
+ const result = await mintNFT({
32
+ accountId: 'alice.near',
33
+ contractId: 'my-store.mintbase1.near',
34
+ metadata: { title: 'My NFT', description: 'A cool token' },
35
+ keyPair: wallet.keyPair,
36
+ networkId: 'mainnet',
37
+ });
38
+
39
+ ---
40
+
41
+ ### `transferNFT(params): Promise<NearRpcResult>`
42
+
43
+ import { transferNFT } from 'mintbase-sdk';
44
+
45
+ const result = await transferNFT({
46
+ accountId: 'alice.near',
47
+ contractId: 'my-store.mintbase1.near',
48
+ tokenId: '123',
49
+ receiverId: 'bob.near',
50
+ keyPair: wallet.keyPair,
51
+ networkId: 'mainnet',
52
+ });
53
+
54
+ ---
55
+
56
+ ### `getNFTsForOwner(params): Promise<NearRpcResult>`
57
+
58
+ import { getNFTsForOwner } from 'mintbase-sdk';
59
+
60
+ const nfts = await getNFTsForOwner({
61
+ accountId: 'alice.near',
62
+ contractId: 'my-store.mintbase1.near',
63
+ });
64
+
65
+ ## License
66
+
67
+ MIT
package/index.js ADDED
@@ -0,0 +1,199 @@
1
+ import * as nearApiJs from 'near-api-js';
2
+ import { KeyPair } from 'near-api-js';
3
+ import { PublicKey } from 'near-api-js/lib/utils';
4
+
5
+ const NEAR_RPC = 'https://rpc.mainnet.near.org';
6
+ const MINTBASE_CONTRACT = 'mintbase1.near';
7
+
8
+ /** Generated wallet keypair and accountId */
9
+ export interface MintbaseWallet {
10
+ accountId: string;
11
+ keyPair: nearApiJs.KeyPair;
12
+ publicKey: string;
13
+ }
14
+
15
+ /** NFT token metadata */
16
+ export interface TokenMetadata {
17
+ title: string;
18
+ description?: string;
19
+ media?: string;
20
+ copies?: number;
21
+ extra?: string;
22
+ }
23
+
24
+ /** RPC response shape */
25
+ export interface NearRpcResult {
26
+ result?: unknown;
27
+ error?: string;
28
+ }
29
+
30
+ /**
31
+ * Creates a new NEAR keypair and returns wallet info for use with SDK functions.
32
+ */
33
+ export async function createWallet(accountId: string): Promise<MintbaseWallet> {
34
+ const keyPair = nearApiJs.KeyPair.fromRandom('ed25519');
35
+ const publicKey = keyPair.getPublicKey().toString();
36
+ return { accountId, keyPair, publicKey };
37
+ }
38
+
39
+ /**
40
+ * Fetches the NEAR account state (balance, storage) via RPC.
41
+ */
42
+ export async function getAccountState(
43
+ accountId: string
44
+ ): Promise<NearRpcResult> {
45
+ const body = {
46
+ jsonrpc: '2.0',
47
+ id: 'dontcare',
48
+ method: 'query',
49
+ params: {
50
+ request_type: 'view_account',
51
+ finality: 'final',
52
+ account_id: accountId,
53
+ },
54
+ };
55
+ const response = await fetch(NEAR_RPC, {
56
+ method: 'POST',
57
+ headers: { 'Content-Type': 'application/json' },
58
+ body: JSON.stringify(body),
59
+ });
60
+ const data = await response.json();
61
+ if (data.error) return { error: data.error.message ?? JSON.stringify(data.error) };
62
+ return { result: data.result };
63
+ }
64
+
65
+ /**
66
+ * Mints an NFT on a Mintbase store contract using near-api-js functionCall.
67
+ */
68
+ export async function mintNFT(params: {
69
+ accountId: string;
70
+ contractId?: string;
71
+ metadata: TokenMetadata;
72
+ keyPair: nearApiJs.KeyPair;
73
+ networkId?: string;
74
+ }): Promise<NearRpcResult> {
75
+ const {
76
+ accountId,
77
+ contractId = MINTBASE_CONTRACT,
78
+ metadata,
79
+ keyPair,
80
+ networkId = 'mainnet',
81
+ } = params;
82
+
83
+ const keyStore = new nearApiJs.keyStores.InMemoryKeyStore();
84
+ await keyStore.setKey(networkId, accountId, keyPair);
85
+
86
+ const near = await nearApiJs.connect({
87
+ networkId,
88
+ keyStore,
89
+ nodeUrl: NEAR_RPC,
90
+ });
91
+
92
+ const account = await near.account(accountId);
93
+
94
+ try {
95
+ const result = await account.functionCall({
96
+ contractId,
97
+ methodName: 'nft_mint',
98
+ args: {
99
+ token_id: `token-${Date.now()}`,
100
+ receiver_id: accountId,
101
+ token_metadata: metadata,
102
+ },
103
+ gas: BigInt('300000000000000'),
104
+ attachedDeposit: BigInt('7000000000000000000000'),
105
+ });
106
+ return { result };
107
+ } catch (err: unknown) {
108
+ return { error: err instanceof Error ? err.message : String(err) };
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Transfers an NFT token to a receiver on a Mintbase store contract.
114
+ */
115
+ export async function transferNFT(params: {
116
+ accountId: string;
117
+ contractId?: string;
118
+ tokenId: string;
119
+ receiverId: string;
120
+ keyPair: nearApiJs.KeyPair;
121
+ networkId?: string;
122
+ }): Promise<NearRpcResult> {
123
+ const {
124
+ accountId,
125
+ contractId = MINTBASE_CONTRACT,
126
+ tokenId,
127
+ receiverId,
128
+ keyPair,
129
+ networkId = 'mainnet',
130
+ } = params;
131
+
132
+ const keyStore = new nearApiJs.keyStores.InMemoryKeyStore();
133
+ await keyStore.setKey(networkId, accountId, keyPair);
134
+
135
+ const near = await nearApiJs.connect({
136
+ networkId,
137
+ keyStore,
138
+ nodeUrl: NEAR_RPC,
139
+ });
140
+
141
+ const account = await near.account(accountId);
142
+
143
+ try {
144
+ const result = await account.functionCall({
145
+ contractId,
146
+ methodName: 'nft_transfer',
147
+ args: { receiver_id: receiverId, token_id: tokenId },
148
+ gas: BigInt('30000000000000'),
149
+ attachedDeposit: BigInt('1'),
150
+ });
151
+ return { result };
152
+ } catch (err: unknown) {
153
+ return { error: err instanceof Error ? err.message : String(err) };
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Queries NFTs owned by an account from a Mintbase store contract via RPC view call.
159
+ */
160
+ export async function getNFTsForOwner(params: {
161
+ accountId: string;
162
+ contractId?: string;
163
+ }): Promise<NearRpcResult> {
164
+ const { accountId, contractId = MINTBASE_CONTRACT } = params;
165
+
166
+ const args = Buffer.from(
167
+ JSON.stringify({ account_id: accountId, from_index: '0', limit: 50 })
168
+ ).toString('base64');
169
+
170
+ const body = {
171
+ jsonrpc: '2.0',
172
+ id: 'dontcare',
173
+ method: 'query',
174
+ params: {
175
+ request_type: 'call_function',
176
+ finality: 'final',
177
+ account_id: contractId,
178
+ method_name: 'nft_tokens_for_owner',
179
+ args_base64: args,
180
+ },
181
+ };
182
+
183
+ const response = await fetch(NEAR_RPC, {
184
+ method: 'POST',
185
+ headers: { 'Content-Type': 'application/json' },
186
+ body: JSON.stringify(body),
187
+ });
188
+ const data = await response.json();
189
+ if (data.error) return { error: data.error.message ?? JSON.stringify(data.error) };
190
+
191
+ try {
192
+ const decoded = JSON.parse(
193
+ Buffer.from(data.result.result).toString()
194
+ );
195
+ return { result: decoded };
196
+ } catch {
197
+ return { result: data.result };
198
+ }
199
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "mintbase-sdk",
3
+ "version": "1.0.0",
4
+ "description": "npm Package - mintbase-sdk",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "jest --passWithNoTests"
8
+ },
9
+ "keywords": [
10
+ "near",
11
+ "blockchain",
12
+ "web3"
13
+ ],
14
+ "license": "MIT",
15
+ "dependencies": {
16
+ "near-api-js": "^4.0.0"
17
+ },
18
+ "devDependencies": {
19
+ "typescript": "^5.0.0",
20
+ "@types/node": "^20.0.0",
21
+ "jest": "^29.0.0",
22
+ "@types/jest": "^29.0.0",
23
+ "ts-jest": "^29.0.0"
24
+ },
25
+ "files": [
26
+ "dist/**/*",
27
+ "README.md"
28
+ ]
29
+ }