From 4fdd973e0f0d25a3305bb17436e6fb7ae0e1b6a9 Mon Sep 17 00:00:00 2001 From: Giovanni Sanchez <108043524+sisyphusSmiling@users.noreply.github.com> Date: Tue, 23 Apr 2024 17:42:22 -0500 Subject: [PATCH] add totalSupply util method & script --- .../contracts/bridge/FlowEVMBridgeUtils.cdc | 21 +++++++++++++++++++ cadence/scripts/utils/total_supply.cdc | 16 ++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 cadence/scripts/utils/total_supply.cdc diff --git a/cadence/contracts/bridge/FlowEVMBridgeUtils.cdc b/cadence/contracts/bridge/FlowEVMBridgeUtils.cdc index c7bd9e4d..99e0386e 100644 --- a/cadence/contracts/bridge/FlowEVMBridgeUtils.cdc +++ b/cadence/contracts/bridge/FlowEVMBridgeUtils.cdc @@ -445,6 +445,27 @@ contract FlowEVMBridgeUtils { return self.balanceOf(owner: owner, evmContractAddress: evmContractAddress) >= amount } + /// Retrieves the total supply of the ERC20 contract at the given EVM contract address. Reverts on EVM call failure. + /// + /// @param evmContractAddress: The EVM contract address to retrieve the total supply from + /// + /// @return the total supply of the ERC20 + /// + access(all) + fun totalSupply(evmContractAddress: EVM.EVMAddress): UInt256 { + let callResult = self.call( + signature: "totalSupply()", + targetEVMAddress: evmContractAddress, + args: [], + gasLimit: 60000, + value: 0.0 + ) + assert(callResult.status == EVM.Status.successful, message: "Call to ERC20.totalSupply() failed") + let decodedResult = EVM.decodeABI(types: [Type()], data: callResult.data) as! [AnyStruct] + assert(decodedResult.length == 1, message: "Invalid response length") + return decodedResult[0] as! UInt256 + } + /************************ Derivation Utils ************************/ diff --git a/cadence/scripts/utils/total_supply.cdc b/cadence/scripts/utils/total_supply.cdc new file mode 100644 index 00000000..d6991ed5 --- /dev/null +++ b/cadence/scripts/utils/total_supply.cdc @@ -0,0 +1,16 @@ +import "EVM" + +import "EVMUtils" +import "FlowEVMBridgeUtils" + +/// Retrieves the total supply of the ERC20 contract at the given EVM contract address. Reverts on EVM call failure. +/// +/// @param evmContractAddress: The EVM contract address to retrieve the total supply from +/// +/// @return the total supply of the ERC20 +/// +access(all) fun main(evmContractAddressHex: String): UInt256 { + let evmContractAddress = EVMUtils.getEVMAddressFromHexString(address: evmContractAddressHex) + ?? panic("Invalid EVM contract address hex: ".concat(evmContractAddressHex)) + return FlowEVMBridgeUtils.totalSupply(evmContractAddress: evmContractAddress) +}