Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Inject bytecode for a contract/ how to debug when only have interface for already deployed contracts #3301

Closed
fxfactorial opened this issue Aug 25, 2020 · 4 comments

Comments

@fxfactorial
Copy link

fxfactorial commented Aug 25, 2020

Somewhat related to this other recently opened issue

0xProject/0x-monorepo#2679

I have interfaces that I'm using in my contract which I deployed against my forked ganache node - and interacting with the deployed contract via truffle console --network forked

the contract has a method call that is exploding and via the 0x stack tracer , I get the line that is exploding, but no deeper insight into the stack because i don't have the bytecode of the external libraries, just their interfaces.

truffle(forked)> Unable to find matching bytecode for contract address 0x45f783cce6b7ff23b2ab2d70e416cdb7d6055f51, please check your artifacts. Ignoring...

in principle, the chain does have the bytecode, and I tried adding the byte code into the build aritifacts by hand for the bytecode key value but this is hacky and it didn't produce any good results either.

I also tried truffle debug but truffle debug itself broke on


debug(forked:0x8ed58570...)> n
⠋ Stepping...TypeError: Cannot read property 'stack' of undefined
    at /home/edgar/.nvm/versions/node/v12.18.3/lib/node_modules/truffle/build/webpack:/packages/debugger/dist/debugger.js:1:10570

so its not clear to me, what is the right flow to debug when you use external, already deployed contracts

I suppose I can build all my dependencies as well - but I am not sure how to do that either because curve.fi uses vyber and contracts expects everything to be just .sol

thank you

@gnidan
Copy link
Contributor

gnidan commented Aug 25, 2020

Thanks for opening this!

For the stacktrace problem, I'm not sure there's a fix right now, but sounds like something @haltman-at will want to know about!

For truffle debug, try adding this flag:

$ truffle debug --fetch-external <tx-hash>

(or -x for short)

This will tell Truffle to download verified contracts from Etherscan and allow you to debug them. Note that your forked network's network ID must match the network ID for the network you're forking from (This is default behavior with ganache-cli, but wanted to warn you in case you're overriding the network ID yourself!) For more info, see our recent blog post about --fetch-external, which includes a step-by-step that should serve as a reference for you in case of potential gotchas.

(If you try this, please let us know if you get it working or not!)

@fxfactorial
Copy link
Author

fxfactorial commented Aug 26, 2020

Cool - thank you for the quick reply -

my attempt:

truffle debug --network forked --fetch-external 0x85aeb3f89b43428fe59cf4305a0725f3bbaefe6e77d0c86c41e8841af028b246

so I tried it against a forked ganache and it errored out with

 truffle debug --network forked --fetch-external 0x85aeb3f89b43428fe59cf4305a0725f3bbaefe6e77d0c86c41e8841af028b246
Starting Truffle Debugger...
⠋ Getting and compiling external sources...
/home/edgar/Repos/hye-token/node_modules/web3-provider-engine/subproviders/websocket.js:120
    end(null, payload.result)
    ^
TypeError: end is not a function
    at WebsocketSubprovider._handleSocketMessage (/home/edgar/Repos/hye-token/node_modules/web3-provider-engine/subproviders/websocket.js:120:5)
    at WebSocket.onMessage (/home/edgar/Repos/hye-token/node_modules/ws/lib/event-target.js:120:16)
    at WebSocket.emit (events.js:315:20)
    at WebSocket.EventEmitter.emit (domain.js:483:12)
    at Receiver.receiverOnMessage (/home/edgar/Repos/hye-token/node_modules/ws/lib/websocket.js:720:20)
    at Receiver.emit (events.js:315:20)

which might or not be related to how i set up my config, based off how sol-trace had in their exmaple truffle repo

const ProviderEngine = require('web3-provider-engine');
const WebsocketSubprovider = require('web3-provider-engine/subproviders/websocket.js');
const {
  TruffleArtifactAdapter,
  RevertTraceSubprovider,
} = require('@0x/sol-trace');

const projectRoot = '.';
const solcVersion = '0.6.5+commit.f956cc89';
const defaultFromAddress = '0xe8B479...0';
const isVerbose = true;

const artifactAdapter = new TruffleArtifactAdapter(projectRoot, solcVersion);

const revertTraceSubprovider = new RevertTraceSubprovider(
  artifactAdapter,
  defaultFromAddress,
  isVerbose
);

const provider = new ProviderEngine();

provider.addProvider(revertTraceSubprovider);
provider.addProvider(
  new WebsocketSubprovider({rpcUrl: 'http://localhost:8545'})
);

provider.start((err) => {
  if (err !== undefined) {
    console.log(err);
    process.exit(1);
  }
});

provider.send = provider.sendAsync.bind(provider);

module.exports = {
  networks: {
    forked: {
      provider,
      network_id: '1',
      skipDryRun: true,
    },
  },

@haltman-at
Copy link
Contributor

So taking a look at this -- it looks like a lot of the issue is with this external tool, the 0x tracer? I don't think we can help you with that. However, it seems you're also reporting a crash in the Truffle debugger? That I should be able to do something about if I can reproduce it. Do you have reproduction steps for that, or perhaps a reproduction project I could take a look at? Thank you!

@fxfactorial
Copy link
Author

fxfactorial commented Aug 27, 2020

ya - apologies - so I removed the sol-trace stuff and the debugger did give awesome stack trace!

I'll also mention that since posting issue - I switched to ganache built off beta branch , which works better than current release

Revert message: UniswapV2: LOCKED
Please inspect your transaction parameters and contract code to determine the meaning of this error.

Stacktrace:
Error: Revert (message: UniswapV2: LOCKED)
  at UniswapV2Pair.swap [address 0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc] (/tmp/tmp--635257-MrbdG7tPt4pm/UniswapV2Pair.sol:327:9)
  at UniswapV2Pair [address 0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc] (/tmp/tmp--635257-MrbdG7tPt4pm/UniswapV2Pair.sol:454:5)
  at Hye.kick_off_uniswap [address 0x3a173729D0De183d4d937e9cb7E28b95F4574946] (/home/edgar/Repos/hye-token/contracts/Hye.sol:310:5)
  at Hye [address 0x3a173729D0De183d4d937e9cb7E28b95F4574946] (/home/edgar/Repos/hye-token/contracts/Hye.sol:285:3)
debug(forked:0x1068db59...)> 

the line numbers don't match up with original source but that's totally fine - the temp files are correct wrt content of source. AMAZING JOB FOLKS!! I love this feature.

I will close because now there is a different issue -

the real revert reason is

        require(balance0Adjusted.mul(balance1Adjusted) >= uint(_reserve0).mul(_reserve1).mul(1000**2), 'UniswapV2: K');

in UniswapV2Pair.sol:182,

but after reverting the txn, ganache keeps the lock value - so the debug is going to the wrong place, reported many times in uniswap discord chat &

Uniswap/v2-periphery#44

after a failed swap iteration, I restart ganache so the locked value is fresh again.

Thank you so much team - this is an amazing project - amazing tools

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants