Skip to content

Commit

Permalink
add totalSupply util method & script
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Apr 23, 2024
1 parent 5cb0a4d commit 4fdd973
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cadence/contracts/bridge/FlowEVMBridgeUtils.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -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<UInt256>()], data: callResult.data) as! [AnyStruct]
assert(decodedResult.length == 1, message: "Invalid response length")
return decodedResult[0] as! UInt256
}

/************************
Derivation Utils
************************/
Expand Down
16 changes: 16 additions & 0 deletions cadence/scripts/utils/total_supply.cdc
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 4fdd973

Please sign in to comment.