Skip to content

Commit

Permalink
tests: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adu-web3 committed Dec 18, 2024
1 parent d80281b commit 683f8d0
Show file tree
Hide file tree
Showing 13 changed files with 840 additions and 63 deletions.
33 changes: 33 additions & 0 deletions precompiles/assets/IAssets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ address constant ASSETS_PRECOMPILE_ADDRESS = 0x000000000000000000000000000000000
IAssets constant ASSETS_CONTRACT = IAssets(ASSETS_PRECOMPILE_ADDRESS);

/// @dev The TokenInfo struct.
/// @param name The name of the token
/// @param symbol The symbol of the token
/// @param clientChainID The client chain ID
/// @param tokenID The token ID, typically the token address encoded in bytes
/// @param decimals The number of decimals of the token
/// @param totalStaked The total staked amount of the token
struct TokenInfo {
string name;
string symbol;
Expand All @@ -17,6 +23,26 @@ struct TokenInfo {
uint256 totalStaked;
}

/// @dev The StakerBalance struct.
/// @param clientChainID The client chain ID
/// @param stakerAddress The staker address, typically the staker's address encoded in bytes
/// @param tokenID The token ID, typically the token address encoded in bytes
/// @param balance The balance of the staker, balance = withdrawable + delegated + pendingUndelegated
/// @param withdrawable The withdrawable balance
/// @param delegated The delegated balance
/// @param pendingUndelegated The pending undelegated balance, during the unboding period and would become withdrawable after the unboding period
/// @param totalDeposited The total deposited balance
struct StakerBalance {
uint32 clientChainID;
bytes stakerAddress;
bytes tokenID;
uint256 balance;
uint256 withdrawable;
uint256 delegated;
uint256 pendingUndelegated;
uint256 totalDeposited;
}

/// @author Exocore Team
/// @title Assets Precompile Contract
/// @dev The interface through which solidity contracts will interact with assets module
Expand Down Expand Up @@ -156,6 +182,13 @@ interface IAssets {
/// @return success true if the query is successful
/// @return assetInfo the asset info
function getTokenInfo(uint32 clientChainId, bytes calldata tokenId) external view returns (bool success, TokenInfo memory assetInfo);

/// @dev Returns the staker's balance for a given token.
/// @param clientChainId is the ID of the client chain
/// @param tokenId is the ID of the token, typically the token address
/// @return success true if the query is successful
/// @return stakerBalance the staker's balance
function getStakerBalanceByToken(uint32 clientChainId, bytes calldata stakerAddress, bytes calldata tokenId) external view returns (bool success, StakerBalance memory stakerBalance);
}


76 changes: 76 additions & 0 deletions precompiles/assets/abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,82 @@
],
"stateMutability": "view"
},
{
"type": "function",
"name": "getStakerBalanceByToken",
"inputs": [
{
"name": "clientChainId",
"type": "uint32",
"internalType": "uint32"
},
{
"name": "stakerAddress",
"type": "bytes",
"internalType": "bytes"
},
{
"name": "tokenId",
"type": "bytes",
"internalType": "bytes"
}
],
"outputs": [
{
"name": "success",
"type": "bool",
"internalType": "bool"
},
{
"name": "stakerBalance",
"type": "tuple",
"internalType": "struct StakerBalance",
"components": [
{
"name": "clientChainID",
"type": "uint32",
"internalType": "uint32"
},
{
"name": "stakerAddress",
"type": "bytes",
"internalType": "bytes"
},
{
"name": "tokenID",
"type": "bytes",
"internalType": "bytes"
},
{
"name": "balance",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "withdrawable",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "delegated",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "pendingUndelegated",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "totalDeposited",
"type": "uint256",
"internalType": "uint256"
}
]
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "getTokenInfo",
Expand Down
24 changes: 24 additions & 0 deletions precompiles/assets/abi_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,27 @@ func NewEmptyTokenInfo() TokenInfo {
TotalStaked: big.NewInt(0),
}
}

type StakerBalance struct {
ClientChainID uint32
StakerAddress []byte
TokenID []byte
Balance *big.Int
Withdrawable *big.Int
Delegated *big.Int
PendingUndelegated *big.Int
TotalDeposited *big.Int
}

func NewEmptyStakerBalance() StakerBalance {
return StakerBalance{
ClientChainID: 0,
StakerAddress: []byte{},
TokenID: []byte{},
Balance: big.NewInt(0),
Withdrawable: big.NewInt(0),
Delegated: big.NewInt(0),
PendingUndelegated: big.NewInt(0),
TotalDeposited: big.NewInt(0),
}
}
8 changes: 7 additions & 1 deletion precompiles/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) (bz [
ctx.Logger().Error("internal error when calling assets precompile", "module", "assets precompile", "method", method.Name, "err", err)
bz, err = method.Outputs.Pack(false, NewEmptyTokenInfo())
}
case MethodGetStakerBalanceByToken:
bz, err = p.GetStakerBalanceByToken(ctx, method, args)
if err != nil {
ctx.Logger().Error("internal error when calling assets precompile", "module", "assets precompile", "method", method.Name, "err", err)
bz, err = method.Outputs.Pack(false, NewEmptyStakerBalance())
}
default:
return nil, fmt.Errorf(cmn.ErrUnknownMethod, method.Name)
}
Expand All @@ -186,7 +192,7 @@ func (Precompile) IsTransaction(methodID string) bool {
MethodRegisterOrUpdateClientChain,
MethodRegisterToken, MethodUpdateToken, MethodUpdateAuthorizedGateways:
return true
case MethodGetClientChains, MethodIsRegisteredClientChain, MethodIsAuthorizedGateway, MethodGetTokenInfo:
case MethodGetClientChains, MethodIsRegisteredClientChain, MethodIsAuthorizedGateway, MethodGetTokenInfo, MethodGetStakerBalanceByToken:
return false
default:
return false
Expand Down
Loading

0 comments on commit 683f8d0

Please sign in to comment.