From 194b8221c127bd33289e8898b6ce102eeab71d25 Mon Sep 17 00:00:00 2001 From: Aleix Date: Mon, 29 Jan 2024 12:25:49 +0100 Subject: [PATCH] Detecting erc165 interface -> hasGovernanceEnabled flag in plugin --- packages/subgraph/abis/ERC20.json | 297 ++++++++++++++++++ .../manifest/subgraph.placeholder.yaml | 6 + packages/subgraph/schema.graphql | 1 + packages/subgraph/src/plugin/plugin.ts | 43 ++- 4 files changed, 346 insertions(+), 1 deletion(-) create mode 100644 packages/subgraph/abis/ERC20.json diff --git a/packages/subgraph/abis/ERC20.json b/packages/subgraph/abis/ERC20.json new file mode 100644 index 00000000..a784675d --- /dev/null +++ b/packages/subgraph/abis/ERC20.json @@ -0,0 +1,297 @@ +{ + "_format": "hh-sol-artifact-1", + "contractName": "ERC20", + "sourceName": "@openzeppelin/contracts/token/ERC20/ERC20.sol", + "abi": [ + { + "inputs": [ + { + "internalType": "string", + "name": "name_", + "type": "string" + }, + { + "internalType": "string", + "name": "symbol_", + "type": "string" + } + ], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "value", + "type": "uint256" + } + ], + "name": "Approval", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "value", + "type": "uint256" + } + ], + "name": "Transfer", + "type": "event" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "internalType": "address", + "name": "spender", + "type": "address" + } + ], + "name": "allowance", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "approve", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "account", + "type": "address" + } + ], + "name": "balanceOf", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "decimals", + "outputs": [ + { + "internalType": "uint8", + "name": "", + "type": "uint8" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "internalType": "uint256", + "name": "subtractedValue", + "type": "uint256" + } + ], + "name": "decreaseAllowance", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "internalType": "uint256", + "name": "addedValue", + "type": "uint256" + } + ], + "name": "increaseAllowance", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "name", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "symbol", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "totalSupply", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "transfer", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "transferFrom", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + } + ], + "bytecode": "0x60806040523480156200001157600080fd5b5060405162000bcb38038062000bcb83398101604081905262000034916200011f565b600362000042838262000218565b50600462000051828262000218565b505050620002e4565b634e487b7160e01b600052604160045260246000fd5b600082601f8301126200008257600080fd5b81516001600160401b03808211156200009f576200009f6200005a565b604051601f8301601f19908116603f01168101908282118183101715620000ca57620000ca6200005a565b81604052838152602092508683858801011115620000e757600080fd5b600091505b838210156200010b5785820183015181830184015290820190620000ec565b600093810190920192909252949350505050565b600080604083850312156200013357600080fd5b82516001600160401b03808211156200014b57600080fd5b620001598683870162000070565b935060208501519150808211156200017057600080fd5b506200017f8582860162000070565b9150509250929050565b600181811c908216806200019e57607f821691505b602082108103620001bf57634e487b7160e01b600052602260045260246000fd5b50919050565b601f8211156200021357600081815260208120601f850160051c81016020861015620001ee5750805b601f850160051c820191505b818110156200020f57828155600101620001fa565b5050505b505050565b81516001600160401b038111156200023457620002346200005a565b6200024c8162000245845462000189565b84620001c5565b602080601f8311600181146200028457600084156200026b5750858301515b600019600386901b1c1916600185901b1785556200020f565b600085815260208120601f198616915b82811015620002b55788860151825594840194600190910190840162000294565b5085821015620002d45787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b6108d780620002f46000396000f3fe608060405234801561001057600080fd5b50600436106100c95760003560e01c80633950935111610081578063a457c2d71161005b578063a457c2d714610187578063a9059cbb1461019a578063dd62ed3e146101ad57600080fd5b8063395093511461014357806370a082311461015657806395d89b411461017f57600080fd5b806318160ddd116100b257806318160ddd1461010f57806323b872dd14610121578063313ce5671461013457600080fd5b806306fdde03146100ce578063095ea7b3146100ec575b600080fd5b6100d66101e6565b6040516100e3919061074a565b60405180910390f35b6100ff6100fa3660046107b4565b610278565b60405190151581526020016100e3565b6002545b6040519081526020016100e3565b6100ff61012f3660046107de565b610292565b604051601281526020016100e3565b6100ff6101513660046107b4565b6102b6565b61011361016436600461081a565b6001600160a01b031660009081526020819052604090205490565b6100d66102f5565b6100ff6101953660046107b4565b610304565b6100ff6101a83660046107b4565b6103b3565b6101136101bb36600461083c565b6001600160a01b03918216600090815260016020908152604080832093909416825291909152205490565b6060600380546101f59061086f565b80601f01602080910402602001604051908101604052809291908181526020018280546102219061086f565b801561026e5780601f106102435761010080835404028352916020019161026e565b820191906000526020600020905b81548152906001019060200180831161025157829003601f168201915b5050505050905090565b6000336102868185856103c1565b60019150505b92915050565b6000336102a08582856104e5565b6102ab858585610577565b506001949350505050565b3360008181526001602090815260408083206001600160a01b038716845290915281205490919061028690829086906102f09087906108a9565b6103c1565b6060600480546101f59061086f565b3360008181526001602090815260408083206001600160a01b0387168452909152812054909190838110156103a65760405162461bcd60e51b815260206004820152602560248201527f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f7760448201527f207a65726f00000000000000000000000000000000000000000000000000000060648201526084015b60405180910390fd5b6102ab82868684036103c1565b600033610286818585610577565b6001600160a01b0383166104235760405162461bcd60e51b8152602060048201526024808201527f45524332303a20617070726f76652066726f6d20746865207a65726f206164646044820152637265737360e01b606482015260840161039d565b6001600160a01b0382166104845760405162461bcd60e51b815260206004820152602260248201527f45524332303a20617070726f766520746f20746865207a65726f206164647265604482015261737360f01b606482015260840161039d565b6001600160a01b0383811660008181526001602090815260408083209487168084529482529182902085905590518481527f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925910160405180910390a3505050565b6001600160a01b03838116600090815260016020908152604080832093861683529290522054600019811461057157818110156105645760405162461bcd60e51b815260206004820152601d60248201527f45524332303a20696e73756666696369656e7420616c6c6f77616e6365000000604482015260640161039d565b61057184848484036103c1565b50505050565b6001600160a01b0383166105f35760405162461bcd60e51b815260206004820152602560248201527f45524332303a207472616e736665722066726f6d20746865207a65726f20616460448201527f6472657373000000000000000000000000000000000000000000000000000000606482015260840161039d565b6001600160a01b0382166106555760405162461bcd60e51b815260206004820152602360248201527f45524332303a207472616e7366657220746f20746865207a65726f206164647260448201526265737360e81b606482015260840161039d565b6001600160a01b038316600090815260208190526040902054818110156106e45760405162461bcd60e51b815260206004820152602660248201527f45524332303a207472616e7366657220616d6f756e742065786365656473206260448201527f616c616e63650000000000000000000000000000000000000000000000000000606482015260840161039d565b6001600160a01b03848116600081815260208181526040808320878703905593871680835291849020805487019055925185815290927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef910160405180910390a3610571565b600060208083528351808285015260005b818110156107775785810183015185820160400152820161075b565b506000604082860101526040601f19601f8301168501019250505092915050565b80356001600160a01b03811681146107af57600080fd5b919050565b600080604083850312156107c757600080fd5b6107d083610798565b946020939093013593505050565b6000806000606084860312156107f357600080fd5b6107fc84610798565b925061080a60208501610798565b9150604084013590509250925092565b60006020828403121561082c57600080fd5b61083582610798565b9392505050565b6000806040838503121561084f57600080fd5b61085883610798565b915061086660208401610798565b90509250929050565b600181811c9082168061088357607f821691505b6020821081036108a357634e487b7160e01b600052602260045260246000fd5b50919050565b8082018082111561028c57634e487b7160e01b600052601160045260246000fdfea164736f6c6343000811000a", + "deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100c95760003560e01c80633950935111610081578063a457c2d71161005b578063a457c2d714610187578063a9059cbb1461019a578063dd62ed3e146101ad57600080fd5b8063395093511461014357806370a082311461015657806395d89b411461017f57600080fd5b806318160ddd116100b257806318160ddd1461010f57806323b872dd14610121578063313ce5671461013457600080fd5b806306fdde03146100ce578063095ea7b3146100ec575b600080fd5b6100d66101e6565b6040516100e3919061074a565b60405180910390f35b6100ff6100fa3660046107b4565b610278565b60405190151581526020016100e3565b6002545b6040519081526020016100e3565b6100ff61012f3660046107de565b610292565b604051601281526020016100e3565b6100ff6101513660046107b4565b6102b6565b61011361016436600461081a565b6001600160a01b031660009081526020819052604090205490565b6100d66102f5565b6100ff6101953660046107b4565b610304565b6100ff6101a83660046107b4565b6103b3565b6101136101bb36600461083c565b6001600160a01b03918216600090815260016020908152604080832093909416825291909152205490565b6060600380546101f59061086f565b80601f01602080910402602001604051908101604052809291908181526020018280546102219061086f565b801561026e5780601f106102435761010080835404028352916020019161026e565b820191906000526020600020905b81548152906001019060200180831161025157829003601f168201915b5050505050905090565b6000336102868185856103c1565b60019150505b92915050565b6000336102a08582856104e5565b6102ab858585610577565b506001949350505050565b3360008181526001602090815260408083206001600160a01b038716845290915281205490919061028690829086906102f09087906108a9565b6103c1565b6060600480546101f59061086f565b3360008181526001602090815260408083206001600160a01b0387168452909152812054909190838110156103a65760405162461bcd60e51b815260206004820152602560248201527f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f7760448201527f207a65726f00000000000000000000000000000000000000000000000000000060648201526084015b60405180910390fd5b6102ab82868684036103c1565b600033610286818585610577565b6001600160a01b0383166104235760405162461bcd60e51b8152602060048201526024808201527f45524332303a20617070726f76652066726f6d20746865207a65726f206164646044820152637265737360e01b606482015260840161039d565b6001600160a01b0382166104845760405162461bcd60e51b815260206004820152602260248201527f45524332303a20617070726f766520746f20746865207a65726f206164647265604482015261737360f01b606482015260840161039d565b6001600160a01b0383811660008181526001602090815260408083209487168084529482529182902085905590518481527f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925910160405180910390a3505050565b6001600160a01b03838116600090815260016020908152604080832093861683529290522054600019811461057157818110156105645760405162461bcd60e51b815260206004820152601d60248201527f45524332303a20696e73756666696369656e7420616c6c6f77616e6365000000604482015260640161039d565b61057184848484036103c1565b50505050565b6001600160a01b0383166105f35760405162461bcd60e51b815260206004820152602560248201527f45524332303a207472616e736665722066726f6d20746865207a65726f20616460448201527f6472657373000000000000000000000000000000000000000000000000000000606482015260840161039d565b6001600160a01b0382166106555760405162461bcd60e51b815260206004820152602360248201527f45524332303a207472616e7366657220746f20746865207a65726f206164647260448201526265737360e81b606482015260840161039d565b6001600160a01b038316600090815260208190526040902054818110156106e45760405162461bcd60e51b815260206004820152602660248201527f45524332303a207472616e7366657220616d6f756e742065786365656473206260448201527f616c616e63650000000000000000000000000000000000000000000000000000606482015260840161039d565b6001600160a01b03848116600081815260208181526040808320878703905593871680835291849020805487019055925185815290927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef910160405180910390a3610571565b600060208083528351808285015260005b818110156107775785810183015185820160400152820161075b565b506000604082860101526040601f19601f8301168501019250505092915050565b80356001600160a01b03811681146107af57600080fd5b919050565b600080604083850312156107c757600080fd5b6107d083610798565b946020939093013593505050565b6000806000606084860312156107f357600080fd5b6107fc84610798565b925061080a60208501610798565b9150604084013590509250925092565b60006020828403121561082c57600080fd5b61083582610798565b9392505050565b6000806040838503121561084f57600080fd5b61085883610798565b915061086660208401610798565b90509250929050565b600181811c9082168061088357607f821691505b6020821081036108a357634e487b7160e01b600052602260045260246000fd5b50919050565b8082018082111561028c57634e487b7160e01b600052601160045260246000fdfea164736f6c6343000811000a", + "linkReferences": {}, + "deployedLinkReferences": {} +} diff --git a/packages/subgraph/manifest/subgraph.placeholder.yaml b/packages/subgraph/manifest/subgraph.placeholder.yaml index bcf87624..7ab72248 100644 --- a/packages/subgraph/manifest/subgraph.placeholder.yaml +++ b/packages/subgraph/manifest/subgraph.placeholder.yaml @@ -27,6 +27,8 @@ dataSources: file: ./imported/PluginSetupProcessor.json - name: ERC165 file: ./abis/ERC165.json + - name: ERC20 + file: ./abis/ERC20.json eventHandlers: - event: InstallationPrepared(indexed address,indexed address,bytes32,indexed address,(uint8,uint16),bytes,address,(address[],(uint8,address,address,address,bytes32)[])) handler: handleInstallationPrepared @@ -59,6 +61,10 @@ templates: abis: - name: VocdoniVoting file: $PLUGIN_MODULE/artifacts/src/VocdoniVoting.sol/VocdoniVoting.json + - name: ERC165 + file: ./abis/ERC165.json + - name: ERC20 + file: ./abis/ERC20.json eventHandlers: - event: PluginSettingsUpdated(bool,indexed uint16,uint32,uint32,uint64,uint64,indexed address,indexed string,uint256) handler: handlePluginSettingsUpdated diff --git a/packages/subgraph/schema.graphql b/packages/subgraph/schema.graphql index 2b1918ac..bb7a7c0d 100644 --- a/packages/subgraph/schema.graphql +++ b/packages/subgraph/schema.graphql @@ -57,6 +57,7 @@ type Plugin implements PluginInstallation @entity { daoTokenAddress: String censusStrategyURI: Bytes minProposerVotingPower: BigInt + hasGovernanceEnabled: Boolean executionMultisigMembers: [String!] diff --git a/packages/subgraph/src/plugin/plugin.ts b/packages/subgraph/src/plugin/plugin.ts index 0bc28db5..fc5180e3 100644 --- a/packages/subgraph/src/plugin/plugin.ts +++ b/packages/subgraph/src/plugin/plugin.ts @@ -1,9 +1,10 @@ import {getPluginInstallationId} from '../../commons/ids'; +import {ERC20} from '../../generated/PluginSetupProcessor/ERC20'; +import {ERC165} from '../../generated/PluginSetupProcessor/ERC165'; import { Action, Dao, Plugin, - PluginMember, PluginProposal, PluginProposalMember, TallyElement, @@ -26,6 +27,13 @@ import { dataSource, } from '@graphprotocol/graph-ts'; +export const ERC165_INTERFACE_ID = '0x01ffc9a7'; +export const GOVERNANCE_SUPPORTED_INTERFACE_IDS: string[] = [ + '0xe90fb3f6', + '0xe90fb3f6', + '0x0f13099a', +]; + export function handlePluginSettingsUpdated( event: PluginSettingsUpdated ): void { @@ -59,6 +67,39 @@ export function handlePluginSettingsUpdated( pluginEntity.daoTokenAddress = event.params.daoTokenAddress.toHexString(); pluginEntity.minProposerVotingPower = event.params.minProposerVotingPower; pluginEntity.censusStrategyURI = event.params.censusStrategyURI; + + let hasGovernanceEnabled = false; + // If it's not ERC20 token, is not a governance-compatible + let token20Contract = ERC20.bind(event.params.daoTokenAddress); + let balanceOf = token20Contract.try_balanceOf(pluginAddress); + let totalSupply = token20Contract.try_totalSupply(); + if (balanceOf.reverted || totalSupply.reverted) { + hasGovernanceEnabled = false; + } else { + // It's a ERC20 token, check if it's governanceERC165 and has all the governance interfaces + let tokenContract = ERC165.bind(event.params.daoTokenAddress); + let supportsInterface = tokenContract.try_supportsInterface( + Bytes.fromHexString(ERC165_INTERFACE_ID) + ); + if (!supportsInterface.reverted) { + for ( + let index = 0; + index < GOVERNANCE_SUPPORTED_INTERFACE_IDS.length; + index++ + ) { + let interfaceId = GOVERNANCE_SUPPORTED_INTERFACE_IDS[index]; + supportsInterface = tokenContract.try_supportsInterface( + Bytes.fromHexString(interfaceId) + ); + if (!supportsInterface.reverted && supportsInterface.value) { + hasGovernanceEnabled = true; + break; + } + } + } + } + + pluginEntity.hasGovernanceEnabled = hasGovernanceEnabled; pluginEntity.save(); }