Skip to content

Commit

Permalink
feat: send tx by type (#42)
Browse files Browse the repository at this point in the history
* feat: send tx by type

* test: add test

* chore: wip

* test: add tests

BREAKING CHANGE: call and send tx by type
  • Loading branch information
nachomazzara authored Nov 8, 2018
1 parent d4d2449 commit c212997
Show file tree
Hide file tree
Showing 16 changed files with 5,201 additions and 2,477 deletions.
3,233 changes: 1,618 additions & 1,615 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@
"type": "git",
"url": "https://github.com/decentraland/decentraland-eth.git"
},
"files": ["dist"],
"keywords": ["common", "modules", "decentraland"],
"files": [
"dist"
],
"keywords": [
"common",
"modules",
"decentraland"
],
"author": "Decentraland",
"license": "ISC",
"dependencies": {
Expand Down
18 changes: 11 additions & 7 deletions scripts/contracts/ContractFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@ export class ContractFile {

switch (type) {
case 'function': {
const args = [...inputs.map((input, index) => input.name || `input${index}`), '...args'].join(', ')
const args = inputs.map(input => input.type).join(',')

if (stateMutability === 'view' || stateMutability === 'pure') {
extensions[name] = `function(${args}) {
return this.sendCall('${name}', ${args})
}`
if (!(name in extensions)) {
extensions[name] = new Function(`return this.sendCall('${name}', ...arguments)`)
}
extensions[name][args] = new Function(`return this.sendCallByType('${name}', '${args}', ...arguments)`)
} else if (stateMutability === 'nonpayable') {
extensions[name] = `function(${args}) {
return this.sendTransaction('${name}', ${args})
}`
if (!(name in extensions)) {
extensions[name] = new Function(`return this.sendTransaction('${name}', ...arguments)`)
}
extensions[name][args] = new Function(
`return this.sendTransactionByType('${name}', '${args}', ...arguments)`
)
}
break
}
Expand Down
94 changes: 94 additions & 0 deletions specs/Contracts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NodeConnectionFactory } from './NodeConnectionFactory'
import { deployContract } from './deployContract'
import { eth, txUtils } from '../dist'
import { MANAToken } from '../dist/contracts'
import { MANAToken as MANATokenOverloaded } from './MANAToken'

describe('ETH tests', () => {
const nodeConnectionFactory = new NodeConnectionFactory()
Expand Down Expand Up @@ -42,6 +43,99 @@ describe('ETH tests', () => {
provider.close()
})
})

describe('Transactions', function() {
let deployedMANAAddress = '0x0'
let MANAFacade: MANAToken
let account = '0x0'
let tenWei = eth.utils.toWei(10)

beforeEach(async () => {
const nodeConnectionFactory = new NodeConnectionFactory()
const provider = nodeConnectionFactory.createProvider()
await eth.connect({ provider })

const contract = await deployContract(eth.wallet, 'MANA', require('./fixtures/MANAToken.json'))
const txRecipt = await eth.wallet.getTransactionReceipt(contract.transactionHash)
deployedMANAAddress = txRecipt.contractAddress

MANAFacade = new MANATokenOverloaded(deployedMANAAddress)
await eth.setContracts([MANAFacade])
account = await eth.wallet.getAccount()
})

describe('Call', function() {
it('should send call by method name', async function() {
await MANAFacade.mint(account, tenWei)
const balance = await MANAFacade.balanceOf(account)
expect(balance).equal(10)
})

it('should send call by sendCall', async function() {
await MANAFacade.mint(account, tenWei)
const balance = await MANAFacade.sendCall('balanceOf', account)
expect(balance.toString()).equal(tenWei.toString())
})

it('should send call by sendCallByType', function() {
let fakeBalanceOf = MANAFacade.sendCallByType('fakeBalanceOf', 'address,address')
expect('then' in fakeBalanceOf).eq(true, 'The injected methods should be thenable')

fakeBalanceOf = MANAFacade.sendCallByType('fakeBalanceOf', 'address')
expect('then' in fakeBalanceOf).eq(true, 'The injected methods should be thenable')
})

it('should call overloaded method', function() {
expect(typeof MANAFacade['fakeBalanceOf']['address,address']).equal('function')
expect(typeof MANAFacade['fakeBalanceOf']['address']).equal('function')
})

it('throws when try to call overloaded method', async function() {
expect(() => MANAFacade['fakeBalanceOf'](account)).to.throw(
'Method: fakeBalanceOf is overloaded. Options available are: contract.fakeBalanceOf["address,address"](...), contract.fakeBalanceOf["address"](...)'
)
})
})

describe('Send Transaction', function() {
it('should send transaction by method name', async function() {
await MANAFacade.mint(account, tenWei)
const balance = await MANAFacade.balanceOf(account)
expect(balance).equal(10)
})

it('should send transaction by sendTransaction', async function() {
await MANAFacade.sendTransaction('mint', account, tenWei)
const balance = await MANAFacade.balanceOf(account)
expect(balance).equal(10)
})

it('should send transaction by sendTransactionByType', async function() {
await MANAFacade.sendTransactionByType('mint', 'address,uint256', account, tenWei)
const balance = await MANAFacade.balanceOf(account)
expect(balance).equal(10)
})

it('should send transaction by sendTransactionByType', function() {
let fakeMint = MANAFacade.sendTransactionByType('fakeMint', 'address,uint256')
expect('then' in fakeMint).eq(true, 'The injected methods should be thenable')

fakeMint = MANAFacade.sendTransactionByType('fakeMint', 'address,uint256,address')
expect('then' in fakeMint).eq(true, 'The injected methods should be thenable')
})

it('should call overloaded method', function() {
expect(typeof MANAFacade['fakeMint']['address,uint256']).equal('function')
expect(typeof MANAFacade['fakeMint']['address,uint256,address']).equal('function')
})

it('throws when try to call overloaded method', function() {
expect(() => MANAFacade['fakeMint'](account, tenWei)).to.throw(
'Method: fakeMint is overloaded. Options available are: contract.fakeMint["address,uint256,address"](...), contract.fakeMint["address,uint256"](...)'
)
})
})
})
})

function doTest() {
Expand Down
48 changes: 48 additions & 0 deletions specs/MANAToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { eth, Contract } from '../dist'
import { fulfillContractMethods } from '../dist/contracts/verification'

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

export interface MANAToken {
mintingFinished(): Promise<boolean>
name(): Promise<string>
totalSupply(): Promise<any /* BigNumber */>
transferFrom(from: string, to: string, value): Promise<boolean>
decimals(): Promise<number>
unpause(): Promise<boolean>
mint(to: string, amount): Promise<string>
burn(value): Promise<void>
paused(): Promise<boolean>
finishMinting(): Promise<boolean>
pause(): Promise<boolean>
owner(): Promise<string>
symbol(): Promise<string>
transferOwnership(newOwner: string): Promise<void>
transfer(to: string, value): Promise<boolean>
}

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

getContractName() {
return 'MANAToken'
}

async approve(spender: string, mana: number) {
return this.sendTransaction('approve', spender, eth.utils.toWei(mana))
}

async allowance(owner: string, spender: string) {
const assigned = await this.sendCall('allowance', owner, spender)
return eth.utils.fromWei(assigned)
}

async balanceOf(owner: string): Promise<number> {
const manaBalance = await this.sendCall('balanceOf', owner)
return eth.utils.fromWei(manaBalance)
}
}
Loading

0 comments on commit c212997

Please sign in to comment.