Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is there a way to callStatic func like in ethers js? #26254

Open
jonathantyar opened this issue Nov 25, 2022 · 7 comments
Open

is there a way to callStatic func like in ethers js? #26254

jonathantyar opened this issue Nov 25, 2022 · 7 comments

Comments

@jonathantyar
Copy link

just try out go-ethereum its worked great so far. but had problem when trying to call static func like in ether js. using transactionOpt to not send the transaction. the command is worked, but abigen only return transacction and error where it should send another value. How to get the value i need on these transaction.

this is how i call to contract func, with nosend and signer params

	trx, err := client.Collect(&bind.TransactOpts{
		Signer: func(addr common.Address, trx *types.Transaction) (*types.Transaction, error) {
			return trx, nil
		},
		NoSend: true,
	}, cex.INonfungiblePositionManagerCollectParams{
		TokenId:    big.NewInt(tokenId),
		Recipient:  address.Address(),
		Amount0Max: bigTotal,
		Amount1Max: bigTotal,
	})

this is generated code from abigen, it shows comment that a func should return 3 values, but instead abigen generated just 2 values


// Collect is a paid mutator transaction binding the contract method 0xfc6f7865.
//
// Solidity: function collect((uint256,address,uint128,uint128) params) payable returns(uint256 amount0, uint256 amount1)
func (_UNISWAPv3 *UNISWAPv3Transactor) Collect(opts *bind.TransactOpts, params INonfungiblePositionManagerCollectParams) (*types.Transaction, error) {
	return _UNISWAPv3.contract.Transact(opts, "collect", params)
}

// Collect is a paid mutator transaction binding the contract method 0xfc6f7865.
//
// Solidity: function collect((uint256,address,uint128,uint128) params) payable returns(uint256 amount0, uint256 amount1)
func (_UNISWAPv3 *UNISWAPv3Session) Collect(params INonfungiblePositionManagerCollectParams) (*types.Transaction, error) {
	return _UNISWAPv3.Contract.Collect(&_UNISWAPv3.TransactOpts, params)
}

// Collect is a paid mutator transaction binding the contract method 0xfc6f7865.
//
// Solidity: function collect((uint256,address,uint128,uint128) params) payable returns(uint256 amount0, uint256 amount1)
func (_UNISWAPv3 *UNISWAPv3TransactorSession) Collect(params INonfungiblePositionManagerCollectParams) (*types.Transaction, error) {
	return _UNISWAPv3.Contract.Collect(&_UNISWAPv3.TransactOpts, params)
}
@jonathantyar
Copy link
Author

i can call this func on ethers js using callstatic and it return the result i need

  const encoded = {
    tokenId: positionId,
    recipient: FROM,
    amount0Max: MAX,
    amount1Max: MAX,
  }

  const trx = await contract.callStatic.collect(encoded)

  const formatUnits = (amt: Number, units?: number): string =>
    ethers.utils
      .formatUnits(ethers.BigNumber.from(amt).toString(), units || 18)
      .toString()

  const amount0 = formatUnits(trx.amount0, 8)
  const amount1 = formatUnits(trx.amount1)

  console.log(`Token 0: ${amount0} | Token 1: ${amount1}`)

@fjl
Copy link
Contributor

fjl commented Dec 1, 2022

While we don't have this feature right now, there is a workaround: encode the call yourself, submit using CallContract with ethclient, and decode the result using accounts/abi.

@jonathantyar
Copy link
Author

Thanks @fjl, its works using CallContract. Changed i made below

GeneratedCode :

// Solidity: function collect((uint256,address,uint128,uint128) params) payable returns(uint256 amount0, uint256 amount1)
func (_UNISWAPv3 *UNISWAPv3Transactor) Collect(opts *bind.TransactOpts, params INonfungiblePositionManagerCollectParams) (*types.Transaction, error) {
	return _UNISWAPv3.contract.Transact(opts, "collect", params)
}

Updated Code using CallContract :

// Solidity: function collect((uint256,address,uint128,uint128) params) payable returns(uint256 amount0, uint256 amount1)
func (_UNISWAPv3 *UNISWAPv3Transactor) Collect(opts *bind.CallOpts, params INonfungiblePositionManagerCollectParams) (*big.Int, *big.Int, error) {
	var out []interface{}
	err := _UNISWAPv3.contract.Call(opts, &out, "collect", params)

	if err != nil {
		return new(big.Int), new(big.Int), err
	}

	out0 := *abi.ConvertType(out[0], new(big.Int)).(*big.Int)
	out1 := *abi.ConvertType(out[1], new(big.Int)).(*big.Int)

	return &out0, &out1, err
}

@s1na
Copy link
Contributor

s1na commented Mar 7, 2023

This becomes possible via #26782

@aftermathdigital
Copy link

Just stumbled across this, thanks so much @jonathantyar for your example.

It feels like this is an area where abigen is a little inflexible compared to the competition. Are there any plans to address this? Perhaps using the pull I see s1na has posted?

@banky
Copy link

banky commented Aug 6, 2024

One workaround here is that you can modify the abi file that is used for abigen to update the state mutability of the function you want to call. For example, I'm running into a similar issue with multicall since the functions are payable. Since I don't need to update the blockchain state, I just modified the stateMutability from payable to view and abigen now does what I need

@kkqy
Copy link
Contributor

kkqy commented Oct 26, 2024

One workaround here is that you can modify the abi file that is used for abigen to update the state mutability of the function you want to call. For example, I'm running into a similar issue with multicall since the functions are payable. Since I don't need to update the blockchain state, I just modified the stateMutability from payable to view and abigen now does what I need这里的一种解决方法是,您可以修改用于 abigen 的 abi 文件,以更新要调用的函数的状态可变性。例如,我在多重调用方面遇到了类似的问题,因为这些函数是付费的。由于我不需要更新区块链状态,我只是将 stateMutability 从payable修改为view ,abigen 现在可以满足我的需要

This method is both good and simple.

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

No branches or pull requests

8 participants