utils

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2024 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// BatchCreateUserOpsCounts = 864
	AccountTreeDepth = 28
	AssetCounts      = 500
	// TierCount: must be even number, the cex assets commitment will depend on the TierCount/2 parts
	TierCount     = 12
	R1csBatchSize = 1000000
)

Variables

View Source
var (
	ZeroBigInt                    = new(big.Int).SetInt64(0)
	OneBigInt                     = new(big.Int).SetInt64(1)
	PercentageMultiplier          = new(big.Int).SetUint64(100)
	MaxTierBoundaryValue, _       = new(big.Int).SetString("332306998946228968225951765070086144", 10) // (pow(2,118))
	Uint64MaxValueBigInt, _       = new(big.Int).SetString("18446744073709551616", 10)
	Uint64MaxValueBigIntSquare, _ = new(big.Int).SetString("340282366920938463463374607431768211456", 10)
	Uint8MaxValueBigInt, _        = new(big.Int).SetString("256", 10)
	Uint16MaxValueBigInt, _       = new(big.Int).SetString("65536", 10)
	Uint126MaxValueBigInt, _      = new(big.Int).SetString("85070591730234615865843651857942052864", 10)
	Uint134MaxValueBigInt, _      = new(big.Int).SetString("21778071482940061661655974875633165533184", 10)
	Uint64MaxValueFr              = new(fr.Element).SetBigInt(Uint64MaxValueBigInt)
	Uint64MaxValueFrSquare        = new(fr.Element).SetBigInt(Uint64MaxValueBigIntSquare)
	Uint8MaxValueFr               = new(fr.Element).SetBigInt(Uint8MaxValueBigInt)
	Uint16MaxValueFr              = new(fr.Element).SetBigInt(Uint16MaxValueBigInt)
	Uint126MaxValueFr             = new(fr.Element).SetBigInt(Uint126MaxValueBigInt)
	Uint134MaxValueFr             = new(fr.Element).SetBigInt(Uint134MaxValueBigInt)
	MaxTierBoundaryValueFr        = new(fr.Element).SetBigInt(MaxTierBoundaryValue)
	PercentageMultiplierFr        = new(fr.Element).SetBigInt(PercentageMultiplier)

	AssetTypeForTwoDigits = map[string]bool{
		"BTTC":     true,
		"bttc":     true,
		"SHIB":     true,
		"shib":     true,
		"LUNC":     true,
		"lunc":     true,
		"XEC":      true,
		"xec":      true,
		"WIN":      true,
		"win":      true,
		"BIDR":     true,
		"bidr":     true,
		"SPELL":    true,
		"spell":    true,
		"HOT":      true,
		"hot":      true,
		"DOGE":     true,
		"doge":     true,
		"PEPE":     true,
		"pepe":     true,
		"FLOKI":    true,
		"floki":    true,
		"IDRT":     true,
		"idrt":     true,
		"DOGS":     true,
		"dogs":     true,
		"BONK":     true,
		"bonk":     true,
		"1000SATS": true,
		"1000sats": true,
		"NEIRO":    true,
		"neiro":    true,
	}
	// the key is the number of assets user own
	// the value is the number of batch create user ops
	BatchCreateUserOpsCountsTiers = map[int]int{
		500: 92,
		50:  700,
	}
	AssetCountsTiers = make([]int, 0)

	// one Fr element is 252 bits, it contains 16 16-bit elements at most
	PowersOfSixteenBits [15]fr.Element
)
View Source
var (
	DbErrSqlOperation = errors.New("unknown sql operation error")
	DbErrNotFound     = errors.New("sql: no rows in result set")
)
View Source
var (
	NilAccountHash []byte
)

Functions

func AccountInfoToHash

func AccountInfoToHash(account *AccountInfo, hasher *hash.Hash) []byte

func CalculateAssetValueForCollateral added in v1.1.0

func CalculateAssetValueForCollateral(loan uint64, margin uint64, portfolioMargin uint64, cexAssetInfo *CexAssetInfo) *big.Int

func CalculateAssetValueViaTiersRatio added in v1.1.0

func CalculateAssetValueViaTiersRatio(collateralValue *big.Int, tiersRatio []TierRatio) *big.Int

func CalculatePrecomputedValue added in v1.1.0

func CalculatePrecomputedValue(tiersRatio []TierRatio)

func ComputeCexAssetsCommitment

func ComputeCexAssetsCommitment(cexAssetsInfo []CexAssetInfo) []byte

func ComputeUserAssetsCommitment

func ComputeUserAssetsCommitment(hasher *hash.Hash, assets []AccountAsset) []byte

func ConvertAssetInfoToBytes

func ConvertAssetInfoToBytes(value any) [][]byte

func ConvertFloatStrToUint64

func ConvertFloatStrToUint64(f string, multiplier int64) (uint64, error)

func ConvertTierRatiosToBytes added in v1.1.0

func ConvertTierRatiosToBytes(tiersRatio []TierRatio) [][]byte

func GetAssetsCountOfUser added in v1.1.0

func GetAssetsCountOfUser(assets []AccountAsset) int

func GetMysqlSource added in v1.0.2

func GetMysqlSource(source string, secretId string) (string, error)

user name can't include ":"

func GetNonEmptyAssetsCountOfUser added in v1.1.0

func GetNonEmptyAssetsCountOfUser(assets []AccountAsset) int

func GetSecretFromAws

func GetSecretFromAws(secretId string) (string, error)

func IsAssetEmpty added in v1.1.0

func IsAssetEmpty(ua *AccountAsset) bool

func NewAccountTree

func NewAccountTree(driver string, addr string) (accountTree bsmt.SparseMerkleTree, err error)

func PaddingAccountAssets added in v1.1.0

func PaddingAccountAssets(assets []AccountAsset) (paddingFlattenAssets []uint64)

func ParseAssetIndexFromUserFile added in v1.1.0

func ParseAssetIndexFromUserFile(userFilename string) ([]string, error)

func ReadUserDataFromCsvFile

func ReadUserDataFromCsvFile(name string, cexAssetsInfo []CexAssetInfo) (map[int][]AccountInfo, int, error)

func SafeAdd

func SafeAdd(a uint64, b uint64) (c uint64)

func SelectAssetValue

func SelectAssetValue(expectAssetIndex int, flag int, currentAssetPosition int, assets []AccountAsset) (*big.Int, bool)

func VerifyMerkleProof

func VerifyMerkleProof(root []byte, accountIndex uint32, proof [][]byte, node []byte) bool

Types

type AccountAsset

type AccountAsset struct {
	Index           uint16
	Equity          uint64
	Debt            uint64
	Loan            uint64
	Margin          uint64
	PortfolioMargin uint64
}

type AccountInfo

type AccountInfo struct {
	AccountIndex    uint32
	AccountId       []byte
	TotalEquity     *big.Int
	TotalDebt       *big.Int
	TotalCollateral *big.Int
	Assets          []AccountAsset
}

func PaddingAccounts added in v1.1.0

func PaddingAccounts(accounts []AccountInfo, assetKey int, paddingStartIndex int) (int, []AccountInfo)

type BatchCreateUserWitness

type BatchCreateUserWitness struct {
	BatchCommitment           []byte
	BeforeAccountTreeRoot     []byte
	AfterAccountTreeRoot      []byte
	BeforeCEXAssetsCommitment []byte
	AfterCEXAssetsCommitment  []byte

	BeforeCexAssets []CexAssetInfo
	CreateUserOps   []CreateUserOperation
}

func DecodeBatchWitness

func DecodeBatchWitness(data string) *BatchCreateUserWitness

type CexAssetInfo

type CexAssetInfo struct {
	TotalEquity               uint64
	TotalDebt                 uint64
	BasePrice                 uint64
	Symbol                    string
	Index                     uint32
	LoanCollateral            uint64
	MarginCollateral          uint64
	PortfolioMarginCollateral uint64
	LoanRatios                [TierCount]TierRatio
	MarginRatios              [TierCount]TierRatio
	PortfolioMarginRatios     [TierCount]TierRatio
}

func ParseCexAssetInfoFromFile added in v1.1.0

func ParseCexAssetInfoFromFile(name string, assetIndexes []string) ([]CexAssetInfo, error)

func ParseUserDataSet

func ParseUserDataSet(dirname string) (map[int][]AccountInfo, []CexAssetInfo, error)

func RecoverAfterCexAssets

func RecoverAfterCexAssets(witness *BatchCreateUserWitness) []CexAssetInfo

type CreateUserOperation

type CreateUserOperation struct {
	BeforeAccountTreeRoot []byte
	AfterAccountTreeRoot  []byte
	Assets                []AccountAsset
	AccountIndex          uint32
	AccountIdHash         []byte
	AccountProof          [AccountTreeDepth][]byte
}

type TierRatio added in v1.1.0

type TierRatio struct {
	BoundaryValue    *big.Int
	Ratio            uint8
	PrecomputedValue *big.Int
}

func PaddingTierRatios added in v1.1.0

func PaddingTierRatios(tiersRatio []TierRatio) (res [TierCount]TierRatio)

func ParseTiersRatioFromStr added in v1.1.0

func ParseTiersRatioFromStr(tiersRatioEnc string) ([TierCount]TierRatio, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL