9

I want to deploy pre-compiled Smart Contract bytecode using Truffle migrations. Specifically, it is the Uniswap V2 Factory contract.

In the Uniswap V2 documentation there is a cliffhanger, where they mention to use:

const UniswapV2FactoryBytecode = require('@uniswap/v2-core/build/UniswapV2Factory.json').bytecode

And then deploy it using Truffle (but do not include an example). However, I cannot get the deployment to work. Here is my migration script:

const UniswapV2FactoryBytecode = require('@uniswap/v2-core/build/UniswapV2Factory.json').bytecode
module.exports = function(_deployer, network, accounts) {
    _deployer.deploy(UniswapV2FactoryBytecode, accounts[0])
};

Note the UniswapV2Factory constructor requires a _feeToSetter address which is why I pass accounts[0] as a parameter.

When I run using truffle migrate --reset the following error is returned:

TypeError: contract.detectNetwork is not a function

Can anyone help point to a solution get this to work?

2 Answers 2

9

One solution appears to be to install @truffle/contract package like so:

npm install @truffle/contract

Then in the migration file you will need to grab the full contract JSON (not just the bytecode as mentioned in the question) and use the @truffle/contract to create a contract instance from the JSON. You will also need to set the provider manually, and set the deployer account manually too.

const json = require('@uniswap/v2-core/build/UniswapV2Factory.json')
const contract = require('@truffle/contract');
const UniswapV2Factory = contract(json);

UniswapV2Factory.setProvider(this.web3._provider);

module.exports = function(_deployer, network, accounts) {
    _deployer.deploy(UniswapV2Factory, accounts[0], {from: accounts[0]})
};

Given all the extra hoops to jump through this might not be the best approach but it does produce what I was after - an instance of the contract deployed in my development environment.

1

My solution was copying the UniswapV2Factory contract's JSON file into build/contracts folder. Then I just used it as any other artifact with artifacts.require("UniswapV2Factory");.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.