-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchainInterface.mjs
55 lines (40 loc) · 1.59 KB
/
blockchainInterface.mjs
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
import fs from 'fs'
import { ethers } from 'ethers'
// If you create a project on Infura, change below to your project ID
// You can also change the network identifier between testnest <-> mainnet
export function getProviderInfura() {
const provider = new ethers.InfuraProvider("homestead", 'fc0ce324e349457ba615cbaba69ef5a1')
return provider
}
export function getProvider(location) {
const provider = new ethers.JsonRpcProvider(location)
return provider
}
export function getSignerEmbedded(provider, index) {
return provider.getSigner(index)
}
export function getSigner(provider, mnemonic) {
return new ethers.Wallet.fromMnemonic(mnemonic)
}
export function readJSON(jsonFilename) {
var jsonContents = fs.readFileSync(jsonFilename, 'utf-8')
return JSON.parse(jsonContents)
}
export function getContractFactory(jsonFilename, signer) {
var jsonContents = fs.readFileSync(jsonFilename, 'utf-8')
return ethers.ContractFactory.fromSolidity(jsonContents, signer)
}
// ABI can be obtained from compiled JSON or from contractABI.mjs in human-readable form
// Bytecode can be obtained from compiled JSON
export function getContractFactory2(contractAbi, contractBytecode, signer) {
return new ethers.ContractFactory(contractAbi, contractBytecode, signer)
}
export async function deployContract(contractFactory, ...args) {
var contract = await contractFactory.deploy(...args)
// Wait until the contract is mined
await contract.deployTransaction.wait()
return contract
}
export function getContract(provider, address, contractInterface) {
return new ethers.Contract(address, contractInterface, provider)
}