Skip to content

Commit

Permalink
feat: convert crosschain FX to apundiai (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Jan 15, 2025
1 parent fab07e5 commit e8c7a53
Show file tree
Hide file tree
Showing 18 changed files with 129 additions and 66 deletions.
2 changes: 1 addition & 1 deletion app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func newDefAppGenesisByDenom(cdc codec.JSONCodec, moduleBasics module.BasicManag
genesis[m.Name()] = cdc.MustMarshalJSON(state)
case banktypes.ModuleName:
state := banktypes.DefaultGenesisState()
state.DenomMetadata = []banktypes.Metadata{fxtypes.NewFXMetaData()}
state.DenomMetadata = []banktypes.Metadata{fxtypes.NewDefaultMetadata()}

ethModuleInitCoins := sdk.NewCoins(sdk.NewCoin(denom, ethInitAmount))
state.Balances = append(state.Balances, banktypes.Balance{
Expand Down
47 changes: 26 additions & 21 deletions app/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"ibc_transfer_timeout_height": "20000",
"oracles": [],
"delegate_threshold": {
"denom": "FX",
"denom": "apundiai",
"amount": "10000000000000000000000"
},
"delegate_multiple": "10",
Expand Down Expand Up @@ -69,7 +69,7 @@
"ibc_transfer_timeout_height": "20000",
"oracles": [],
"delegate_threshold": {
"denom": "FX",
"denom": "apundiai",
"amount": "10000000000000000000000"
},
"delegate_multiple": "10",
Expand Down Expand Up @@ -111,32 +111,37 @@
"address": "cosmos1c602zv38ht8xu8u2qcmymyl55mcyvvjrzq9ur3",
"coins": [
{
"denom": "FX",
"denom": "apundiai",
"amount": "378600525462891000000000000"
}
]
}
],
"supply": [
{
"denom": "FX",
"denom": "apundiai",
"amount": "378600525462891000000000000"
}
],
"denom_metadata": [
{
"description": "The native staking token of the Function X",
"description": "The native staking token of the Pundi AIFX",
"denom_units": [
{
"denom": "FX",
"denom": "apundiai",
"exponent": 0,
"aliases": []
},
{
"denom": "pundiai",
"exponent": 18,
"aliases": []
}
],
"base": "FX",
"display": "FX",
"name": "Function X",
"symbol": "FX",
"base": "apundiai",
"display": "pundiai",
"name": "Pundi AIFX Token",
"symbol": "PUNDIAI",
"uri": "",
"uri_hash": ""
}
Expand All @@ -155,7 +160,7 @@
"ibc_transfer_timeout_height": "20000",
"oracles": [],
"delegate_threshold": {
"denom": "FX",
"denom": "apundiai",
"amount": "10000000000000000000000"
},
"delegate_multiple": "10",
Expand Down Expand Up @@ -194,7 +199,7 @@
"consensus": null,
"crisis": {
"constant_fee": {
"denom": "FX",
"denom": "apundiai",
"amount": "13333000000000000000000"
}
},
Expand Down Expand Up @@ -235,7 +240,7 @@
"ibc_transfer_timeout_height": "20000",
"oracles": [],
"delegate_threshold": {
"denom": "FX",
"denom": "apundiai",
"amount": "10000000000000000000000"
},
"delegate_multiple": "10",
Expand Down Expand Up @@ -273,7 +278,7 @@
"evm": {
"accounts": [],
"params": {
"evm_denom": "FX",
"evm_denom": "apundiai",
"enable_create": true,
"enable_call": true,
"extra_eips": [],
Expand Down Expand Up @@ -332,7 +337,7 @@
"params": {
"min_deposit": [
{
"denom": "FX",
"denom": "apundiai",
"amount": "10000000000000000000000"
}
],
Expand Down Expand Up @@ -412,7 +417,7 @@
"ibc_transfer_timeout_height": "20000",
"oracles": [],
"delegate_threshold": {
"denom": "FX",
"denom": "apundiai",
"amount": "10000000000000000000000"
},
"delegate_multiple": "10",
Expand Down Expand Up @@ -451,7 +456,7 @@
"annual_provisions": "0.000000000000000000"
},
"params": {
"mint_denom": "FX",
"mint_denom": "apundiai",
"inflation_rate_change": "0.300000000000000000",
"inflation_max": "0.416762000000000000",
"inflation_min": "0.170000000000000000",
Expand All @@ -471,7 +476,7 @@
"ibc_transfer_timeout_height": "20000",
"oracles": [],
"delegate_threshold": {
"denom": "FX",
"denom": "apundiai",
"amount": "10000000000000000000000"
},
"delegate_multiple": "10",
Expand Down Expand Up @@ -516,7 +521,7 @@
"ibc_transfer_timeout_height": "20000",
"oracles": [],
"delegate_threshold": {
"denom": "FX",
"denom": "apundiai",
"amount": "10000000000000000000000"
},
"delegate_multiple": "10",
Expand Down Expand Up @@ -565,7 +570,7 @@
"max_validators": 20,
"max_entries": 7,
"historical_entries": 20000,
"bond_denom": "FX",
"bond_denom": "apundiai",
"min_commission_rate": "0.000000000000000000"
},
"last_total_power": "0",
Expand Down Expand Up @@ -597,7 +602,7 @@
"ibc_transfer_timeout_height": "20000",
"oracles": [],
"delegate_threshold": {
"denom": "FX",
"denom": "apundiai",
"amount": "10000000000000000000000"
},
"delegate_multiple": "10",
Expand Down
9 changes: 5 additions & 4 deletions local-node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
set -eo pipefail

export FX_HOME=${FX_HOME:-"/tmp/fxcore"}
readonly default_denom=apundiai

if [[ "$1" == "init" ]]; then
if [ -d "$FX_HOME" ]; then
Expand All @@ -13,7 +14,7 @@ if [[ "$1" == "init" ]]; then
fi

# Initialize private validator, p2p, genesis, and application configuration files
fxcored init local --chain-id fxcore --default-denom FX
fxcored init local --chain-id fxcore --default-denom $default_denom

fxcored config config.toml rpc.cors_allowed_origins "*"
# open prometheus
Expand All @@ -39,11 +40,11 @@ if [[ "$1" == "init" ]]; then

echo "test test test test test test test test test test test junk" | fxcored keys add fx1 --recover
if [ -n "${2:-""}" ]; then
fxcored genesis add-genesis-account fx1 10004000000000000000000000FX
fxcored genesis add-genesis-account fx1 10004000000000000000000000$default_denom
else
fxcored genesis add-genesis-account fx1 4000000000000000000000FX
fxcored genesis add-genesis-account fx1 4000000000000000000000$default_denom
fi
fxcored genesis gentx fx1 100000000000000000000FX --chain-id=fxcore \
fxcored genesis gentx fx1 100000000000000000000$default_denom --chain-id=fxcore \
--gas="200000" \
--moniker="fx-validator" \
--commission-max-change-rate="0.01" \
Expand Down
2 changes: 1 addition & 1 deletion server/config/data/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"max-txs": -1
},
"min-retain-blocks": 0,
"minimum-gas-prices": "4000000000000FX",
"minimum-gas-prices": "4000000000000apundiai",
"pruning": "default",
"pruning-interval": "0",
"pruning-keep-recent": "0",
Expand Down
2 changes: 1 addition & 1 deletion testutil/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func NoSupplyGenesisState(myApp *app.App) app.GenesisState {

// reset supply
bankState := banktypes.DefaultGenesisState()
bankState.DenomMetadata = []banktypes.Metadata{fxtypes.NewFXMetaData()}
bankState.DenomMetadata = []banktypes.Metadata{fxtypes.NewDefaultMetadata()}
genesisState[banktypes.ModuleName] = cdc.MustMarshalJSON(bankState)

var govGenState govv1.GenesisState
Expand Down
26 changes: 19 additions & 7 deletions types/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ const (
AddressPrefix = "fx"
EnvPrefix = "FX"

DefaultDenom = "FX"
DenomUnit = 18
DefaultDenom = "apundiai"
DefaultSymbol = "PUNDIAI"
DenomUnit = 18

FXDenom = "FX"

AddrLen = 20
)
Expand All @@ -33,11 +36,12 @@ func init() {
// votingPower = delegateToken / sdk.PowerReduction -- sdk.TokensToConsensusPower(tokens Int)
sdk.DefaultPowerReduction = sdkmath.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil))

fxHome := os.ExpandEnv("$FX_HOME")
if len(fxHome) > 0 {
defaultNodeHome = fxHome
nodeHome := os.ExpandEnv("$FX_HOME")
if len(nodeHome) > 0 {
defaultNodeHome = nodeHome
return
}

userHomeDir, err := os.UserHomeDir()
if err != nil {
stdlog.Println("Failed to get home dir %2", err)
Expand Down Expand Up @@ -84,6 +88,14 @@ func VerifyAddressFormat(bz []byte) error {
return nil
}

func IsOriginDenom(denom string) bool {
return denom == DefaultDenom
func SwapAmount(amount sdkmath.Int) sdkmath.Int {
return amount.QuoRaw(100)
}

func SwapCoin(coin sdk.Coin) sdk.Coin {
if coin.Denom != FXDenom {
return coin
}
coin.Amount = SwapAmount(coin.Amount)
return coin
}
4 changes: 2 additions & 2 deletions types/constant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ func Test_NormalizeCoin(t *testing.T) {
coin := sdk.NewCoin(DefaultDenom, sdkmath.NewInt(1e18))
assert.Equal(t, coin, sdk.NormalizeCoin(coin))

myCoin, err := sdk.ParseCoinNormalized("1000000000000000000FX")
myCoin, err := sdk.ParseCoinNormalized("1000000000000000000" + DefaultDenom)
require.NoError(t, err)
assert.Equal(t, myCoin, sdk.NewCoin(DefaultDenom, sdkmath.NewInt(1e18)))

myCoin, err = sdk.ParseCoinNormalized("1FX")
myCoin, err = sdk.ParseCoinNormalized("1" + DefaultDenom)
require.NoError(t, err)
assert.Equal(t, myCoin, sdk.NewCoin(DefaultDenom, sdkmath.NewInt(1)))
}
18 changes: 11 additions & 7 deletions types/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

func NewFXMetaData() banktypes.Metadata {
func NewDefaultMetadata() banktypes.Metadata {
return banktypes.Metadata{
Description: "The native staking token of the Function X",
Description: "The native staking token of the Pundi AIFX",
DenomUnits: []*banktypes.DenomUnit{
{
Denom: DefaultDenom,
Exponent: 0,
},
{
Denom: strings.ToLower(DefaultSymbol),
Exponent: DenomUnit,
},
},
Base: DefaultDenom,
Display: DefaultDenom,
Name: "Function X",
Symbol: DefaultDenom,
Display: strings.ToLower(DefaultSymbol),
Name: "Pundi AIFX Token",
Symbol: DefaultSymbol,
}
}

func NewMetadata(name, symbol string, decimals uint32) banktypes.Metadata {
return banktypes.Metadata{
Description: "The crosschain token of the Function X",
Description: "The crosschain token of the Pundi AIFX",
DenomUnits: []*banktypes.DenomUnit{
{
Denom: strings.ToLower(symbol),
Expand All @@ -36,7 +40,7 @@ func NewMetadata(name, symbol string, decimals uint32) banktypes.Metadata {
},
},
Base: strings.ToLower(symbol),
Display: strings.ToLower(symbol),
Display: symbol,
Name: name,
Symbol: symbol,
}
Expand Down
14 changes: 14 additions & 0 deletions types/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package types

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewMetadata(t *testing.T) {
metadata := NewMetadata("ABC Token", "ABC", 18)
assert.NoError(t, metadata.Validate())

assert.NoError(t, NewDefaultMetadata().Validate())
}
8 changes: 2 additions & 6 deletions x/crosschain/keeper/bridge_call_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ func (k Keeper) BridgeCallExecuted(ctx sdk.Context, caller contract.Caller, msg

baseCoins := sdk.NewCoins()
for i, tokenAddr := range msg.TokenContracts {
bridgeToken, err := k.DepositBridgeToken(ctx, receiverAddr.Bytes(), msg.Amounts[i], tokenAddr)
if err != nil {
return err
}
baseCoin, err := k.BridgeTokenToBaseCoin(ctx, receiverAddr.Bytes(), msg.Amounts[i], bridgeToken)
baseCoin, err := k.DepositBridgeTokenToBaseCoin(ctx, receiverAddr.Bytes(), msg.Amounts[i], tokenAddr)
if err != nil {
return err
}
Expand Down Expand Up @@ -73,7 +69,7 @@ func (k Keeper) BridgeCallExecuted(ctx sdk.Context, caller contract.Caller, msg
}

for _, coin := range baseCoins {
if fxtypes.IsOriginDenom(coin.Denom) {
if coin.Denom == fxtypes.DefaultDenom {
continue
}
if _, err = k.erc20Keeper.BaseCoinToEvm(ctx, caller, msg.GetRefundAddr(), coin); err != nil {
Expand Down
6 changes: 1 addition & 5 deletions x/crosschain/keeper/bridge_call_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,7 @@ func (k Keeper) RefundOutgoingBridgeCall(ctx sdk.Context, caller contract.Caller
refund := fxtypes.ExternalAddrToAccAddr(k.moduleName, data.GetRefund())
baseCoins := sdk.NewCoins()
for _, token := range data.Tokens {
bridgeToken, err := k.DepositBridgeToken(ctx, refund.Bytes(), token.Amount, token.Contract)
if err != nil {
return err
}
baseCoin, err := k.BridgeTokenToBaseCoin(ctx, refund.Bytes(), token.Amount, bridgeToken)
baseCoin, err := k.DepositBridgeTokenToBaseCoin(ctx, refund.Bytes(), token.Amount, token.Contract)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions x/crosschain/keeper/bridge_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (
func (k Keeper) AddBridgeTokenExecuted(ctx sdk.Context, claim *types.MsgBridgeTokenClaim) error {
k.Logger(ctx).Info("add bridge token claim", "symbol", claim.Symbol, "token", claim.TokenContract)

if claim.Symbol == fxtypes.DefaultDenom {
if claim.Symbol == fxtypes.DefaultSymbol {
if uint64(fxtypes.DenomUnit) != claim.Decimals {
return types.ErrInvalid.Wrapf("%s denom decimals not match %d, expect %d",
fxtypes.DefaultDenom, claim.Decimals, fxtypes.DenomUnit)
}
return k.erc20Keeper.AddBridgeToken(ctx, claim.Symbol, k.moduleName, claim.TokenContract, false)
return k.erc20Keeper.AddBridgeToken(ctx, fxtypes.DefaultDenom, k.moduleName, claim.TokenContract, false)
}

return k.erc20Keeper.AddBridgeToken(ctx, strings.ToLower(claim.Symbol), k.moduleName, claim.TokenContract, false)
Expand Down
Loading

0 comments on commit e8c7a53

Please sign in to comment.