forked from keypom/keypom
-
Notifications
You must be signed in to change notification settings - Fork 9
/
simple.js
118 lines (100 loc) · 3.26 KB
/
simple.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
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 = 0.1;
let NETWORK_ID = "testnet";
let near;
let config;
let keyStore;
// 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 = [];
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());
}
console.log("Finished.");
} else {
let keyPair = await KeyPair.fromRandom('ed25519');
keyPairs.push(keyPair);
pubKeys.push(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)
},
"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)
},
"300000000000000",
parseNearAmount((parseFloat(LINKDROP_NEAR_AMOUNT) + OFFSET).toString())
);
}
} catch(e) {
console.log('error initializing contract: ', 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();