forked from keypom/keypom
-
Notifications
You must be signed in to change notification settings - Fork 9
/
nft.js
184 lines (161 loc) · 4.95 KB
/
nft.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const { connect, KeyPair, keyStores, utils } = require("near-api-js");
const { parseNearAmount, formatNearAmount } = require("near-api-js/lib/utils/format");
const path = require("path");
const homedir = require("os").homedir();
let LINKDROP_PROXY_CONTRACT_ID = process.env.LINKDROP_PROXY_CONTRACT_ID;
let FUNDING_ACCOUNT_ID = process.env.FUNDING_ACCOUNT_ID;
let LINKDROP_NEAR_AMOUNT = process.env.LINKDROP_NEAR_AMOUNT;
let SEND_MULTIPLE = process.env.SEND_MULTIPLE;
let OFFSET = 2;
let NETWORK_ID = "testnet";
let near;
let config;
let keyStore;
/*
Hard coding NFT contract and metadata. Change this if you want.
*/
let NFT_CONTRACT_ID = "example-nft.testnet";
const METADATA = {
"title": "Linkdropped Go Team NFT",
"description": "Testing Linkdrop NFT Go Team Token",
"media": "https://bafybeiftczwrtyr3k7a2k4vutd3amkwsmaqyhrdzlhvpt33dyjivufqusq.ipfs.dweb.link/goteam-gif.gif",
"media_hash": null,
"copies": 10000,
"issued_at": null,
"expires_at": null,
"starts_at": null,
"updated_at": null,
"extra": null,
"reference": null,
"reference_hash": null
};
// set up near
const initiateNear = async () => {
const CREDENTIALS_DIR = ".near-credentials";
const credentialsPath = (await path).join(homedir, CREDENTIALS_DIR);
(await path).join;
keyStore = new keyStores.UnencryptedFileSystemKeyStore(credentialsPath);
config = {
networkId: NETWORK_ID,
keyStore,
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://wallet.testnet.near.org",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://explorer.testnet.near.org",
};
near = await connect(config);
};
async function start() {
//deployed linkdrop proxy contract
await initiateNear();
if(!LINKDROP_PROXY_CONTRACT_ID || !FUNDING_ACCOUNT_ID || !LINKDROP_NEAR_AMOUNT || !SEND_MULTIPLE) {
throw "must specify proxy contract ID, funding account ID, linkdrop $NEAR amount and whether to send multiple";
}
const contractAccount = await near.account(LINKDROP_PROXY_CONTRACT_ID);
const fundingAccount = await near.account(FUNDING_ACCOUNT_ID);
console.log(`initializing contract for account ${LINKDROP_PROXY_CONTRACT_ID}`);
try {
await contractAccount.functionCall(
LINKDROP_PROXY_CONTRACT_ID,
'new',
{
linkdrop_contract: "testnet",
},
"300000000000000",
);
} catch(e) {
console.log('error initializing contract: ', e);
}
let keyPairs = [];
let pubKeys = [];
let nft_data = [];
if(SEND_MULTIPLE != "false") {
console.log("BATCH Creating keypairs");
for(var i = 0; i < 5; i++) {
console.log('i: ', i);
let keyPair = await KeyPair.fromRandom('ed25519');
keyPairs.push(keyPair);
pubKeys.push(keyPair.publicKey.toString());
nft_data.push({
nft_sender: FUNDING_ACCOUNT_ID,
nft_contract: NFT_CONTRACT_ID,
nft_token_id: keyPair.publicKey.toString()
});
}
console.log("Finished.");
} else {
let keyPair = await KeyPair.fromRandom('ed25519');
keyPairs.push(keyPair);
pubKeys.push(keyPair.publicKey.toString());
nft_data.push({
nft_sender: FUNDING_ACCOUNT_ID,
nft_contract: NFT_CONTRACT_ID,
nft_token_id: keyPair.publicKey.toString()
});
}
try {
if(SEND_MULTIPLE != "false") {
await fundingAccount.functionCall(
LINKDROP_PROXY_CONTRACT_ID,
'send_multiple',
{
public_keys: pubKeys,
balance: parseNearAmount(LINKDROP_NEAR_AMOUNT),
nft_data
},
"300000000000000",
parseNearAmount(((parseFloat(LINKDROP_NEAR_AMOUNT) + OFFSET) * pubKeys.length).toString())
);
} else {
console.log("Sending one linkdrop");
await fundingAccount.functionCall(
LINKDROP_PROXY_CONTRACT_ID,
'send',
{
public_key: pubKeys[0],
balance: parseNearAmount(LINKDROP_NEAR_AMOUNT),
nft_data: nft_data[0]
},
"300000000000000",
parseNearAmount((parseFloat(LINKDROP_NEAR_AMOUNT) + OFFSET).toString())
);
}
} catch(e) {
console.log('error initializing contract: ', e);
}
try {
for(var i = 0; i < pubKeys.length; i++) {
console.log(`minting NFT with token ID ${nft_data[i].nft_token_id} on contract ${NFT_CONTRACT_ID} with receiver: ${FUNDING_ACCOUNT_ID}`);
await fundingAccount.functionCall(
NFT_CONTRACT_ID,
'nft_mint',
{
token_id: nft_data[i].nft_token_id,
receiver_id: FUNDING_ACCOUNT_ID,
token_metadata: METADATA,
},
"300000000000000",
parseNearAmount('1')
);
console.log(`transferring NFT to linkdrop proxy contract with nft_transfer_call`);
await fundingAccount.functionCall(
NFT_CONTRACT_ID,
'nft_transfer_call',
{
token_id: nft_data[i].nft_token_id,
receiver_id: LINKDROP_PROXY_CONTRACT_ID,
msg: pubKeys[i],
},
"300000000000000",
'1'
);
}
} catch(e) {
console.log('error minting and sending NFTs: ', e);
}
for(var i = 0; i < keyPairs.length; i++) {
console.log(`https://wallet.testnet.near.org/linkdrop/${LINKDROP_PROXY_CONTRACT_ID}/${keyPairs[i].secretKey}`);
console.log("Pub Key: ", keyPairs[i].publicKey.toString());
}
}
start();