Skip to content

Commit

Permalink
refactor!: make contractDeploy a wrapper, remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Nov 11, 2021
1 parent 2bb494d commit 48d36f9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 397 deletions.
35 changes: 7 additions & 28 deletions src/ae/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
*/

import Ae from './'
import * as R from 'ramda'
import ContractCompilerAPI from '../contract/compiler'
import ContractBase from '../contract'
import getContractInstance from '../contract/aci'
Expand Down Expand Up @@ -122,10 +121,11 @@ async function contractCall (source, contractAddress, name, args, options) {
* @function
* @alias module:@aeternity/aepp-sdk/es/ae/contract
* @category async
* @deprecated
* @param {String} code Compiled contract
* @param {String} source Contract source code
* @param {Array|String} initState Arguments of contract constructor(init) function. Can be array of arguments or callData string
* @param {Object} [options={}] Transaction options (fee, ttl, gas, amount, deposit)
* @param {Array} params Arguments of contract constructor(init) function. Can be array of arguments or callData string
* @param {Object} [options] Transaction options (fee, ttl, gas, amount, deposit)
* @param {Object} [options.filesystem={}] Contract external namespaces map* @return {Promise<Object>} Result object
* @return {Promise<Object>} Result object
* @example
Expand All @@ -140,31 +140,10 @@ async function contractCall (source, contractAddress, name, args, options) {
* callStatic: (fnName, args = [], options) => Static all contract function
* }
*/
async function contractDeploy (code, source, initState = [], options = {}) {
const opt = { ...this.Ae.defaults, ...options, deposit: DEPOSIT }
const callData = Array.isArray(initState) ? await this.contractEncodeCallDataAPI(source, 'init', initState, opt) : initState
const ownerId = await this.address(opt)

const { tx, contractId } = await this.contractCreateTx(R.merge(opt, {
callData,
code,
ownerId
}))

const { hash, rawTx, result, txData } = await this._sendAndProcess(tx, source, 'init', opt)
return Object.freeze({
result,
owner: ownerId,
transaction: hash,
rawTx,
txData,
address: contractId,
call: (name, args, options) =>
this.contractCall(source, contractId, name, args, { ...opt, ...options }),
callStatic: (name, args, options) =>
this.contractCallStatic(source, contractId, name, args, { ...opt, ...options }),
createdAt: new Date()
})
async function contractDeploy (code, source, params, options) {
const contract = await this.getContractInstance(source, options)
contract.compiled = code
return contract.deploy(params, options)
}

/**
Expand Down
49 changes: 0 additions & 49 deletions src/contract/aci/helpers.js

This file was deleted.

63 changes: 52 additions & 11 deletions src/contract/aci/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,35 @@
import * as R from 'ramda'
import BigNumber from 'bignumber.js'
import { Encoder as Calldata } from '@aeternity/aepp-calldata'
import { getFunctionACI, prepareArgsForEncode } from './helpers'
import { decodeEvents } from './transformation'
import { DRY_RUN_ACCOUNT } from '../../tx/builder/schema'
import { DRY_RUN_ACCOUNT, DEPOSIT } from '../../tx/builder/schema'
import TxObject from '../../tx/tx-object'

/**
* Get function schema from contract ACI object
* @param {Object} aci Contract ACI object
* @param {String} name Function name
* @param external
* @return {Object} function ACI
*/
function getFunctionACI (aci, name, external) {
const fn = aci.functions.find(f => f.name === name)
if (!fn && name !== 'init') throw new Error(`Function ${name} doesn't exist in contract`)

return {
...fn,
bindings: [
{
state: aci.state,
type_defs: aci.type_defs,
name: aci.name
},
...external.map(R.pick(['state', 'type_defs', 'name']))
],
event: aci.event ? aci.event.variant : []
}
}

/**
* Generate contract ACI object with predefined js methods for contract usage - can be used for creating a reference to already deployed contracts
* @alias module:@aeternity/aepp-sdk/es/contract/aci
Expand Down Expand Up @@ -95,20 +119,37 @@ export default async function getContractInstance (source, { aci, contractAddres
* Deploy contract
* @alias module:@aeternity/aepp-sdk/es/contract/aci
* @rtype (init: Array, options: Object) => ContractInstance: Object
* @param {Array} init Contract init function arguments array
* @param {Object} [options={}] options
* @param {Array} params Contract init function arguments array
* @param {Object} [options] options
* @return {Object} deploy info
*/
instance.deploy = async (init = [], options = {}) => {
const opt = { ...instance.options, ...options }

instance.deploy = async (params = [], options) => {
const opt = { ...instance.options, ...options, deposit: DEPOSIT }
if (!instance.compiled) await instance.compile(opt)

if (opt.callStatic) return instance.call('init', init, opt)
if (opt.callStatic) return instance.call('init', params, opt)

const fnACI = getFunctionACI(instance.aci, 'init', instance.externalAci)
init = await prepareArgsForEncode(fnACI, init)
instance.deployInfo = await this.contractDeploy(instance.compiled, opt.source || instance.source, init, opt)
const source = opt.source || instance.source
const ownerId = await this.address(opt)
const { tx, contractId } = await this.contractCreateTx(R.merge(opt, {
callData: instance.calldata.encode(instance.aci.name, 'init', params),
code: instance.compiled,
ownerId
}))
const { hash, rawTx, result, txData } = await this._sendAndProcess(tx, source, 'init', opt)
instance.deployInfo = Object.freeze({
result,
owner: ownerId,
transaction: hash,
rawTx,
txData,
address: contractId,
call: (name, args, options) =>
this.contractCall(source, contractId, name, args, { ...opt, ...options }),
callStatic: (name, args, options) =>
this.contractCallStatic(source, contractId, name, args, { ...opt, ...options }),
createdAt: new Date()
})
return instance.deployInfo
}

Expand Down
Loading

0 comments on commit 48d36f9

Please sign in to comment.