forked from keypom/keypom
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ft.js
154 lines (136 loc) · 4.25 KB
/
ft.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
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 FT_CONTRACT_ID = process.env.FT_CONTRACT_ID;
let SEND_MULTIPLE = process.env.SEND_MULTIPLE;
let OFFSET = 2;
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());
}
console.log(`sending ${LINKDROP_NEAR_AMOUNT} $NEAR as ${FUNDING_ACCOUNT_ID}`);
try {
let ft_data = {};
ft_data["ft_contract"] = FT_CONTRACT_ID;
ft_data["ft_sender"] = FUNDING_ACCOUNT_ID;
ft_data["ft_balance"] = "25";
if(SEND_MULTIPLE != "false") {
await fundingAccount.functionCall(
LINKDROP_PROXY_CONTRACT_ID,
'send_multiple',
{
public_keys: pubKeys,
balance: parseNearAmount(LINKDROP_NEAR_AMOUNT),
ft_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),
ft_data
},
"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(`Paying for FT storage on contract: ${FT_CONTRACT_ID} for the proxy contract ID`);
await fundingAccount.functionCall(
FT_CONTRACT_ID,
'storage_deposit',
{
account_id: LINKDROP_PROXY_CONTRACT_ID,
},
"300000000000000",
parseNearAmount('1')
);
console.log(`Transferring 25 FTs from ${FUNDING_ACCOUNT_ID} to ${LINKDROP_PROXY_CONTRACT_ID}`);
await fundingAccount.functionCall(
FT_CONTRACT_ID,
'ft_transfer_call',
{
receiver_id: LINKDROP_PROXY_CONTRACT_ID,
amount: "25",
msg: pubKeys[i],
},
"300000000000000",
'1'
);
}
} catch(e) {
console.log('error sending FTs: ', 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();