xclud / web3dart

Ethereum library, written in Dart.
https://pub.dev/packages/web3dart
MIT License
170 stars 94 forks source link

Proxy contract interaction #94

Closed julian-CStack closed 1 year ago

julian-CStack commented 1 year ago

USDC (https://etherscan.io/token/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48#code) for example, has no balanceOf or transfer functions in its ABI so final balanceFunction = DeployedContract.function('balanceOf'); will throw unless the balanceOf function is manually added to the ABI string before construction of the DeployedContract object.

xclud commented 1 year ago

Can you paste some code, please?

xclud commented 1 year ago

The contract does have the functions. Refer to "Read as Proxy" tab. Or https://etherscan.io/readContract?m=l&a=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48&n=mainnet&v=0xa2327a938febf5fec13bacfb16ae10ecbc4cbdcf

julian-CStack commented 1 year ago

I am currently fetching the ABI via etherscan's api for the USDC contract above and I do not get the functions from the 'Read as Proxy'. The result of the abi fetch request is: { "status": "1", "message": "OK", "result": "[{\"constant\":false,\"inputs\":[{\"name\":\"newImplementation\",\"type\":\"address\"}],\"name\":\"upgradeTo\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"newImplementation\",\"type\":\"address\"},{\"name\":\"data\",\"type\":\"bytes\"}],\"name\":\"upgradeToAndCall\",\"outputs\":[],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"implementation\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"newAdmin\",\"type\":\"address\"}],\"name\":\"changeAdmin\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"admin\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"name\":\"_implementation\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"fallback\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"previousAdmin\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"newAdmin\",\"type\":\"address\"}],\"name\":\"AdminChanged\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"implementation\",\"type\":\"address\"}],\"name\":\"Upgraded\",\"type\":\"event\"}]" }

Its entirely possible I'm missing something simple to get the actual ABI of the contract the proxy points to.

xclud commented 1 year ago

Because USDC is a proxy contract, all the calls are forwarded to the proxied contract. So you must get the ABI of the Proxy contract.

https://ethereum.stackexchange.com/questions/121370/how-do-i-call-functions-from-read-as-proxy-on-smart-contracts

https://ethereum.stackexchange.com/questions/122085/usdc-etherscan-abi-missing-decimals-function

julian-CStack commented 1 year ago

Still a bit new to working with eth contracts and I thought there might be a better way to fetch the ABI of the contract that the proxy contract points to. Looking it up manually is not an option so I will need to figure out some other way to fetch the correct ABI. Possibly like this: https://ethereum.stackexchange.com/questions/135333/is-it-possible-to-pull-proxy-contracts-through-an-ethereum-based-api

xclud commented 1 year ago

I've found a solution for you, but I am not sure if this is the best possible way of doing it:

export YOUR_API_KEY=12345678
$ curl -d "address=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48" "https://api.etherscan.io/api?module=contract&action=verifyproxycontract&apikey=$YOUR_API_KEY"

# {"status":"1","message":"OK","result":"vbdpwlap3xapgkxvltjazvhqqssy2jmyhdr4a2wlrbimt9qrhm"}

$ curl "https://api.etherscan.io/api?module=contract&action=checkproxyverification&guid=vbdpwlap3xapgkxvltjazvhqqssy2jmyhdr4a2wlrbimt9qrhm&apikey=$YOUR_API_KEY"

# {"status":"1","message":"OK","result":"The proxy's (0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48) implementation contract is found at 0xa2327a938febf5fec13bacfb16ae10ecbc4cbdcf and is successfully updated."}

Then in your browser (or Get command):

https://api.etherscan.io/api?module=contract&action=getabi&address=0xa2327a938febf5fec13bacfb16ae10ecbc4cbdcf&apikey=<>

julian-CStack commented 1 year ago

I've found a solution for you, but I am not sure if this is the best possible way of doing it:

export YOUR_API_KEY=12345678
$ curl -d "address=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48" "https://api.etherscan.io/api?module=contract&action=verifyproxycontract&apikey=$YOUR_API_KEY"

# {"status":"1","message":"OK","result":"vbdpwlap3xapgkxvltjazvhqqssy2jmyhdr4a2wlrbimt9qrhm"}

$ curl "https://api.etherscan.io/api?module=contract&action=checkproxyverification&guid=vbdpwlap3xapgkxvltjazvhqqssy2jmyhdr4a2wlrbimt9qrhm&apikey=$YOUR_API_KEY"

# {"status":"1","message":"OK","result":"The proxy's (0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48) implementation contract is found at 0xa2327a938febf5fec13bacfb16ae10ecbc4cbdcf and is successfully updated."}

Then in your browser (or Get command):

https://api.etherscan.io/api?module=contract&action=getabi&address=0xa2327a938febf5fec13bacfb16ae10ecbc4cbdcf&apikey=<>

This should work. Will test. I was able to successfully grab the implementation address using the method described in the SE link in:

Still a bit new to working with eth contracts and I thought there might be a better way to fetch the ABI of the contract that the proxy contract points to. Looking it up manually is not an option so I will need to figure out some other way to fetch the correct ABI. Possibly like this: https://ethereum.stackexchange.com/questions/135333/is-it-possible-to-pull-proxy-contracts-through-an-ethereum-based-api

I closed the issue as its not really a web3dart issue but a user (me) error. Thanks for getting me pointed in the right direction. Much appreciated!

xclud commented 1 year ago

@julian-CStack Any time!