Skip to content

Commit

Permalink
[contractkit] 0.1 release + tests (#628)
Browse files Browse the repository at this point in the history
* Fix test configuration
* Test contract & wrapper caches
* contrackit v0.1.0
  • Loading branch information
Mariano Cortesi committed Aug 18, 2019
1 parent 1d8bee1 commit 7524b66
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 10 deletions.
11 changes: 9 additions & 2 deletions packages/contractkit/jest.config.js
@@ -1,7 +1,14 @@
const { pathsToModuleNameMapper } = require('ts-jest/utils')

const { compilerOptions } = require('./test/tsconfig')

module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleNameMapper: {
'^src/(.*)$': '<rootDir>/src/$1',
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }),
globals: {
'ts-jest': {
tsConfig: '<rootDir>/test/tsconfig.json',
},
},
}
9 changes: 7 additions & 2 deletions packages/contractkit/package.json
@@ -1,14 +1,19 @@
{
"name": "@celo/contractkit",
"version": "0.0.5-beta3",
"version": "0.1.0",
"description": "Celo's ContractKit to interact with Celo network",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"author": "Celo",
"license": "Apache-2.0",
"homepage": "https://github.com/celo-org/celo-monorepo/tree/master/packages/contractkit",
"repository": "https://github.com/celo-org/celo-monorepo/tree/master/packages/contractkit",
"keywords": ["celo", "blockchain", "contractkit", "defi"],
"keywords": [
"celo",
"blockchain",
"contractkit",
"defi"
],
"scripts": {
"build": "tsc -b .",
"clean": "tsc -b . --clean",
Expand Down
3 changes: 2 additions & 1 deletion packages/contractkit/src/contract-cache.ts
Expand Up @@ -25,6 +25,7 @@ const WrapperFactories = {
}

type CFType = typeof WrapperFactories
export type ValidWrappers = keyof CFType

interface WrapperCacheMap {
// [CeloContract.Attestations]?: AttestationsWrapper,
Expand Down Expand Up @@ -93,7 +94,7 @@ export class WrapperCache {
return this.getContract(CeloContract.Validators)
}

public async getContract<C extends keyof CFType>(contract: C) {
public async getContract<C extends ValidWrappers>(contract: C) {
if (this.wrapperCache[contract] == null) {
const instance = await this.kit._web3Contracts.getContract(contract)
const Klass: CFType[C] = WrapperFactories[contract]
Expand Down
2 changes: 1 addition & 1 deletion packages/contractkit/src/index.ts
@@ -1,6 +1,6 @@
import Web3 from 'web3'

export { Address, CeloContract, CeloToken, NULL_ADDRESS } from './base'
export { Address, AllContracts, CeloContract, CeloToken, NULL_ADDRESS } from './base'
export * from './kit'

export function newWeb3(url: string) {
Expand Down
39 changes: 39 additions & 0 deletions packages/contractkit/test/cache/contract-cache.test.ts
@@ -0,0 +1,39 @@
import { ValidWrappers, WrapperCache } from '@src/contract-cache'
import { CeloContract, newKit } from '@src/index'

const TestedWrappers: ValidWrappers[] = [
CeloContract.GoldToken,
CeloContract.StableToken,
CeloContract.Exchange,
CeloContract.Validators,
CeloContract.BondedDeposits,
]

function newWrapperCache() {
const kit = newKit('')
const AnyContractAddress = '0xe832065fb5117dbddcb566ff7dc4340999583e38'
jest.spyOn(kit.registry, 'addressFor').mockResolvedValue(AnyContractAddress)
const contractCache = new WrapperCache(kit)
return contractCache
}

describe('getContract()', () => {
const contractCache = newWrapperCache()

for (const contractName of TestedWrappers) {
test(`SBAT get ${contractName}`, async () => {
const contract = await contractCache.getContract(contractName)
expect(contract).not.toBeNull()
expect(contract).toBeDefined()
})
}
})

test('should cache contracts', async () => {
const contractCache = newWrapperCache()
for (const contractName of TestedWrappers) {
const contract = await contractCache.getContract(contractName)
const contractBis = await contractCache.getContract(contractName)
expect(contract).toBe(contractBis)
}
})
31 changes: 31 additions & 0 deletions packages/contractkit/test/cache/web3-contract-cache.test.ts
@@ -0,0 +1,31 @@
import { AllContracts, newKit } from '@src/index'
import { Web3ContractCache } from '@src/web3-contract-cache'

function newWeb3ContractCache() {
const kit = newKit('')
const AnyContractAddress = '0xe832065fb5117dbddcb566ff7dc4340999583e38'
jest.spyOn(kit.registry, 'addressFor').mockResolvedValue(AnyContractAddress)
const contractCache = new Web3ContractCache(kit)
return contractCache
}

describe('getContract()', () => {
const contractCache = newWeb3ContractCache()

for (const contractName of AllContracts) {
test(`SBAT get ${contractName}`, async () => {
const contract = await contractCache.getContract(contractName)
expect(contract).not.toBeNull()
expect(contract).toBeDefined()
})
}
})

test('should cache contracts', async () => {
const contractCache = newWeb3ContractCache()
for (const contractName of AllContracts) {
const contract = await contractCache.getContract(contractName)
const contractBis = await contractCache.getContract(contractName)
expect(contract).toBe(contractBis)
}
})
6 changes: 5 additions & 1 deletion packages/contractkit/test/tsconfig.json
Expand Up @@ -2,7 +2,11 @@
"extends": "../../typescript/tsconfig.library.json",
"compilerOptions": {
"rootDir": ".",
"noEmit": true
"noEmit": true,
"baseUrl": "..",
"paths": {
"@src/*": ["src/*"]
}
},
"include": ["."],
"references": [{ "path": ".." }]
Expand Down
2 changes: 1 addition & 1 deletion packages/contractkit/test/utils/future.test.ts
@@ -1,4 +1,4 @@
import { Future } from '../../src/utils/future'
import { Future } from '@src/utils/future'

test('it should expose resolve/reject inmediately', async () => {
const ep = new Future<number>()
Expand Down
2 changes: 1 addition & 1 deletion packages/contractkit/test/utils/send-tx.test.ts
@@ -1,7 +1,7 @@
import { sendTransaction } from '@src/utils/send-tx'
import { TransactionObject, Tx } from 'web3/eth/types'
import PromiEvent from 'web3/promiEvent'
import { TransactionReceipt } from 'web3/types'
import { sendTransaction } from '../../src/utils/send-tx'
import { promiEventSpy } from '../test-utils/PromiEventStub'

interface TransactionObjectStub<T> extends TransactionObject<T> {
Expand Down
2 changes: 1 addition & 1 deletion packages/contractkit/test/utils/tx-result.test.ts
@@ -1,4 +1,4 @@
import { toTxResult } from '../../src/utils/tx-result'
import { toTxResult } from '@src/utils/tx-result'
import { promiEventSpy } from '../test-utils/PromiEventStub'

test('should resolve to hash & receipt on normal flow', async () => {
Expand Down

0 comments on commit 7524b66

Please sign in to comment.