Skip to content

Commit 7458801

Browse files
committed
update unit tests for Loom caveats
1 parent 93560be commit 7458801

6 files changed

Lines changed: 114 additions & 87 deletions

File tree

jest.testOrder.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import { CloseConnection as CloseRedis } from "./src/redis";
2525
import { loadContractFixtures } from "./rpc/contracts";
2626
import { cleanupDeployedContracts } from "./rpc/__tests__/utils";
2727

28+
import { EncryptorContainer } from "./src/entity/encryption/EncryptorContainer";
29+
import { Wallet } from "./src/entity/Wallet";
30+
2831

2932
// RPC Tests
3033
// =========
@@ -84,6 +87,9 @@ const CONTRACT_METADATA_PATH = '/shipchain-contracts/meta.json';
8487
const STATIC_TEST_METADATA_FILE = '/app/src/__tests__/meta.json';
8588

8689

90+
jest.setTimeout(20000);
91+
92+
8793
describe('RPC', async () => {
8894

8995
beforeAll(async () => {
@@ -94,6 +100,11 @@ describe('RPC', async () => {
94100
// read connection options from ormconfig file (or ENV variables)
95101
const connectionOptions = await typeorm.getConnectionOptions();
96102
await typeorm.createConnection(connectionOptions);
103+
await EncryptorContainer.init();
104+
const rootWallet = await Wallet.import_entity('0x0000000000000000000000000000000000000000000000000000000000000001');
105+
await typeorm
106+
.getConnection()
107+
.getRepository(Wallet).insert(rootWallet);
97108
await loadContractFixtures();
98109
if (!staticTestMetadataNock.isDone()) {
99110
console.error(`Failed to load static metadata from ${STATIC_TEST_METADATA_FILE}`);
@@ -102,7 +113,7 @@ describe('RPC', async () => {
102113
} catch(err){
103114
console.error(`beforeAll Error ${err}`);
104115
}
105-
}, 10000);
116+
}, 90000);
106117

107118
afterAll(async() => {
108119
try {

rpc/__tests__/transaction.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { RPCTransaction } from '../transaction';
3232
import { RPCLoad } from '../Load/1.2.0/RPCLoad';
3333
import { Wallet } from "../../src/entity/Wallet";
3434
import { EncryptorContainer } from '../../src/entity/encryption/EncryptorContainer';
35+
import { LoomHooks } from "../../src/eth/LoomHooks";
3536

3637
export const RPCTransactions = async function() {
3738

@@ -121,15 +122,20 @@ export const RPCTransactions = async function() {
121122
});
122123

123124
const txResponse = new EthereumTx(response.transaction);
124-
const validTx = txResponse.validate();
125+
126+
// Validate checks gasLimit which is always 0 in a Loom environment
127+
let validTx = true;
128+
if (!LoomHooks.enabled) {
129+
validTx = txResponse.validate();
130+
}
125131

126132
expect(response).toBeDefined();
127133
expect(response.success).toBeTruthy();
128134
expect(response.hash).toMatch(/^0x[a-f0-9]{64}$/);
129135
expect(validTx).toBeTruthy();
130136
txSigned = txResponse;
131137
} catch (err){
132-
fail(`Should not have thrown [${err}]`);
138+
fail(`Should not have thrown [${err.message}]`);
133139
}
134140
}));
135141
});

rpc/__tests__/wallet.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ export const RPCWalletTests = async function() {
165165
wallet: imported.wallet.id,
166166
});
167167
expect(response.success).toBeTruthy();
168-
expect(response.ether).toEqual('158456325028527356587087900672');
169-
expect(response.ship).toEqual('0');
168+
expect(response.ether).toEqual('0');
169+
expect(response.ship).toEqual('500000000000000000000');
170170
} catch (err) {
171171
fail(`Should not have thrown [${err}]`);
172172
}

src/__tests__/contracts.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { Wallet } from '../entity/Wallet';
2121
import { Contract, Version, Project, Network } from '../entity/Contract';
2222
import { EncryptorContainer } from '../entity/encryption/EncryptorContainer';
2323
import { AbstractEthereumService } from "../eth/AbstractEthereumService";
24+
import { LoomHooks } from "../eth/LoomHooks";
2425

2526
const utils = require('../local-test-net-utils');
2627

@@ -55,13 +56,16 @@ export const ContractEntityTests = async function() {
5556
const network: Network = await Network.getLocalTestNet();
5657
const ethereumService: AbstractEthereumService = network.getEthereumService();
5758

58-
expect(await local.ShipToken.call_static('balanceOf', [owner.address])).toEqual(ethereumService.unitToWei(500, 'ether'));
59+
const ownerBalance = await local.ShipToken.call_static('balanceOf', [await owner.asyncEvmAddress]);
60+
expect(ownerBalance).toEqual(ethereumService.unitToWei(500, 'ether'));
5961

60-
expect(await ethereumService.getBalance(owner.address)).toEqual(ethereumService.unitToWei(5, 'ether').toString());
62+
if (!LoomHooks.enabled) {
63+
expect(await ethereumService.getBalance(await owner.asyncEvmAddress)).toEqual(ethereumService.unitToWei(5, 'ether').toString());
64+
}
6165

6266
const txParams = await owner.add_tx_params(
6367
network,
64-
await local.ShipToken.build_transaction('transfer', [other.address, ethereumService.unitToWei(100, 'ether').toString()]),
68+
await local.ShipToken.build_transaction('transfer', [await other.asyncEvmAddress, ethereumService.unitToWei(100, 'ether').toString()]),
6569
);
6670

6771
const [signed_tx, txHash] = await owner.sign_tx(txParams);
@@ -70,13 +74,13 @@ export const ContractEntityTests = async function() {
7074

7175
expect(receipt.transactionHash.length).toEqual(66);
7276

73-
const new_owner_balance = await local.ShipToken.call_static('balanceOf', [owner.address]);
74-
const new_other_balance = await local.ShipToken.call_static('balanceOf', [other.address]);
77+
const new_owner_balance = await local.ShipToken.call_static('balanceOf', [await owner.asyncEvmAddress]);
78+
const new_other_balance = await local.ShipToken.call_static('balanceOf', [await other.asyncEvmAddress]);
7579

7680
expect(new_owner_balance).toEqual(ethereumService.unitToWei(400, 'ether'));
7781

7882
expect(new_other_balance).toEqual(ethereumService.unitToWei(100, 'ether'));
7983
},
80-
10000,
84+
30000,
8185
);
8286
};

src/__tests__/eventsubscriptions.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ export const EventSubscriptionEntityTests = async function() {
9898
const ETH = 10 ** 18;
9999
const TOTAL = 500 * SHIP;
100100

101-
expect(Number(await local.ShipToken.call_static('balanceOf', [owner.address]))).toEqual(TOTAL);
101+
expect(Number(await local.ShipToken.call_static('balanceOf', [await owner.asyncEvmAddress]))).toEqual(TOTAL);
102102

103-
expect(Number(await ethereumService.getBalance(owner.address))).toEqual(5 * ETH);
103+
expect(Number(await ethereumService.getBalance(await owner.asyncEvmAddress))).toEqual(5 * ETH);
104104

105105
const txParams = await owner.add_tx_params(
106106
network,
107-
await local.ShipToken.build_transaction('transfer', [other.address, 100 * SHIP]),
107+
await local.ShipToken.build_transaction('transfer', [await other.asyncEvmAddress, 100 * SHIP]),
108108
);
109109

110110
const [signed_tx, txHash] = await owner.sign_tx(txParams);
@@ -113,8 +113,8 @@ export const EventSubscriptionEntityTests = async function() {
113113

114114
expect(receipt.transactionHash.length).toEqual(66);
115115

116-
const new_owner_balance = await local.ShipToken.call_static('balanceOf', [owner.address]);
117-
const new_other_balance = await local.ShipToken.call_static('balanceOf', [other.address]);
116+
const new_owner_balance = await local.ShipToken.call_static('balanceOf', [await owner.asyncEvmAddress]);
117+
const new_other_balance = await local.ShipToken.call_static('balanceOf', [await other.asyncEvmAddress]);
118118

119119
expect(Number(new_owner_balance)).toEqual(TOTAL - 100 * SHIP);
120120

src/__tests__/gaspriceoracle.ts

Lines changed: 77 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import 'mocha';
2020
const nock = require('nock');
2121
import { GasPriceOracle } from '../GasPriceOracle';
2222
import { AbstractEthereumService } from "../eth/AbstractEthereumService";
23+
import { LoomHooks } from "../eth/LoomHooks";
2324

2425
export const GasPriceOracleTests = async function() {
2526

@@ -32,74 +33,79 @@ export const GasPriceOracleTests = async function() {
3233
expect(gpo.gasPrice).toEqual(ethereumService.unitToWei(20, 'gwei'));
3334
});
3435

35-
it(`can get price from Provider in gwei`, async () => {
36-
const gpo: GasPriceOracle = GasPriceOracle.Instance;
37-
38-
// @ts-ignore
39-
let providerPrice = await gpo.getProviderOracleGasPrice();
40-
41-
// geth-poa image always generates 18 gwei
42-
expect(providerPrice).toEqual(18);
43-
});
44-
45-
it(`can get price from EthGasStation`, async () => {
46-
const gpo: GasPriceOracle = GasPriceOracle.Instance;
47-
48-
nock('https://ethgasstation.info')
49-
.get('/json/ethgasAPI.json')
50-
.times(3)
51-
.reply(200, {
52-
fast: 500,
53-
fastest: 600,
54-
average: 400,
55-
safeLow: 300,
56-
safeLowWait: 2.0,
57-
avgWait: 1.5,
58-
fastWait: 1.0,
59-
fastestWait: 0.5
60-
});
61-
62-
// @ts-ignore
63-
let ethGasStationPrice = await gpo.getEthGasStationBestPrice();
64-
65-
// Based on DESIRED_WAIT_TIME of 2 minutes, this should pick safeLow
66-
expect(ethGasStationPrice.price).toEqual(30);
67-
});
68-
69-
it(`can get default price from EthGasStation if no desired time match`, async () => {
70-
const gpo: GasPriceOracle = GasPriceOracle.Instance;
71-
72-
nock('https://ethgasstation.info')
73-
.get('/json/ethgasAPI.json')
74-
.times(3)
75-
.reply(200, {
76-
fast: 500,
77-
fastest: 600,
78-
average: 400,
79-
safeLow: 300,
80-
safeLowWait: 10.0,
81-
avgWait: 10.0,
82-
fastWait: 10.0,
83-
fastestWait: 10.0
84-
});
85-
86-
// @ts-ignore
87-
let ethGasStationPrice = await gpo.getEthGasStationBestPrice();
88-
89-
// Based on DESIRED_WAIT_TIME of 2 minutes, this should default to fastest
90-
expect(ethGasStationPrice.price).toEqual(60);
91-
});
92-
93-
it(`returns only Provider price when not in PROD`, async () => {
94-
const gpo: GasPriceOracle = GasPriceOracle.Instance;
95-
96-
// @ts-ignore
97-
await gpo.calculateGasPrice();
98-
99-
// @ts-ignore
100-
const ethereumService: AbstractEthereumService = gpo.ethereumService;
101-
102-
expect(gpo.gasPrice).toEqual(ethereumService.unitToWei(18, 'gwei'));
103-
});
104-
105-
};
36+
if (LoomHooks.enabled) {
37+
console.log('\n\nSKIPPING - GasPriceOracleTests test because Loom does not have gas\n');
38+
} else {
39+
40+
it(`can get price from Provider in gwei`, async () => {
41+
const gpo: GasPriceOracle = GasPriceOracle.Instance;
42+
43+
// @ts-ignore
44+
let providerPrice = await gpo.getProviderOracleGasPrice();
45+
46+
// geth-poa image always generates 18 gwei
47+
expect(providerPrice).toEqual(18);
48+
});
49+
50+
it(`can get price from EthGasStation`, async () => {
51+
const gpo: GasPriceOracle = GasPriceOracle.Instance;
52+
53+
nock('https://ethgasstation.info')
54+
.get('/json/ethgasAPI.json')
55+
.times(3)
56+
.reply(200, {
57+
fast: 500,
58+
fastest: 600,
59+
average: 400,
60+
safeLow: 300,
61+
safeLowWait: 2.0,
62+
avgWait: 1.5,
63+
fastWait: 1.0,
64+
fastestWait: 0.5
65+
});
66+
67+
// @ts-ignore
68+
let ethGasStationPrice = await gpo.getEthGasStationBestPrice();
69+
70+
// Based on DESIRED_WAIT_TIME of 2 minutes, this should pick safeLow
71+
expect(ethGasStationPrice.price).toEqual(30);
72+
});
73+
74+
it(`can get default price from EthGasStation if no desired time match`, async () => {
75+
const gpo: GasPriceOracle = GasPriceOracle.Instance;
76+
77+
nock('https://ethgasstation.info')
78+
.get('/json/ethgasAPI.json')
79+
.times(3)
80+
.reply(200, {
81+
fast: 500,
82+
fastest: 600,
83+
average: 400,
84+
safeLow: 300,
85+
safeLowWait: 10.0,
86+
avgWait: 10.0,
87+
fastWait: 10.0,
88+
fastestWait: 10.0
89+
});
90+
91+
// @ts-ignore
92+
let ethGasStationPrice = await gpo.getEthGasStationBestPrice();
93+
94+
// Based on DESIRED_WAIT_TIME of 2 minutes, this should default to fastest
95+
expect(ethGasStationPrice.price).toEqual(60);
96+
});
97+
98+
it(`returns only Provider price when not in PROD`, async () => {
99+
const gpo: GasPriceOracle = GasPriceOracle.Instance;
100+
101+
// @ts-ignore
102+
await gpo.calculateGasPrice();
103+
104+
// @ts-ignore
105+
const ethereumService: AbstractEthereumService = gpo.ethereumService;
106+
107+
expect(gpo.gasPrice).toEqual(ethereumService.unitToWei(18, 'gwei'));
108+
});
109+
}
110+
111+
};

0 commit comments

Comments
 (0)