Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion XDCx/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ func (XDCx *XDCX) GetTokenDecimal(chain consensus.ChainContext, statedb *state.S
return common.BasePrice, nil
}
var decimals uint8
relayerSMC := statedb.RelayerRegistrationSMC()
defer func() {
log.Debug("GetTokenDecimal from ", "relayerSMC", common.RelayerRegistrationSMC, "tokenAddr", tokenAddr.Hex(), "decimals", decimals)
log.Debug("GetTokenDecimal from ", "relayerSMC", relayerSMC, "tokenAddr", tokenAddr.Hex(), "decimals", decimals)
}()
contractABI, err := GetTokenAbi()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion XDCx/tradingstate/orderitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func IsValidRelayer(statedb *state.StateDB, address common.Address) bool {

locBigDeposit := new(big.Int).SetUint64(uint64(0)).Add(locRelayerState, RelayerStructMappingSlot["_deposit"])
locHashDeposit := common.BigToHash(locBigDeposit)
balance := statedb.GetState(common.RelayerRegistrationSMC, locHashDeposit).Big()
balance := statedb.GetState(statedb.RelayerRegistrationSMC(), locHashDeposit).Big()
if balance.Cmp(new(big.Int).Mul(common.BasePrice, common.RelayerLockedFund)) <= 0 {
log.Debug("Relayer is not in relayer list", "relayer", address, "balance", balance)
return false
Expand Down
44 changes: 24 additions & 20 deletions XDCx/tradingstate/relayer_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ func GetLocMappingAtKey(key common.Hash, slot uint64) *big.Int {
return ret
}

func relayerRegistrationSMC(statedb *state.StateDB) common.Address {
return statedb.RelayerRegistrationSMC()
}

func GetExRelayerFee(relayer common.Address, statedb *state.StateDB) *big.Int {
slot := RelayerMappingSlot["RELAYER_LIST"]
locBig := GetLocMappingAtKey(relayer.Hash(), slot)
locBig = new(big.Int).Add(locBig, RelayerStructMappingSlot["_fee"])
locHash := common.BigToHash(locBig)
return statedb.GetState(common.RelayerRegistrationSMC, locHash).Big()
return statedb.GetState(relayerRegistrationSMC(statedb), locHash).Big()
}

func GetRelayerOwner(relayer common.Address, statedb *state.StateDB) common.Address {
Expand All @@ -34,23 +38,23 @@ func GetRelayerOwner(relayer common.Address, statedb *state.StateDB) common.Addr
log.Debug("GetRelayerOwner", "relayer", relayer.Hex(), "slot", slot, "locBig", locBig)
locBig = new(big.Int).Add(locBig, RelayerStructMappingSlot["_owner"])
locHash := common.BigToHash(locBig)
return common.BytesToAddress(statedb.GetState(common.RelayerRegistrationSMC, locHash).Bytes())
return common.BytesToAddress(statedb.GetState(relayerRegistrationSMC(statedb), locHash).Bytes())
}

// return true if relayer request to resign and have not withdraw locked fund
func IsResignedRelayer(relayer common.Address, statedb *state.StateDB) bool {
slot := RelayerMappingSlot["RESIGN_REQUESTS"]
locBig := GetLocMappingAtKey(relayer.Hash(), slot)
locHash := common.BigToHash(locBig)
return statedb.GetState(common.RelayerRegistrationSMC, locHash) != (common.Hash{})
return statedb.GetState(relayerRegistrationSMC(statedb), locHash) != (common.Hash{})
}

func GetBaseTokenLength(relayer common.Address, statedb *state.StateDB) uint64 {
slot := RelayerMappingSlot["RELAYER_LIST"]
locBig := GetLocMappingAtKey(relayer.Hash(), slot)
locBig = new(big.Int).Add(locBig, RelayerStructMappingSlot["_fromTokens"])
locHash := common.BigToHash(locBig)
return statedb.GetState(common.RelayerRegistrationSMC, locHash).Big().Uint64()
return statedb.GetState(relayerRegistrationSMC(statedb), locHash).Big().Uint64()
}

func GetBaseTokenAtIndex(relayer common.Address, statedb *state.StateDB, index uint64) common.Address {
Expand All @@ -59,15 +63,15 @@ func GetBaseTokenAtIndex(relayer common.Address, statedb *state.StateDB, index u
locBig = new(big.Int).Add(locBig, RelayerStructMappingSlot["_fromTokens"])
locHash := common.BigToHash(locBig)
loc := state.GetLocDynamicArrAtElement(locHash, index, 1)
return common.BytesToAddress(statedb.GetState(common.RelayerRegistrationSMC, loc).Bytes())
return common.BytesToAddress(statedb.GetState(relayerRegistrationSMC(statedb), loc).Bytes())
}

func GetQuoteTokenLength(relayer common.Address, statedb *state.StateDB) uint64 {
slot := RelayerMappingSlot["RELAYER_LIST"]
locBig := GetLocMappingAtKey(relayer.Hash(), slot)
locBig = new(big.Int).Add(locBig, RelayerStructMappingSlot["_toTokens"])
locHash := common.BigToHash(locBig)
return statedb.GetState(common.RelayerRegistrationSMC, locHash).Big().Uint64()
return statedb.GetState(relayerRegistrationSMC(statedb), locHash).Big().Uint64()
}

func GetQuoteTokenAtIndex(relayer common.Address, statedb *state.StateDB, index uint64) common.Address {
Expand All @@ -76,13 +80,13 @@ func GetQuoteTokenAtIndex(relayer common.Address, statedb *state.StateDB, index
locBig = new(big.Int).Add(locBig, RelayerStructMappingSlot["_toTokens"])
locHash := common.BigToHash(locBig)
loc := state.GetLocDynamicArrAtElement(locHash, index, 1)
return common.BytesToAddress(statedb.GetState(common.RelayerRegistrationSMC, loc).Bytes())
return common.BytesToAddress(statedb.GetState(relayerRegistrationSMC(statedb), loc).Bytes())
}

func GetRelayerCount(statedb *state.StateDB) uint64 {
slot := RelayerMappingSlot["RelayerCount"]
slotHash := common.BigToHash(new(big.Int).SetUint64(slot))
valueHash := statedb.GetState(common.RelayerRegistrationSMC, slotHash)
valueHash := statedb.GetState(relayerRegistrationSMC(statedb), slotHash)
return new(big.Int).SetBytes(valueHash.Bytes()).Uint64()
}

Expand All @@ -91,7 +95,7 @@ func GetAllCoinbases(statedb *state.StateDB) []common.Address {
slot := RelayerMappingSlot["RELAYER_COINBASES"]
coinbases := []common.Address{}
for i := uint64(0); i < relayerCount; i++ {
valueHash := statedb.GetState(common.RelayerRegistrationSMC, common.BytesToHash(state.GetLocMappingAtKey(common.BigToHash(big.NewInt(int64(i))), slot).Bytes()))
valueHash := statedb.GetState(relayerRegistrationSMC(statedb), common.BytesToHash(state.GetLocMappingAtKey(common.BigToHash(big.NewInt(int64(i))), slot).Bytes()))
coinbases = append(coinbases, common.BytesToAddress(valueHash.Bytes()))
}
return coinbases
Expand All @@ -103,21 +107,21 @@ func GetAllTradingPairs(statedb *state.StateDB) (map[common.Hash]bool, error) {
for _, coinbase := range coinbases {
locBig := GetLocMappingAtKey(coinbase.Hash(), slot)
fromTokenSlot := new(big.Int).Add(locBig, RelayerStructMappingSlot["_fromTokens"])
fromTokenLength := statedb.GetState(common.RelayerRegistrationSMC, common.BigToHash(fromTokenSlot)).Big().Uint64()
fromTokenLength := statedb.GetState(relayerRegistrationSMC(statedb), common.BigToHash(fromTokenSlot)).Big().Uint64()
toTokenSlot := new(big.Int).Add(locBig, RelayerStructMappingSlot["_toTokens"])
toTokenLength := statedb.GetState(common.RelayerRegistrationSMC, common.BigToHash(toTokenSlot)).Big().Uint64()
toTokenLength := statedb.GetState(relayerRegistrationSMC(statedb), common.BigToHash(toTokenSlot)).Big().Uint64()
if toTokenLength != fromTokenLength {
return map[common.Hash]bool{}, fmt.Errorf("invalid length from token & to token: from :%d , to :%d ", fromTokenLength, toTokenLength)
}
fromTokens := []common.Address{}
fromTokenSlotHash := common.BytesToHash(fromTokenSlot.Bytes())
for i := uint64(0); i < fromTokenLength; i++ {
fromToken := common.BytesToAddress(statedb.GetState(common.RelayerRegistrationSMC, state.GetLocDynamicArrAtElement(fromTokenSlotHash, i, uint64(1))).Bytes())
fromToken := common.BytesToAddress(statedb.GetState(relayerRegistrationSMC(statedb), state.GetLocDynamicArrAtElement(fromTokenSlotHash, i, uint64(1))).Bytes())
fromTokens = append(fromTokens, fromToken)
}
toTokenSlotHash := common.BytesToHash(toTokenSlot.Bytes())
for i := uint64(0); i < toTokenLength; i++ {
toToken := common.BytesToAddress(statedb.GetState(common.RelayerRegistrationSMC, state.GetLocDynamicArrAtElement(toTokenSlotHash, i, uint64(1))).Bytes())
toToken := common.BytesToAddress(statedb.GetState(relayerRegistrationSMC(statedb), state.GetLocDynamicArrAtElement(toTokenSlotHash, i, uint64(1))).Bytes())

log.Debug("GetAllTradingPairs all pair info", "from", fromTokens[i].Hex(), "toToken", toToken.Hex())
allPairs[GetTradingOrderBookHash(fromTokens[i], toToken)] = true
Expand All @@ -133,14 +137,14 @@ func SubRelayerFee(relayer common.Address, fee *big.Int, statedb *state.StateDB)

locBigDeposit := new(big.Int).SetUint64(uint64(0)).Add(locBig, RelayerStructMappingSlot["_deposit"])
locHashDeposit := common.BigToHash(locBigDeposit)
balance := statedb.GetState(common.RelayerRegistrationSMC, locHashDeposit).Big()
balance := statedb.GetState(relayerRegistrationSMC(statedb), locHashDeposit).Big()
log.Debug("ApplyXDCXMatchedTransaction settle balance: SubRelayerFee BEFORE", "relayer", relayer, "balance", balance)
if balance.Cmp(fee) < 0 {
return errors.Errorf("relayer %s isn't enough XDC fee", relayer)
} else {
balance = new(big.Int).Sub(balance, fee)
statedb.SetState(common.RelayerRegistrationSMC, locHashDeposit, common.BigToHash(balance))
statedb.SubBalance(common.RelayerRegistrationSMC, fee, tracing.BalanceChangeUnspecified)
statedb.SetState(relayerRegistrationSMC(statedb), locHashDeposit, common.BigToHash(balance))
statedb.SubBalance(relayerRegistrationSMC(statedb), fee, tracing.BalanceChangeUnspecified)
log.Debug("ApplyXDCXMatchedTransaction settle balance: SubRelayerFee AFTER", "relayer", relayer, "balance", balance)
return nil
}
Expand All @@ -152,7 +156,7 @@ func CheckRelayerFee(relayer common.Address, fee *big.Int, statedb *state.StateD

locBigDeposit := new(big.Int).SetUint64(uint64(0)).Add(locBig, RelayerStructMappingSlot["_deposit"])
locHashDeposit := common.BigToHash(locBigDeposit)
balance := statedb.GetState(common.RelayerRegistrationSMC, locHashDeposit).Big()
balance := statedb.GetState(relayerRegistrationSMC(statedb), locHashDeposit).Big()
if new(big.Int).Sub(balance, fee).Cmp(new(big.Int).Mul(common.BasePrice, common.RelayerLockedFund)) < 0 {
return errors.Errorf("relayer %s isn't enough XDC fee : balance %d , fee : %d ", relayer.Hex(), balance.Uint64(), fee.Uint64())
}
Expand Down Expand Up @@ -296,7 +300,7 @@ func CheckSubRelayerFee(relayer common.Address, fee *big.Int, statedb *state.Sta
locBig := GetLocMappingAtKey(relayer.Hash(), slot)
locBigDeposit := new(big.Int).SetUint64(uint64(0)).Add(locBig, RelayerStructMappingSlot["_deposit"])
locHashDeposit := common.BigToHash(locBigDeposit)
balance = statedb.GetState(common.RelayerRegistrationSMC, locHashDeposit).Big()
balance = statedb.GetState(relayerRegistrationSMC(statedb), locHashDeposit).Big()
}
log.Debug("CheckSubRelayerFee settle balance: SubRelayerFee ", "relayer", relayer, "balance", balance, "fee", fee)
if balance.Cmp(fee) < 0 {
Expand Down Expand Up @@ -344,6 +348,6 @@ func SetSubRelayerFee(relayer common.Address, balance *big.Int, fee *big.Int, st
locBig := GetLocMappingAtKey(relayer.Hash(), slot)
locBigDeposit := new(big.Int).SetUint64(uint64(0)).Add(locBig, RelayerStructMappingSlot["_deposit"])
locHashDeposit := common.BigToHash(locBigDeposit)
statedb.SetState(common.RelayerRegistrationSMC, locHashDeposit, common.BigToHash(balance))
statedb.SubBalance(common.RelayerRegistrationSMC, fee, tracing.BalanceChangeUnspecified)
statedb.SetState(relayerRegistrationSMC(statedb), locHashDeposit, common.BigToHash(balance))
statedb.SubBalance(relayerRegistrationSMC(statedb), fee, tracing.BalanceChangeUnspecified)
}
44 changes: 24 additions & 20 deletions XDCxlending/lendingstate/lendingcontract.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ var (
}
)

func lendingRegistrationSMC(statedb *state.StateDB) common.Address {
return statedb.LendingRegistrationSMC()
}

// @function IsValidRelayer : return whether the given address is the coinbase of a valid relayer or not
// @param statedb : current state
// @param coinbase: coinbase address of relayer
Expand All @@ -45,7 +49,7 @@ func IsValidRelayer(statedb *state.StateDB, coinbase common.Address) bool {

// a valid relayer must have baseToken
locBaseToken := state.GetLocOfStructElement(locRelayerState, LendingRelayerStructSlots["bases"])
if v := statedb.GetState(common.LendingRegistrationSMC, common.BytesToHash(locBaseToken.Bytes())); v != (common.Hash{}) {
if v := statedb.GetState(lendingRegistrationSMC(statedb), common.BytesToHash(locBaseToken.Bytes())); v != (common.Hash{}) {
if tradingstate.IsResignedRelayer(coinbase, statedb) {
return false
}
Expand All @@ -54,7 +58,7 @@ func IsValidRelayer(statedb *state.StateDB, coinbase common.Address) bool {

locBigDeposit := new(big.Int).SetUint64(uint64(0)).Add(locRelayerStateTrading, tradingstate.RelayerStructMappingSlot["_deposit"])
locHashDeposit := common.BigToHash(locBigDeposit)
balance := statedb.GetState(common.RelayerRegistrationSMC, locHashDeposit).Big()
balance := statedb.GetState(statedb.RelayerRegistrationSMC(), locHashDeposit).Big()
expectedFund := new(big.Int).Mul(common.BasePrice, common.RelayerLockedFund)
if balance.Cmp(expectedFund) <= 0 {
log.Debug("Relayer is not in relayer list", "relayer", coinbase, "balance", balance, "expected", expectedFund)
Expand All @@ -72,7 +76,7 @@ func IsValidRelayer(statedb *state.StateDB, coinbase common.Address) bool {
func GetFee(statedb *state.StateDB, coinbase common.Address) *big.Int {
locRelayerState := state.GetLocMappingAtKey(coinbase.Hash(), LendingRelayerListSlot)
locHash := common.BytesToHash(new(big.Int).Add(locRelayerState, LendingRelayerStructSlots["fee"]).Bytes())
return statedb.GetState(common.LendingRegistrationSMC, locHash).Big()
return statedb.GetState(lendingRegistrationSMC(statedb), locHash).Big()
}

// @function GetBaseList
Expand All @@ -83,10 +87,10 @@ func GetBaseList(statedb *state.StateDB, coinbase common.Address) []common.Addre
baseList := []common.Address{}
locRelayerState := state.GetLocMappingAtKey(coinbase.Hash(), LendingRelayerListSlot)
locBaseHash := state.GetLocOfStructElement(locRelayerState, LendingRelayerStructSlots["bases"])
length := statedb.GetState(common.LendingRegistrationSMC, locBaseHash).Big().Uint64()
length := statedb.GetState(lendingRegistrationSMC(statedb), locBaseHash).Big().Uint64()
for i := uint64(0); i < length; i++ {
loc := state.GetLocDynamicArrAtElement(locBaseHash, i, 1)
addr := common.BytesToAddress(statedb.GetState(common.LendingRegistrationSMC, loc).Bytes())
addr := common.BytesToAddress(statedb.GetState(lendingRegistrationSMC(statedb), loc).Bytes())
if addr != (common.Address{}) {
baseList = append(baseList, addr)
}
Expand All @@ -102,10 +106,10 @@ func GetTerms(statedb *state.StateDB, coinbase common.Address) []uint64 {
terms := []uint64{}
locRelayerState := state.GetLocMappingAtKey(coinbase.Hash(), LendingRelayerListSlot)
locTermHash := state.GetLocOfStructElement(locRelayerState, LendingRelayerStructSlots["terms"])
length := statedb.GetState(common.LendingRegistrationSMC, locTermHash).Big().Uint64()
length := statedb.GetState(lendingRegistrationSMC(statedb), locTermHash).Big().Uint64()
for i := uint64(0); i < length; i++ {
loc := state.GetLocDynamicArrAtElement(locTermHash, i, 1)
t := statedb.GetState(common.LendingRegistrationSMC, loc).Big().Uint64()
t := statedb.GetState(lendingRegistrationSMC(statedb), loc).Big().Uint64()
if t != uint64(0) {
terms = append(terms, t)
}
Expand Down Expand Up @@ -163,10 +167,10 @@ func GetCollaterals(statedb *state.StateDB, coinbase common.Address, baseToken c

// if collaterals is not defined for the relayer, return default collaterals
locDefaultCollateralHash := state.GetLocSimpleVariable(DefaultCollateralSlot)
length := statedb.GetState(common.LendingRegistrationSMC, locDefaultCollateralHash).Big().Uint64()
length := statedb.GetState(lendingRegistrationSMC(statedb), locDefaultCollateralHash).Big().Uint64()
for i := uint64(0); i < length; i++ {
loc := state.GetLocDynamicArrAtElement(locDefaultCollateralHash, i, 1)
addr := common.BytesToAddress(statedb.GetState(common.LendingRegistrationSMC, loc).Bytes())
addr := common.BytesToAddress(statedb.GetState(lendingRegistrationSMC(statedb), loc).Bytes())
if addr != (common.Address{}) {
collaterals = append(collaterals, addr)
}
Expand All @@ -183,9 +187,9 @@ func GetCollateralDetail(statedb *state.StateDB, token common.Address) (depositR
locDepositRate := state.GetLocOfStructElement(collateralState, CollateralStructSlots["depositRate"])
locLiquidationRate := state.GetLocOfStructElement(collateralState, CollateralStructSlots["liquidationRate"])
locRecallRate := state.GetLocOfStructElement(collateralState, CollateralStructSlots["recallRate"])
depositRate = statedb.GetState(common.LendingRegistrationSMC, locDepositRate).Big()
liquidationRate = statedb.GetState(common.LendingRegistrationSMC, locLiquidationRate).Big()
recallRate = statedb.GetState(common.LendingRegistrationSMC, locRecallRate).Big()
depositRate = statedb.GetState(lendingRegistrationSMC(statedb), locDepositRate).Big()
liquidationRate = statedb.GetState(lendingRegistrationSMC(statedb), locLiquidationRate).Big()
recallRate = statedb.GetState(lendingRegistrationSMC(statedb), locRecallRate).Big()
return depositRate, liquidationRate, recallRate
}

Expand All @@ -197,8 +201,8 @@ func GetCollateralPrice(statedb *state.StateDB, collateralToken common.Address,
locCollateralPrice := common.BigToHash(new(big.Int).Add(new(big.Int).SetBytes(locLendingTokenPriceByte), PriceStructSlots["price"]))
locBlockNumber := common.BigToHash(new(big.Int).Add(new(big.Int).SetBytes(locLendingTokenPriceByte), PriceStructSlots["blockNumber"]))

price = statedb.GetState(common.LendingRegistrationSMC, locCollateralPrice).Big()
blockNumber = statedb.GetState(common.LendingRegistrationSMC, locBlockNumber).Big()
price = statedb.GetState(lendingRegistrationSMC(statedb), locCollateralPrice).Big()
blockNumber = statedb.GetState(lendingRegistrationSMC(statedb), locBlockNumber).Big()
return price, blockNumber
}

Expand All @@ -208,10 +212,10 @@ func GetCollateralPrice(statedb *state.StateDB, collateralToken common.Address,
func GetSupportedTerms(statedb *state.StateDB) []uint64 {
terms := []uint64{}
locSupportedTerm := state.GetLocSimpleVariable(SupportedTermSlot)
length := statedb.GetState(common.LendingRegistrationSMC, locSupportedTerm).Big().Uint64()
length := statedb.GetState(lendingRegistrationSMC(statedb), locSupportedTerm).Big().Uint64()
for i := uint64(0); i < length; i++ {
loc := state.GetLocDynamicArrAtElement(locSupportedTerm, i, 1)
t := statedb.GetState(common.LendingRegistrationSMC, loc).Big().Uint64()
t := statedb.GetState(lendingRegistrationSMC(statedb), loc).Big().Uint64()
if t != 0 {
terms = append(terms, t)
}
Expand All @@ -225,10 +229,10 @@ func GetSupportedTerms(statedb *state.StateDB) []uint64 {
func GetSupportedBaseToken(statedb *state.StateDB) []common.Address {
baseTokens := []common.Address{}
locSupportedBaseToken := state.GetLocSimpleVariable(SupportedBaseSlot)
length := statedb.GetState(common.LendingRegistrationSMC, locSupportedBaseToken).Big().Uint64()
length := statedb.GetState(lendingRegistrationSMC(statedb), locSupportedBaseToken).Big().Uint64()
for i := uint64(0); i < length; i++ {
loc := state.GetLocDynamicArrAtElement(locSupportedBaseToken, i, 1)
addr := common.BytesToAddress(statedb.GetState(common.LendingRegistrationSMC, loc).Bytes())
addr := common.BytesToAddress(statedb.GetState(lendingRegistrationSMC(statedb), loc).Bytes())
if addr != (common.Address{}) {
baseTokens = append(baseTokens, addr)
}
Expand All @@ -254,10 +258,10 @@ func GetAllCollateral(statedb *state.StateDB) []common.Address {
//}

locDefaultCollateralHash := state.GetLocSimpleVariable(DefaultCollateralSlot)
length := statedb.GetState(common.LendingRegistrationSMC, locDefaultCollateralHash).Big().Uint64()
length := statedb.GetState(lendingRegistrationSMC(statedb), locDefaultCollateralHash).Big().Uint64()
for i := uint64(0); i < length; i++ {
loc := state.GetLocDynamicArrAtElement(locDefaultCollateralHash, i, 1)
addr := common.BytesToAddress(statedb.GetState(common.LendingRegistrationSMC, loc).Bytes())
addr := common.BytesToAddress(statedb.GetState(lendingRegistrationSMC(statedb), loc).Bytes())
if addr != (common.Address{}) {
collaterals = append(collaterals, addr)
}
Expand Down
Loading
Loading