Skip to content

Commit

Permalink
Remove dep to Ganache for dist (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewAR2 committed Jul 26, 2022
1 parent dd1c711 commit 85a9dfb
Show file tree
Hide file tree
Showing 42 changed files with 231 additions and 165 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ambrosus-node-contracts",
"author": "Ambrosus",
"description": "Smart contracts used in AMB-NET",
"version": "0.0.92",
"version": "0.0.93",
"license": "MPL-2.0-no-copyleft-exception",
"repository": "git@github.com:ambrosus/ambrosus-node-contracts.git",
"main": "dist/index.js",
Expand Down
87 changes: 1 addition & 86 deletions src/utils/web3_tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,91 +11,13 @@ import Web3 from 'web3';
import config from '../../config/config';

export const DEFAULT_GAS = 6000000;
const DEFAULT_PORT = 8545;

export const {utils} = new Web3();

function isValidRPCAddress(rpc) {
return /^((?:https?)|(?:ws)):\/\//g.test(rpc);
}

function isUsingGanache(rpc) {
return rpc === 'ganache';
}

function getDefaultGanacheOptions(secretKey) {
if (!secretKey) {
throw 'Secret key not defined';
}
return {
wallet: {
defaultBalance: 1000000000,
accounts: [
{
balance: '0x1ed09bead87c0378d8e6400000000',
secretKey
},
...Array(9).fill({balance: '0x1ed09bead87c0378d8e64000000000'})
]
},
logging: {
logger: {
log: () => {} // don't do anything
}
},
chain: {
hardfork: 'istanbul',
vmErrorsOnRPCResponse: false
}
};
}

export async function createGanacheServer(secretKey) {
const Ganache = require('ganache');
const server = Ganache.server(getDefaultGanacheOptions(secretKey));
await server.listen(DEFAULT_PORT);
}

function createGanacheProvider(secretKey) {
// import in code with purpose:D
const Ganache = require('ganache');
const memdown = require('memdown');
return Ganache.provider({
...getDefaultGanacheOptions(secretKey),
db: memdown()
});
}

async function ganacheTopUpDefaultAccount(web3) {
const [firstGanacheMasterAccount] = await web3.eth.getAccounts();
await web3.eth.sendTransaction({
from: firstGanacheMasterAccount,
to: getDefaultAddress(web3),
value: web3.utils.toWei('10', 'ether'),
gas: DEFAULT_GAS
});
}

function augmentWithSnapshotMethods(web3) {
web3.eth.extend({
methods: [
{
name: 'makeSnapshot',
call: 'evm_snapshot',
params: 0,
inputFormatter: [],
outputFormatter: web3.utils.hexToNumberString
},
{
name: 'restoreSnapshot',
call: 'evm_revert',
params: 1,
inputFormatter: [web3.utils.numberToHex]
}
]
});
}

function importPrivateKey(web3, conf) {
try {
const {nodePrivateKey} = conf;
Expand All @@ -112,14 +34,7 @@ export async function createWeb3(conf = config) {
const web3 = new Web3();

const rpc = conf.web3Rpc;
const account = importPrivateKey(web3, conf);

if (isUsingGanache(rpc)) {
web3.setProvider(createGanacheProvider(account.privateKey));
await ganacheTopUpDefaultAccount(web3);
augmentWithSnapshotMethods(web3);
return web3;
}
importPrivateKey(web3, conf);

if (!isValidRPCAddress(rpc)) {
throw new Error(`The config value for the Parity RPC server is invalid: ${rpc}`);
Expand Down
5 changes: 3 additions & 2 deletions test/actions/multisig_actions_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ This Source Code Form is “Incompatible With Secondary Licenses”, as defined
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import {multisig} from '../../src/contract_jsons';
import {createWeb3, deployContract, makeSnapshot, restoreSnapshot} from '../../src/utils/web3_tools';
import {deployContract, makeSnapshot, restoreSnapshot} from '../../src/utils/web3_tools';
import {createWeb3Ganache} from '../utils/web3_tools';
import deploy from '../helpers/deploy';
import MultiplexerWrapper from '../../src/wrappers/multiplexer_wrapper';
import MultisigWrapper from '../../src/wrappers/multisig_wrapper';
Expand Down Expand Up @@ -53,7 +54,7 @@ describe('Multisig actions integration', () => {
const onboardAsApollo = async (sender, value) => roles.methods.onboardAsApollo().send({from: sender, value});

before(async () => {
web3 = await createWeb3();
web3 = await createWeb3Ganache();
[owner, otherOwner, otherAddress] = await web3.eth.getAccounts();
multisigContract = await deployContract(web3, multisig, [[owner, otherOwner], 2]);
({multiplexer, head, kycWhitelist, fees, validatorSet, blockRewards, apolloDepositStore, roles, rolesPrivilagesStore} = await deploy({
Expand Down
5 changes: 3 additions & 2 deletions test/contracts/boilerplate/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ This Source Code Form is “Incompatible With Secondary Licenses”, as defined
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinonChai from 'sinon-chai';
import {createWeb3, deployContract} from '../../../src/utils/web3_tools';
import {deployContract} from '../../../src/utils/web3_tools';
import {createWeb3Ganache} from '../../utils/web3_tools';
import chaiEmitEvents from '../../helpers/chaiEmitEvents';
import observeBalanceChange from '../../helpers/web3BalanceObserver';

Expand Down Expand Up @@ -40,7 +41,7 @@ describe('Base Contract', () => {


before(async () => {
web3 = await createWeb3();
web3 = await createWeb3Ganache();
[deployer, catalogue, storageCatalogue, migrator, fundsMigrationTarget] = await web3.eth.getAccounts();

head = await deployContract(web3, HeadJson, [deployer], {from: deployer});
Expand Down
5 changes: 3 additions & 2 deletions test/contracts/boilerplate/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ This Source Code Form is “Incompatible With Secondary Licenses”, as defined
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinonChai from 'sinon-chai';
import {createWeb3, deployContract} from '../../../src/utils/web3_tools';
import {deployContract} from '../../../src/utils/web3_tools';
import {createWeb3Ganache} from '../../utils/web3_tools';
import ContextJson from '../../../src/contracts/Context.json';

chai.use(sinonChai);
Expand All @@ -34,7 +35,7 @@ describe('Context Contract', () => {
const versionTag = async (contract) => contract.methods.versionTag().call();

before(async () => {
web3 = await createWeb3();
web3 = await createWeb3Ganache();
[deployer, trustedAddress, catalogueAddress, storageCatalogueAddress, untrustedAddress] = await web3.eth.getAccounts();
});

Expand Down
5 changes: 3 additions & 2 deletions test/contracts/boilerplate/head.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ This Source Code Form is “Incompatible With Secondary Licenses”, as defined
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinonChai from 'sinon-chai';
import {createWeb3, deployContract} from '../../../src/utils/web3_tools';
import {deployContract} from '../../../src/utils/web3_tools';
import {createWeb3Ganache} from '../../utils/web3_tools';
import HeadJson from '../../../src/contracts/Head.json';

chai.use(sinonChai);
Expand All @@ -36,7 +37,7 @@ describe('Head Contract', () => {
const transferOwnership = async (contract, sender, newOwner) => contract.methods.transferOwnership(newOwner).send({...standardOptions, from: sender});

before(async () => {
web3 = await createWeb3();
web3 = await createWeb3Ganache();
[deployAddress, ownerAddress, nonOwnerAddress, otherAddress] = await web3.eth.getAccounts();
});

Expand Down
5 changes: 3 additions & 2 deletions test/contracts/boilerplate/multiplexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ This Source Code Form is “Incompatible With Secondary Licenses”, as defined

import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import {createWeb3, makeSnapshot, restoreSnapshot} from '../../../src/utils/web3_tools';
import {makeSnapshot, restoreSnapshot} from '../../../src/utils/web3_tools';
import {createWeb3Ganache} from '../../utils/web3_tools';
import deploy from '../../helpers/deploy';
import {APOLLO, HERMES, APOLLO_DEPOSIT} from '../../../src/constants';

Expand Down Expand Up @@ -55,7 +56,7 @@ describe('Multiplexer', () => {
const baseReward = '2000000000000000000';

before(async () => {
web3 = await createWeb3();
web3 = await createWeb3Ganache();
[oldOwner, newOwner, otherAddress, superUser, apollo, developer, support] = await web3.eth.getAccounts();
({head, multiplexer, kycWhitelist, fees, roles, validatorProxy, validatorSet, blockRewards, apolloDepositStore} = await deploy({
web3,
Expand Down
5 changes: 3 additions & 2 deletions test/contracts/configuration/fees.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import deploy from '../../helpers/deploy';
import {DAY, TWO} from '../../helpers/consts';
import TimeMockJson from '../../../src/contracts/TimeMock.json';
import BN from 'bn.js';
import {makeSnapshot, restoreSnapshot, createWeb3, utils} from '../../../src/utils/web3_tools';
import {makeSnapshot, restoreSnapshot, utils} from '../../../src/utils/web3_tools';
import {createWeb3Ganache} from '../../utils/web3_tools';

const {expect} = chai;

Expand Down Expand Up @@ -55,7 +56,7 @@ describe('Fees Contract', () => {
const developerFee = (fee) => fee.sub(nonDeveloperFee(fee));

before(async () => {
web3 = await createWeb3();
web3 = await createWeb3Ganache();
[from, other, developer, support] = await web3.eth.getAccounts();
({fees, time} = await deploy({
web3,
Expand Down
5 changes: 3 additions & 2 deletions test/contracts/configuration/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinonChai from 'sinon-chai';
import deploy from '../../helpers/deploy';
import {createWeb3, makeSnapshot, restoreSnapshot} from '../../../src/utils/web3_tools';
import {makeSnapshot, restoreSnapshot} from '../../../src/utils/web3_tools';
import {createWeb3Ganache} from '../../utils/web3_tools';

chai.use(sinonChai);
chai.use(chaiAsPromised);
Expand All @@ -33,7 +34,7 @@ describe('Time Contract', () => {
const payoutPeriodOffset = (timestamp, senderAddress = validUser) => time.methods.payoutPeriodOffset(timestamp).call({from: senderAddress});

before(async () => {
web3 = await createWeb3();
web3 = await createWeb3Ganache();
[validUser] = await web3.eth.getAccounts();
({time} = await deploy({
web3,
Expand Down
5 changes: 3 additions & 2 deletions test/contracts/consensus/block_rewards.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ This Source Code Form is “Incompatible With Secondary Licenses”, as defined

import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import {createWeb3, deployContract, makeSnapshot, restoreSnapshot} from '../../../src/utils/web3_tools';
import {deployContract, makeSnapshot, restoreSnapshot} from '../../../src/utils/web3_tools';
import {createWeb3Ganache} from '../../utils/web3_tools';
import chaiEmitEvents from '../../helpers/chaiEmitEvents';
import BlockRewardsJson from '../../../src/contracts/BlockRewards.json';

Expand Down Expand Up @@ -56,7 +57,7 @@ describe('Block rewards contract', () => {
let snapshotId;

before(async () => {
web3 = await createWeb3();
web3 = await createWeb3Ganache();
[deployer, owner, otherUser, superUser, newOwner] = await web3.eth.getAccounts();
exampleBeneficiaries = Array(3)
.fill(null)
Expand Down
5 changes: 3 additions & 2 deletions test/contracts/consensus/constructor_ownable.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ This Source Code Form is “Incompatible With Secondary Licenses”, as defined

import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import {createWeb3, deployContract, makeSnapshot, restoreSnapshot} from '../../../src/utils/web3_tools';
import {deployContract, makeSnapshot, restoreSnapshot} from '../../../src/utils/web3_tools';
import {createWeb3Ganache} from '../../utils/web3_tools';
import chaiEmitEvents from '../../helpers/chaiEmitEvents';
import ConstructorOwnableJson from '../../../src/contracts/ConstructorOwnable.json';

Expand All @@ -35,7 +36,7 @@ describe('ConstructorOwnable base contract', () => {
let snapshotId;

before(async () => {
web3 = await createWeb3();
web3 = await createWeb3Ganache();
[deployer, owner, otherUser, newOwner] = await web3.eth.getAccounts();
contract = await deploy(web3, deployer, owner);
});
Expand Down
5 changes: 3 additions & 2 deletions test/contracts/consensus/validator_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ This Source Code Form is “Incompatible With Secondary Licenses”, as defined

import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import {createWeb3, deployContract, makeSnapshot, restoreSnapshot} from '../../../src/utils/web3_tools';
import {deployContract, makeSnapshot, restoreSnapshot} from '../../../src/utils/web3_tools';
import {createWeb3Ganache} from '../../utils/web3_tools';
import chaiEmitEvents from '../../helpers/chaiEmitEvents';
import ValidatorSetJson from '../../../src/contracts/ValidatorSet.json';

Expand Down Expand Up @@ -41,7 +42,7 @@ describe('Validator set contract', () => {
let additionalValidator;

before(async () => {
web3 = await createWeb3();
web3 = await createWeb3Ganache();
[deployer, owner, otherUser, superUser, newOwner] = await web3.eth.getAccounts();
exampleValidatorAddresses = Array(3)
.fill(null)
Expand Down
5 changes: 3 additions & 2 deletions test/contracts/front/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ This Source Code Form is “Incompatible With Secondary Licenses”, as defined
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinonChai from 'sinon-chai';
import {createWeb3, deployContract, makeSnapshot, restoreSnapshot, utils} from '../../../src/utils/web3_tools';
import {deployContract, makeSnapshot, restoreSnapshot, utils} from '../../../src/utils/web3_tools';
import {createWeb3Ganache} from '../../utils/web3_tools';
import chaiEmitEvents from '../../helpers/chaiEmitEvents';
import BN from 'bn.js';
import {
Expand Down Expand Up @@ -126,7 +127,7 @@ describe('Challenges Contract', () => {
// eslint-disable-next-line new-cap

before(async () => {
web3 = await createWeb3();
web3 = await createWeb3Ganache();
[context, challenger, uploader, atlases[0], atlases[1], atlases[2], atlases[3], atlases[4], shelterer, totalStranger] = await web3.eth.getAccounts();
({challenges, challengesStore, bundleStore, fees, sheltering, kycWhitelist, atlasStakeStore, time, roles, challengesEventEmitter} = await deploy({
web3,
Expand Down
5 changes: 3 additions & 2 deletions test/contracts/front/kyc_whitelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ This Source Code Form is “Incompatible With Secondary Licenses”, as defined
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinonChai from 'sinon-chai';
import {createWeb3, makeSnapshot, restoreSnapshot} from '../../../src/utils/web3_tools';
import {makeSnapshot, restoreSnapshot} from '../../../src/utils/web3_tools';
import {createWeb3Ganache} from '../../utils/web3_tools';
import deploy from '../../helpers/deploy';
import {APOLLO, ATLAS, HERMES, APOLLO_DEPOSIT, ATLAS1_STAKE, ATLAS2_STAKE, ATLAS3_STAKE} from '../../../src/constants';
import {ONE} from '../../helpers/consts';
Expand Down Expand Up @@ -42,7 +43,7 @@ describe('KYC Whitelist Contract', () => {
const getRoleAssigned = async (address) => kycWhitelist.methods.getRoleAssigned(address).call();

before(async () => {
web3 = await createWeb3();
web3 = await createWeb3Ganache();
[from, other, totalStranger] = await web3.eth.getAccounts();
({kycWhitelist} = await deploy({
web3,
Expand Down
5 changes: 3 additions & 2 deletions test/contracts/front/payouts.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import sinonChai from 'sinon-chai';
import deploy from '../../helpers/deploy';
import TimeMockJson from '../../../src/contracts/TimeMock.json';
import observeBalanceChange from '../../helpers/web3BalanceObserver';
import {createWeb3, makeSnapshot, restoreSnapshot} from '../../../src/utils/web3_tools';
import {makeSnapshot, restoreSnapshot} from '../../../src/utils/web3_tools';
import {createWeb3Ganache} from '../../utils/web3_tools';
import {PAYOUT_PERIOD_UNIT} from '../../helpers/consts';

chai.use(sinonChai);
Expand Down Expand Up @@ -46,7 +47,7 @@ describe('Payouts Contract', () => {
const expectBalanceChange = async (account, amount, codeBlock) => expect((await observeBalanceChange(web3, account, codeBlock)).toString()).to.eq(amount);

before(async () => {
web3 = await createWeb3();
web3 = await createWeb3Ganache();
[validUser, beneficiary, withdrawTarget, otherUser] = await web3.eth.getAccounts();
({payoutsStore, payouts, time} = await deploy({
web3,
Expand Down
5 changes: 3 additions & 2 deletions test/contracts/front/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ This Source Code Form is “Incompatible With Secondary Licenses”, as defined
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinonChai from 'sinon-chai';
import {createWeb3, makeSnapshot, restoreSnapshot} from '../../../src/utils/web3_tools';
import {makeSnapshot, restoreSnapshot} from '../../../src/utils/web3_tools';
import {createWeb3Ganache} from '../../utils/web3_tools';
import deploy from '../../helpers/deploy';
import {ONE} from '../../helpers/consts';
import {
Expand Down Expand Up @@ -80,7 +81,7 @@ describe('Roles Contract', () => {
let snapshotId;

before(async () => {
web3 = await createWeb3();
web3 = await createWeb3Ganache();
[owner, apollo, apolloStake, initialApollo, atlas1, atlas2, atlas3, atlasStake, hermes] = await web3.eth.getAccounts();
({roles, kycWhitelist, atlasStakeStore, apolloDepositStore, validatorSet, blockRewards, rolesEventEmitter} = await deploy({
web3,
Expand Down
Loading

0 comments on commit 85a9dfb

Please sign in to comment.