Skip to content

Commit

Permalink
fix(tx builder)!: throw exception if deposit is not zero
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Feb 11, 2022
1 parent 9d8339a commit 7b3d0e3
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/ae/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import Ae from './'
import ContractCompilerHttp from '../contract/compiler'
import getContractInstance from '../contract/aci'
import { AMOUNT, DEPOSIT, MIN_GAS_PRICE } from '../tx/builder/schema'
import { AMOUNT, MIN_GAS_PRICE } from '../tx/builder/schema'
import { decode, produceNameId } from '../tx/builder/helpers'

/**
Expand Down Expand Up @@ -138,7 +138,6 @@ export default Ae.compose(ContractCompilerHttp, {
deepProps: {
Ae: {
defaults: {
deposit: DEPOSIT,
gasPrice: MIN_GAS_PRICE,
amount: AMOUNT
}
Expand Down
4 changes: 2 additions & 2 deletions src/contract/aci.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

import { Encoder as Calldata } from '@aeternity/aepp-calldata'
import { DRY_RUN_ACCOUNT, DEPOSIT, GAS_MAX } from '../tx/builder/schema'
import { DRY_RUN_ACCOUNT, GAS_MAX } from '../tx/builder/schema'
import TxObject from '../tx/tx-object'
import { decode } from '../tx/builder/helpers'
import {
Expand Down Expand Up @@ -182,7 +182,7 @@ export default async function getContractInstance ({
* @return {Object} deploy info
*/
instance.deploy = async (params = [], options) => {
const opt = { ...instance.options, ...options, deposit: DEPOSIT }
const opt = { ...instance.options, ...options }
if (!instance.bytecode) await instance.compile(opt)
if (opt.callStatic) return instance.call('init', params, opt)
if (instance.deployInfo.address) throw new DuplicateContractError()
Expand Down
13 changes: 12 additions & 1 deletion src/tx/builder/field-types.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
writeId, readId, isNameValid, produceNameId, ensureNameValid, getMinimumNameFee, readInt, writeInt
} from './helpers'
import { InsufficientNameFeeError } from '../../utils/errors'
import { InsufficientNameFeeError, IllegalArgumentError } from '../../utils/errors'

export class Field {
static serialize (value) {
Expand Down Expand Up @@ -48,3 +48,14 @@ export class NameFee extends Field {
return readInt(value)
}
}

export class Deposit extends Field {
static serialize (value, { name }) {
if (+value) throw new IllegalArgumentError(`Contract deposit is not refundable, so it should be equal 0, got ${value} instead`)
return writeInt(0)
}

static deserialize (value) {
return readInt(value)
}
}
4 changes: 2 additions & 2 deletions src/tx/builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function deserializeField (value, type, prefix) {
return { vmVersion: readInt(Buffer.from([vm])), abiVersion: readInt(Buffer.from([abi])) }
}
case FIELD_TYPES.amount:
return readInt(value)
case FIELD_TYPES.int:
return readInt(value)
case FIELD_TYPES.id:
Expand Down Expand Up @@ -306,7 +305,8 @@ export function validateParams (params, schema, { excludeKeys = [] }) {
return Object.fromEntries(
schema
// TODO: allow optional keys in schema
.filter(([key]) => !excludeKeys.includes(key) && !['payload', 'nameFee'].includes(key))
.filter(([key]) => !excludeKeys.includes(key) &&
!['payload', 'nameFee', 'deposit'].includes(key))
.map(([key, type, prefix]) => [key, validateField(params[key], type, prefix)])
.filter(([, message]) => message)
)
Expand Down
7 changes: 3 additions & 4 deletions src/tx/builder/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// # https://github.com/aeternity/protocol/blob/master/serializations.md#binary-serialization

import BigNumber from 'bignumber.js'
import { Name, NameId, NameFee } from './field-types'
import { Name, NameId, NameFee, Deposit } from './field-types'

export const VSN = 1
export const VSN_2 = 2
Expand All @@ -21,7 +21,6 @@ export const ORACLE_TTL = { type: 'delta', value: 500 }
export const QUERY_TTL = { type: 'delta', value: 10 }
export const RESPONSE_TTL = { type: 'delta', value: 10 }
// # CONTRACT
export const DEPOSIT = 0
export const AMOUNT = 0
export const GAS_MAX = 1600000 - 21000
export const MIN_GAS_PRICE = 1e9
Expand Down Expand Up @@ -519,7 +518,7 @@ const CONTRACT_TX = [
TX_FIELD('log', FIELD_TYPES.binary, 'cb'),
TX_FIELD('active', FIELD_TYPES.bool),
TX_FIELD('referers', FIELD_TYPES.ids, 'ak'),
TX_FIELD('deposit', FIELD_TYPES.amount)
TX_FIELD('deposit', Deposit)
]

const GA_ATTACH_TX = [
Expand Down Expand Up @@ -563,7 +562,7 @@ const CONTRACT_CREATE_TX = [
TX_FIELD('ctVersion', FIELD_TYPES.ctVersion),
TX_FIELD('fee', FIELD_TYPES.int),
TX_FIELD('ttl', FIELD_TYPES.int),
TX_FIELD('deposit', FIELD_TYPES.amount),
TX_FIELD('deposit', Deposit),
TX_FIELD('amount', FIELD_TYPES.amount),
TX_FIELD('gas', FIELD_TYPES.int),
TX_FIELD('gasPrice', FIELD_TYPES.int),
Expand Down
2 changes: 1 addition & 1 deletion src/tx/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ async function nameRevokeTx ({ accountId, nameId }) {
}

async function contractCreateTx ({
ownerId, code, vmVersion, abiVersion, deposit, amount, gas, gasPrice = MIN_GAS_PRICE, callData
ownerId, code, vmVersion, abiVersion, amount, gas, gasPrice = MIN_GAS_PRICE, callData
}) {
// Get VM_ABI version
const ctVersion = this.getVmVersion(TX_TYPE.contractCreate, arguments[0])
Expand Down
10 changes: 6 additions & 4 deletions test/integration/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { DRY_RUN_ACCOUNT } from '../../src/tx/builder/schema'
import { messageToHash, salt } from '../../src/utils/crypto'
import { randomName } from '../utils'
import { BaseAe, getSdk, publicKey } from './'
import { Crypto, MemoryAccount } from '../../src'
import { Crypto, IllegalArgumentError, MemoryAccount } from '../../src'
import { NodeInvocationError } from '../../src/utils/errors'

const identityContract = `
Expand Down Expand Up @@ -137,10 +137,12 @@ describe('Contract', function () {
expect(await contract.deploy()).to.have.property('address')
})

it('enforces zero deposit for contract deployment', async () => {
it('throws exception if deploy deposit is not zero', async () => {
contract.deployInfo = {}
expect((await contract.deploy([], { deposit: 10 })).txData.tx.deposit)
.to.be.equal(0)
await expect(contract.deploy([], { deposit: 10 })).to.be.rejectedWith(
IllegalArgumentError,
'Contract deposit is not refundable, so it should be equal 0, got 10 instead'
)
})

it('deploys static', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ contract Identity =
entrypoint getArg(x : int) = x
`
let contractId
const deposit = 4
const deposit = 0

let _salt
let commitmentId
Expand Down

0 comments on commit 7b3d0e3

Please sign in to comment.