Skip to content

Commit

Permalink
feat: sanitize abi (#56)
Browse files Browse the repository at this point in the history
Sanitize ABI events with parameters name which conflict with web3 0.X.X
  • Loading branch information
nachomazzara authored and nicosantangelo committed Mar 7, 2019
1 parent cede496 commit c741bf2
Show file tree
Hide file tree
Showing 17 changed files with 33 additions and 29 deletions.
5 changes: 5 additions & 0 deletions specs/txUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from 'chai'
import { deployContract } from './deployContract'
import { eth, txUtils } from '../dist'
import { testGeth } from './helpers'
import { sleep } from '../dist/utils'

testGeth(provider => {
it('initializes eth provider', async () => {
Expand All @@ -13,11 +14,15 @@ testGeth(provider => {
it('should return the confirmed transaction status and its receipt', async function() {
this.timeout(100000)

await sleep(500)
const contract = await deployContract(eth.wallet, 'MANA', require('./fixtures/MANAToken.json'))
await sleep(500)

/* tslint:disable-next-line:no-unnecessary-type-assertion */
const { receipt, ...tx } = (await txUtils.getTransaction(
contract.transactionHash
)) as txUtils.ConfirmedTransaction
await sleep(500)

expect(tx).to.include.keys([
'type',
Expand Down
2 changes: 0 additions & 2 deletions src/contracts/DecentralandInvite.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Contract } from '../ethereum'
import { fulfillContractMethods } from './verification'

const { abi } = require('./artifacts/DecentralandInvite.json')

/** DecentralandInvite contract class */
export class DecentralandInvite extends Contract {
constructor(address: string) {
super(address, abi)
fulfillContractMethods(this, abi)
}

getContractName() {
Expand Down
2 changes: 0 additions & 2 deletions src/contracts/DecentralandVesting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { eth, Contract } from '../ethereum'
import { fulfillContractMethods } from './verification'
import { BigNumber } from 'bignumber.js'

const { abi } = require('./artifacts/DecentralandVesting.json')
Expand All @@ -8,7 +7,6 @@ const { abi } = require('./artifacts/DecentralandVesting.json')
export class DecentralandVesting extends Contract {
constructor(address: string = process.env.TERRAFORM_RESERVE_CONTRACT_ADDRESS) {
super(address, abi)
fulfillContractMethods(this, abi)
}

getContractName() {
Expand Down
2 changes: 0 additions & 2 deletions src/contracts/ERC20Token.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Contract } from '../ethereum'
import { fulfillContractMethods } from './verification'

const { abi } = require('./artifacts/ERC20Token.json')

/** ERC20Token contract class */
export class ERC20Token extends Contract {
constructor(address: string) {
super(address, abi)
fulfillContractMethods(this, abi)
}

getContractName() {
Expand Down
2 changes: 0 additions & 2 deletions src/contracts/ERC721Bid.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Contract } from '../ethereum'
import { fulfillContractMethods } from './verification'

const { abi } = require('./artifacts/ERC721Bid.json')

export class ERC721Bid extends Contract {
constructor(address: string) {
super(address, abi)
fulfillContractMethods(this, abi)
}

getContractName() {
Expand Down
2 changes: 0 additions & 2 deletions src/contracts/ERC721Token.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Contract } from '../ethereum'
import { fulfillContractMethods } from './verification'

const { abi } = require('./artifacts/ERC721Token.json')

Expand All @@ -8,7 +7,6 @@ const { abi } = require('./artifacts/ERC721Token.json')
export class ERC721Token extends Contract {
constructor(address: string) {
super(address, abi)
fulfillContractMethods(this, abi)
}

getContractName() {
Expand Down
2 changes: 0 additions & 2 deletions src/contracts/EstateRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Contract } from '../ethereum'
import { fulfillContractMethods } from './verification'

const { abi } = require('./artifacts/EstateRegistry.json')

export class EstateRegistry extends Contract {
constructor(address: string) {
super(address, abi)
fulfillContractMethods(this, abi)
}

getContractName() {
Expand Down
2 changes: 0 additions & 2 deletions src/contracts/LANDAuction.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Contract } from '../ethereum'
import { fulfillContractMethods } from './verification'

const { abi } = require('./artifacts/LANDAuction.json')

export class LANDAuction extends Contract {
constructor(address: string) {
super(address, abi)
fulfillContractMethods(this, abi)
}

getContractName() {
Expand Down
2 changes: 0 additions & 2 deletions src/contracts/LANDRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as CSV from 'comma-separated-values'
import { Contract } from '../ethereum'
import { fulfillContractMethods } from './verification'
const { abi } = require('./artifacts/LANDRegistry.json')

const MAX_NAME_LENGTH = 50
Expand All @@ -19,7 +18,6 @@ export class LANDRegistry extends Contract {

constructor(address: string) {
super(address, abi)
fulfillContractMethods(this, abi)
}

static decodeLandData(data = '') {
Expand Down
2 changes: 0 additions & 2 deletions src/contracts/LegacyMarketplace.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Contract } from '../ethereum'
import { fulfillContractMethods } from './verification'

const { abi } = require('./artifacts/LegacyMarketplace.json')

/** LegacyMarketplace contract class */
export class LegacyMarketplace extends Contract {
constructor(address: string) {
super(address, abi)
fulfillContractMethods(this, abi)
}

getContractName() {
Expand Down
2 changes: 0 additions & 2 deletions src/contracts/MANAToken.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { eth, Contract } from '../ethereum'
import { fulfillContractMethods } from './verification'

const { abi } = require('./artifacts/MANAToken.json')

Expand All @@ -25,7 +24,6 @@ export interface MANAToken {
export class MANAToken extends Contract {
constructor(address: string) {
super(address, abi)
fulfillContractMethods(this, abi)
}

getContractName() {
Expand Down
2 changes: 0 additions & 2 deletions src/contracts/Marketplace.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Contract } from '../ethereum'
import { fulfillContractMethods } from './verification'

const { abi } = require('./artifacts/Marketplace.json')

/** Marketplace contract class */
export class Marketplace extends Contract {
constructor(address: string) {
super(address, abi)
fulfillContractMethods(this, abi)
}

getContractName() {
Expand Down
2 changes: 0 additions & 2 deletions src/contracts/MortgageHelper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Contract } from '../ethereum'
import { fulfillContractMethods } from './verification'
const { abi } = require('./artifacts/MortgageHelper.json')

/** MortgageCreator contract class */
export class MortgageHelper extends Contract {
constructor(address: string) {
super(address, abi)
fulfillContractMethods(this, abi)
}

getContractName() {
Expand Down
2 changes: 0 additions & 2 deletions src/contracts/MortgageManager.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Contract } from '../ethereum'
import { fulfillContractMethods } from './verification'
const { abi } = require('./artifacts/MortgageManager.json')

/** MortgageManager contract class */
export class MortgageManager extends Contract {
constructor(address: string) {
super(address, abi)
fulfillContractMethods(this, abi)
}

getContractName() {
Expand Down
2 changes: 0 additions & 2 deletions src/contracts/RCNEngine.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Contract } from '../ethereum'
import { fulfillContractMethods } from './verification'
const { abi } = require('./artifacts/RCNEngine.json')

/** RCNEngine contract class */
export class RCNEngine extends Contract {
constructor(address: string) {
super(address, abi)
fulfillContractMethods(this, abi)
}

getContractName() {
Expand Down
7 changes: 6 additions & 1 deletion src/ethereum/Contract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { promisify } from '../utils'
import { Abi } from './abi'
import { Event } from './Event'
import { fulfillContractMethods } from '../contracts/verification'

/** Class to work with Ethereum contracts */
export abstract class Contract<T = any> {
Expand All @@ -14,7 +15,11 @@ export abstract class Contract<T = any> {
*/
constructor(address: string, abi: object) {
this.setAddress(address)
this.setAbi(abi)
const sanitizedABI = Abi.sanitize(abi)

this.setAbi(sanitizedABI)

fulfillContractMethods(this, sanitizedABI)

this.instance = null
}
Expand Down
22 changes: 22 additions & 0 deletions src/ethereum/abi/Abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ const abi = {
if (param) {
return param.value
}
},

/**
* Sanitize ABI.
* Web3 0.X.X has "owner" as a reserved word, so if one of the event parameters name is "owner"
* it will break. This method will change every "owner" to "_owner"
* @param {array} ABI
* @return {array} - Sanitized ABI
*/
sanitize(abi) {
return abi.map(prop => {
let inputs = prop.inputs
if (prop.name && prop.type === 'event') {
inputs = prop.inputs.map(input => {
if (input.name === 'owner') {
input.name = '_owner'
}
return input
})
}
return { ...prop, inputs }
})
}
}

Expand Down

0 comments on commit c741bf2

Please sign in to comment.