2023-01-24 15:10:36 +01:00
|
|
|
const { Web3Provider } = require('@ethersproject/providers');
|
|
|
|
const { ContractFactory } = require('@ethersproject/contracts');
|
|
|
|
|
2022-08-09 15:59:20 +02:00
|
|
|
const { SMART_CONTRACTS, contractConfiguration } = require('./smart-contracts');
|
2022-07-21 19:08:01 +02:00
|
|
|
const GanacheContractAddressRegistry = require('./ganache-contract-address-registry');
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ganache seeder is used to seed initial smart contract or set initial blockchain state.
|
|
|
|
*/
|
|
|
|
class GanacheSeeder {
|
2022-08-09 15:59:20 +02:00
|
|
|
constructor(ganacheProvider) {
|
2022-07-21 19:08:01 +02:00
|
|
|
this.smartContractRegistry = new GanacheContractAddressRegistry();
|
2022-08-09 15:59:20 +02:00
|
|
|
this.ganacheProvider = ganacheProvider;
|
2022-07-21 19:08:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deploy initial smart contracts that can be used later within the e2e tests.
|
|
|
|
*
|
|
|
|
* @param contractName
|
|
|
|
*/
|
|
|
|
|
|
|
|
async deploySmartContract(contractName) {
|
2023-01-24 15:10:36 +01:00
|
|
|
const ethersProvider = new Web3Provider(this.ganacheProvider, 'any');
|
2022-08-09 15:59:20 +02:00
|
|
|
const signer = ethersProvider.getSigner();
|
|
|
|
const fromAddress = await signer.getAddress();
|
2023-01-24 15:10:36 +01:00
|
|
|
const contractFactory = new ContractFactory(
|
2022-07-21 19:08:01 +02:00
|
|
|
contractConfiguration[contractName].abi,
|
|
|
|
contractConfiguration[contractName].bytecode,
|
2022-08-09 15:59:20 +02:00
|
|
|
signer,
|
2022-07-21 19:08:01 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
let contract;
|
|
|
|
|
2022-08-09 15:59:20 +02:00
|
|
|
if (contractName === SMART_CONTRACTS.HST) {
|
2022-07-21 19:08:01 +02:00
|
|
|
contract = await contractFactory.deploy(
|
2022-08-09 15:59:20 +02:00
|
|
|
contractConfiguration[SMART_CONTRACTS.HST].initialAmount,
|
|
|
|
contractConfiguration[SMART_CONTRACTS.HST].tokenName,
|
|
|
|
contractConfiguration[SMART_CONTRACTS.HST].decimalUnits,
|
|
|
|
contractConfiguration[SMART_CONTRACTS.HST].tokenSymbol,
|
2022-07-21 19:08:01 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
contract = await contractFactory.deploy();
|
|
|
|
}
|
|
|
|
|
|
|
|
await contract.deployTransaction.wait();
|
|
|
|
|
2022-08-09 15:59:20 +02:00
|
|
|
if (contractName === SMART_CONTRACTS.COLLECTIBLES) {
|
|
|
|
const transaction = await contract.mintCollectibles(1, {
|
|
|
|
from: fromAddress,
|
|
|
|
});
|
|
|
|
await transaction.wait();
|
2022-07-21 19:08:01 +02:00
|
|
|
}
|
|
|
|
this.storeSmartContractAddress(contractName, contract.address);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store deployed smart contract address within the environment variables
|
|
|
|
* to make it available everywhere.
|
|
|
|
*
|
|
|
|
* @param contractName
|
|
|
|
* @param contractAddress
|
|
|
|
*/
|
|
|
|
storeSmartContractAddress(contractName, contractAddress) {
|
|
|
|
this.smartContractRegistry.storeNewContractAddress(
|
|
|
|
contractName,
|
|
|
|
contractAddress,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the currently used smart contract registry.
|
|
|
|
*
|
|
|
|
* @returns GanacheContractAddressRegistry
|
|
|
|
*/
|
|
|
|
getContractRegistry() {
|
|
|
|
return this.smartContractRegistry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = GanacheSeeder;
|