-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathbase-model.spec.ts
69 lines (56 loc) · 2.93 KB
/
base-model.spec.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
import {Web3ConnectionOptions} from '@interfaces/web3-connection-options';
import {Web3Connection} from '@base/web3-connection';
import {Model} from '@base/model';
import {expect} from 'chai';
import {Errors} from '@interfaces/error-enum';
import {getPrivateKeyFromFile, hasTxBlockNumber, shouldBeRejected} from '../utils/';
import erc20 from "../../build/contracts/ERC20.json";
import {ERC20} from "../../src";
describe(`Model<any>`, () => {
let deployedAddress: string;
const options: Web3ConnectionOptions = {
web3Host: process.env.WEB3_HOST_PROVIDER || 'HTTP://127.0.0.1:8545',
privateKey: process.env.WALLET_PRIVATE_KEY || getPrivateKeyFromFile(),
skipWindowAssignment: true,
autoStart: false,
}
it(`throws because no Abi`, () => {
const web3Connection = new Web3Connection(options);
expect(() => new Model(web3Connection, []))
.to.throw(Errors.MissingAbiInterfaceFromArguments);
expect(() => new Model(web3Connection, undefined as any))
.to.throw(Errors.MissingAbiInterfaceFromArguments);
});
it(`Does not load abi if no autoStart`, () => {
const web3Connection = new Web3Connection(options);
const model = new Model(web3Connection, erc20.abi as any);
expect(model.contract).to.be.undefined;
});
describe(`with autoStart: true`, () => {
it(`Starts and loads the ABI automatically and re-assigns`, async () => {
const web3Connection = new Web3Connection({...options, autoStart: true});
const model = new Model(web3Connection, erc20.abi as any);
const tx =
await hasTxBlockNumber(model.deploy({data: erc20.bytecode, arguments: ["name", "symbol"]}, web3Connection.Account));
expect(model.contract.abi).to.exist;
expect(tx.blockNumber).to.exist;
expect(tx.contractAddress).to.exist;
expect(model.contractAddress).to.be.eq(tx.contractAddress);
deployedAddress = tx.contractAddress;
});
it(`Starts but can't interact, only read because no pvtkey`, async () => {
const model = new Model({...options, privateKey: undefined, autoStart: true}, erc20.abi as any, deployedAddress);
expect(await model.callTx(model.contract.methods.name())).to.be.eq('name');
const AliceAddress = model.web3.eth.accounts.privateKeyToAccount(getPrivateKeyFromFile(1)).address;
await shouldBeRejected(model.sendTx(model.contract.methods.transfer(AliceAddress, '10')))
})
it(`should await the start of a custom model after deploy`, async () => {
const model = new ERC20({...options, autoStart: true});
const BobAddress = model.web3.eth.accounts.privateKeyToAccount(getPrivateKeyFromFile(0)).address;
const AliceAddress = model.web3.eth.accounts.privateKeyToAccount(getPrivateKeyFromFile(1)).address;
await hasTxBlockNumber(model.deployJsonAbi("name", "symbol", "2000000000000000000", BobAddress));
await hasTxBlockNumber(model.transfer(AliceAddress, 1));
expect(await model.name()).to.be.eq(`name`)
})
})
})