This repository was archived by the owner on Jan 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcontract-service.test.js
104 lines (89 loc) · 3.2 KB
/
contract-service.test.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { expect } from 'chai'
import ContractService from '../src/services/contract-service'
import { ipfsHashes } from './fixtures'
import Money from '../src/models/money'
import Web3 from 'web3'
const methodNames = ['getBytes32FromIpfsHash', 'getIpfsHashFromBytes32']
describe('ContractService', function() {
this.timeout(5000) // default is 2000
let contractService
beforeEach(async () => {
const provider = new Web3.providers.HttpProvider('http://localhost:8545')
const web3 = new Web3(provider)
contractService = new ContractService({
web3,
currencies: {
FOO: { address: '0x1234', decimals: 3 },
BAR: { address: '0x1234' }
}
})
})
methodNames.forEach(methodName => {
it(`should have ${methodName} method`, () => {
expect(contractService[methodName]).to.be.an.instanceof(Function)
})
})
describe('getBytes32FromIpfsHash', () => {
ipfsHashes.forEach(({ ipfsHash, bytes32 }) => {
it(`should correctly convert from IPFS hash ${ipfsHash}`, () => {
const result = contractService.getBytes32FromIpfsHash(ipfsHash)
expect(result).to.equal(bytes32)
})
})
})
describe('getIpfsHashFromBytes32', () => {
ipfsHashes.forEach(({ ipfsHash, bytes32 }) => {
it(`should correctly convert to IPFS hash ${ipfsHash}`, () => {
const result = contractService.getIpfsHashFromBytes32(bytes32)
expect(result).to.equal(ipfsHash)
})
})
})
describe('moneyToUnits', () => {
beforeEach(async () => {
contractService._currencies = {
FOO: { address: '0x1234', decimals: 3 },
BAR: { address: '0x1234' }
}
})
it(`should handle ERC20 token`, async () => {
const money = new Money({ amount: 123, currency: 'FOO' })
const units = await contractService.moneyToUnits(money)
expect(units).to.equal('123000')
})
it(`should handle ETH`, async () => {
const money = new Money({ amount: 123, currency: 'ETH' })
const units = await contractService.moneyToUnits(money)
expect(units).to.equal('123000000000000000000')
})
it(`should handle undefined currency decimals`, async () => {
const money = new Money({ amount: 123, currency: 'BAR' })
const units = await contractService.moneyToUnits(money)
expect(units).to.equal('123')
})
})
describe('passing in contract addresses', () => {
it('should allow contract addresses to be overridden', () => {
const web3 = new Web3()
const userAddress = '0x1234567890123456789012345678901234567890'
const contractAddresses = {
V00_UserRegistry: { 4: { address: userAddress } }
}
const contSrv = new ContractService({ web3, contractAddresses })
expect(contSrv.contracts.V00_UserRegistry.networks[4].address).to.equal(
userAddress
)
})
})
describe('currencies', () => {
it('should include OGN', async () => {
const currencies = await contractService.currencies()
expect(currencies).to.be.an('object')
const OGN = currencies.OGN
expect(OGN).to.be.an('object')
expect(OGN.address).to.be.a('string')
expect(OGN.address).to.include('0x')
expect(OGN.decimals).to.equal(18)
})
})
})