Skip to content

Commit

Permalink
baseSlot for collateral idx*3
Browse files Browse the repository at this point in the history
  • Loading branch information
atvanguard committed Sep 27, 2023
1 parent 8f3dbc7 commit 703c5ed
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
7 changes: 4 additions & 3 deletions precompile/contracts/bibliophile/margin_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ func GetCollaterals(stateDB contract.StateDB) []hu.Collateral {
}

func getCollateralCount(stateDB contract.StateDB) uint8 {
rawVal := stateDB.GetState(common.HexToAddress(MARGIN_ACCOUNT_GENESIS_ADDRESS), common.BytesToHash(common.LeftPadBytes(big.NewInt(SUPPORTED_COLLATERAL_SLOT).Bytes(), 32)))
rawVal := stateDB.GetState(common.HexToAddress(MARGIN_ACCOUNT_GENESIS_ADDRESS), common.BigToHash(big.NewInt(SUPPORTED_COLLATERAL_SLOT)))
return uint8(new(big.Int).SetBytes(rawVal.Bytes()).Uint64())
}

func getCollateralAt(stateDB contract.StateDB, idx uint8) hu.Collateral {
baseSlot := hu.Add(collateralStorageSlot(), big.NewInt(int64(idx)))
// struct Collateral { IERC20 token; uint weight; uint8 decimals; }
baseSlot := hu.Add(collateralStorageSlot(), big.NewInt(int64(idx)*3)) // collateral struct size = 3 * 32 bytes
tokenAddress := common.BytesToAddress(stateDB.GetState(common.HexToAddress(MARGIN_ACCOUNT_GENESIS_ADDRESS), common.BigToHash(baseSlot)).Bytes())
return hu.Collateral{
Weight: stateDB.GetState(common.HexToAddress(MARGIN_ACCOUNT_GENESIS_ADDRESS), common.BigToHash(hu.Add(baseSlot, big.NewInt(1)))).Big(),
Expand All @@ -78,5 +79,5 @@ func getCollateralAt(stateDB contract.StateDB, idx uint8) hu.Collateral {
}

func collateralStorageSlot() *big.Int {
return new(big.Int).SetBytes(crypto.Keccak256(common.LeftPadBytes(big.NewInt(SUPPORTED_COLLATERAL_SLOT).Bytes(), 32)))
return new(big.Int).SetBytes(crypto.Keccak256(common.BigToHash(big.NewInt(SUPPORTED_COLLATERAL_SLOT)).Bytes()))
}
9 changes: 3 additions & 6 deletions precompile/contracts/bibliophile/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func getUnderlyingPrice_(stateDB contract.StateDB, underlying common.Address) *b
return redstonePrice
}
// red stone oracle is not enabled for this market, we use the default TestOracle
slot := crypto.Keccak256(append(common.LeftPadBytes(underlying.Bytes(), 32), common.LeftPadBytes(big.NewInt(TEST_ORACLE_PRICES_MAPPING_SLOT).Bytes(), 32)...))
slot := crypto.Keccak256(append(common.LeftPadBytes(underlying.Bytes(), 32), common.BigToHash(big.NewInt(TEST_ORACLE_PRICES_MAPPING_SLOT)).Bytes()...))
return fromTwosComplement(stateDB.GetState(oracle, common.BytesToHash(slot)).Bytes())
}

Expand All @@ -51,9 +51,6 @@ func getlatestRoundId(stateDB contract.StateDB, adapterAddress common.Address) *
}

func getRedStoneFeedId(stateDB contract.StateDB, oracle, underlying common.Address) common.Hash {
return stateDB.GetState(oracle, common.BytesToHash(aggregatorMapSlot(underlying)))
}

func aggregatorMapSlot(underlying common.Address) []byte {
return crypto.Keccak256(append(common.LeftPadBytes(underlying.Bytes(), 32), common.LeftPadBytes(big.NewInt(AGGREGATOR_MAP_SLOT).Bytes(), 32)...))
aggregatorMapSlot := crypto.Keccak256(append(common.LeftPadBytes(underlying.Bytes(), 32), common.BigToHash(big.NewInt(AGGREGATOR_MAP_SLOT)).Bytes()...))
return stateDB.GetState(oracle, common.BytesToHash(aggregatorMapSlot))
}

0 comments on commit 703c5ed

Please sign in to comment.