-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmoonlight.ts
142 lines (124 loc) · 4.45 KB
/
moonlight.ts
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
import Web3 from 'web3';
import { TransactionReceipt } from 'web3-core';
import { Contract } from 'web3-eth-contract';
import { ERC1155_ABI_INTERFACE, ERC721_ABI_INTERFACE } from './abi';
import { AIRDROP_TARGETS, ERC1155_CONTRACT, ERC721_CONTRACT, PRIVATE_KEY, URIS, WEB3_ENDPOINT } from './config';
enum Standard {
ERC721 = 'erc721',
ERC1155 = 'erc1155'
}
async function abort(web3: Web3) {
return await web3.eth.sendTransaction({
from: web3.defaultAccount as string,
to: web3.defaultAccount as string,
gasPrice: Number(await web3.eth.getGasPrice) * 1.12
});
}
/**
* Mint NFT from sky to everyone on the earth.
*/
class MoonLight721 {
web3: Web3;
contract: Contract;
constructor(endpoint: string, contract: string, privateKey: string) {
this.web3 = new Web3(endpoint);
this.web3.eth.accounts.wallet.add(privateKey);
this.web3.eth.defaultAccount = this.web3.eth.accounts.privateKeyToAccount(privateKey).address;
this.contract = new this.web3.eth.Contract(ERC721_ABI_INTERFACE, contract);
}
async nonce(): Promise<number> {
return await this.web3.eth.getTransactionCount(this.web3.defaultAccount as string, 'latest');
};
/**
* Mint NFT(s) to an address
* @param to Target address that receives the NFT
* @param uri NFT Uri
* @param amount Amount to mint
*/
async mint(to: string, uri: string, amount: number): Promise<TransactionReceipt> {
return await this.contract.methods.mint(to, uri, amount).send({
nonce: await this.nonce(),
from: this.web3.defaultAccount,
gasPrice: await this.web3.eth.getGasPrice()
});
};
/**
* Mint multiple NFTs to a list of addresses
* @param targets Target list of addresses
* @param uris Uri list
*/
async airdrop(targets: string[], uris: string[]): Promise<TransactionReceipt> {
return await this.contract.methods.mintMulti(targets, uris).send({
from: this.web3.defaultAccount,
})
};
/**
* Abort last transaction with nonce by a new tx with higher gas price.
* @param nonce Last tx nonce
*/
async abort(): Promise<TransactionReceipt> {
return await this.web3.eth.sendTransaction({
from: this.web3.defaultAccount as string,
to: this.web3.defaultAccount as string,
gasPrice: Number(await this.web3.eth.getGasPrice) * 1.12
});
};
}
// Mint erc1155-based NFT and airdrops
class MoonLight1155 {
web3: Web3;
contract: Contract;
constructor(endpoint: string, contract: string, privateKey: string) {
this.web3 = new Web3(endpoint);
this.web3.eth.accounts.wallet.add(privateKey);
this.web3.eth.defaultAccount = this.web3.eth.accounts.privateKeyToAccount(privateKey).address;
this.contract = new this.web3.eth.Contract(ERC1155_ABI_INTERFACE, contract);
}
async nonce(): Promise<number> {
return await this.web3.eth.getTransactionCount(this.web3.defaultAccount as string, 'latest');
};
async setCreator(to: string, ids: number[]): Promise<TransactionReceipt> {
return await this.contract.methods.setCreator(to, ids).send({
nonce: await this.nonce(),
from: this.web3.defaultAccount,
gasPrice: await this.web3.eth.getGasPrice()
})
}
async mint(to: string, id: number, amount: number): Promise<TransactionReceipt> {
return await this.contract.methods.mint(to, id, amount, "").send({
nonce: await this.nonce(),
from: this.web3.defaultAccount,
gasPrice: await this.web3.eth.getGasPrice()
})
}
async airdrop(targets: string[], id: number, amount: number): Promise<TransactionReceipt> {
return await this.contract.methods.airdrop(targets, id, amount).send({
nonce: await this.nonce(),
from: this.web3.defaultAccount,
gasPrice: await this.web3.eth.getGasPrice()
})
}
async setURI(uri: string, id?: number): Promise<TransactionReceipt> {
if (!id) {
return await this.contract.methods.setURI(uri).send({
nonce: await this.nonce(),
from: this.web3.defaultAccount,
gasPrice: await this.web3.eth.getGasPrice()
})
} else {
return await this.contract.methods.setCustomURI(id, uri).send({
nonce: await this.nonce(),
from: this.web3.defaultAccount,
gasPrice: await this.web3.eth.getGasPrice()
})
}
}
async abort(): Promise<TransactionReceipt> {
return abort(this.web3);
}
}
async function run() {
const moonlight = new MoonLight1155(WEB3_ENDPOINT, ERC1155_CONTRACT, PRIVATE_KEY);
// await moonlight.airdrop(AIRDROP_TARGETS, URIS);
}
run();