Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(algebra-integral): add mutex to protect timepoint storage #633

Merged
merged 6 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions pkg/liquidity-source/algebra/integral/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package integral
import (
"errors"
"fmt"
"sync"

"github.com/KyberNetwork/elastic-go-sdk/v2/utils"
"github.com/KyberNetwork/int256"
Expand All @@ -17,6 +18,7 @@ var (
)

type TimepointStorage struct {
mu sync.RWMutex
data map[uint16]Timepoint
}

Expand All @@ -27,6 +29,9 @@ func NewTimepointStorage(data map[uint16]Timepoint) *TimepointStorage {
}

func (s *TimepointStorage) Get(index uint16) Timepoint {
s.mu.RLock()
defer s.mu.RUnlock()

if v, ok := s.data[index]; ok {
logger.Debugf("access exists %v %v", index, v)
return v
Expand All @@ -45,6 +50,9 @@ func (s *TimepointStorage) Get(index uint16) Timepoint {
}
}
func (s *TimepointStorage) set(index uint16, v Timepoint) {
s.mu.Lock()
defer s.mu.Unlock()

s.data[index] = v
}

Expand Down
38 changes: 38 additions & 0 deletions pkg/liquidity-source/algebra/v1/abis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package algebrav1

import (
"bytes"

"github.com/ethereum/go-ethereum/accounts/abi"
)

var (
algebraV1PoolABI abi.ABI
algebraV1DirFeePoolABI abi.ABI
algebraV1DataStorageOperatorAPI abi.ABI
algebraV1DirFeeDataStorageOperatorAPI abi.ABI
erc20ABI abi.ABI
ticklensABI abi.ABI
)

func init() {
builder := []struct {
ABI *abi.ABI
data []byte
}{
{&algebraV1PoolABI, algebraV1PoolJson},
{&algebraV1DirFeePoolABI, algebraV1DirFeePoolJson},
{&algebraV1DataStorageOperatorAPI, algebraV1DataStorageOperatorJson},
{&algebraV1DirFeeDataStorageOperatorAPI, algebraV1DirFeeDataStorageOperatorJson},
{&erc20ABI, erc20Json},
{&ticklensABI, ticklensJson},
}

for _, b := range builder {
var err error
*b.ABI, err = abi.JSON(bytes.NewReader(b.data))
if err != nil {
panic(err)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
[
{ "inputs": [{ "internalType": "address", "name": "_pool", "type": "address" }], "stateMutability": "nonpayable", "type": "constructor" },
{
"anonymous": false,
"inputs": [
{
"components": [
{ "internalType": "uint16", "name": "alpha1", "type": "uint16" },
{ "internalType": "uint16", "name": "alpha2", "type": "uint16" },
{ "internalType": "uint32", "name": "beta1", "type": "uint32" },
{ "internalType": "uint32", "name": "beta2", "type": "uint32" },
{ "internalType": "uint16", "name": "gamma1", "type": "uint16" },
{ "internalType": "uint16", "name": "gamma2", "type": "uint16" },
{ "internalType": "uint32", "name": "volumeBeta", "type": "uint32" },
{ "internalType": "uint16", "name": "volumeGamma", "type": "uint16" },
{ "internalType": "uint16", "name": "baseFee", "type": "uint16" }
],
"indexed": false,
"internalType": "struct AdaptiveFee.Configuration",
"name": "feeConfig",
"type": "tuple"
}
],
"name": "FeeConfiguration",
"type": "event"
},
{
"inputs": [
{ "internalType": "uint128", "name": "liquidity", "type": "uint128" },
{ "internalType": "int256", "name": "amount0", "type": "int256" },
{ "internalType": "int256", "name": "amount1", "type": "int256" }
],
"name": "calculateVolumePerLiquidity",
"outputs": [{ "internalType": "uint128", "name": "volumePerLiquidity", "type": "uint128" }],
"stateMutability": "pure",
"type": "function"
},
{
"inputs": [
{
"components": [
{ "internalType": "uint16", "name": "alpha1", "type": "uint16" },
{ "internalType": "uint16", "name": "alpha2", "type": "uint16" },
{ "internalType": "uint32", "name": "beta1", "type": "uint32" },
{ "internalType": "uint32", "name": "beta2", "type": "uint32" },
{ "internalType": "uint16", "name": "gamma1", "type": "uint16" },
{ "internalType": "uint16", "name": "gamma2", "type": "uint16" },
{ "internalType": "uint32", "name": "volumeBeta", "type": "uint32" },
{ "internalType": "uint16", "name": "volumeGamma", "type": "uint16" },
{ "internalType": "uint16", "name": "baseFee", "type": "uint16" }
],
"internalType": "struct AdaptiveFee.Configuration",
"name": "_feeConfig",
"type": "tuple"
}
],
"name": "changeFeeConfiguration",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "feeConfig",
"outputs": [
{ "internalType": "uint16", "name": "alpha1", "type": "uint16" },
{ "internalType": "uint16", "name": "alpha2", "type": "uint16" },
{ "internalType": "uint32", "name": "beta1", "type": "uint32" },
{ "internalType": "uint32", "name": "beta2", "type": "uint32" },
{ "internalType": "uint16", "name": "gamma1", "type": "uint16" },
{ "internalType": "uint16", "name": "gamma2", "type": "uint16" },
{ "internalType": "uint32", "name": "volumeBeta", "type": "uint32" },
{ "internalType": "uint16", "name": "volumeGamma", "type": "uint16" },
{ "internalType": "uint16", "name": "baseFee", "type": "uint16" }
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{ "internalType": "uint32", "name": "time", "type": "uint32" },
{ "internalType": "int24", "name": "tick", "type": "int24" },
{ "internalType": "uint16", "name": "index", "type": "uint16" },
{ "internalType": "uint128", "name": "liquidity", "type": "uint128" }
],
"name": "getAverages",
"outputs": [
{ "internalType": "uint112", "name": "TWVolatilityAverage", "type": "uint112" },
{ "internalType": "uint256", "name": "TWVolumePerLiqAverage", "type": "uint256" }
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{ "internalType": "uint32", "name": "_time", "type": "uint32" },
{ "internalType": "int24", "name": "_tick", "type": "int24" },
{ "internalType": "uint16", "name": "_index", "type": "uint16" },
{ "internalType": "uint128", "name": "_liquidity", "type": "uint128" }
],
"name": "getFee",
"outputs": [{ "internalType": "uint16", "name": "fee", "type": "uint16" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{ "internalType": "uint32", "name": "time", "type": "uint32" },
{ "internalType": "uint32", "name": "secondsAgo", "type": "uint32" },
{ "internalType": "int24", "name": "tick", "type": "int24" },
{ "internalType": "uint16", "name": "index", "type": "uint16" },
{ "internalType": "uint128", "name": "liquidity", "type": "uint128" }
],
"name": "getSingleTimepoint",
"outputs": [
{ "internalType": "int56", "name": "tickCumulative", "type": "int56" },
{ "internalType": "uint160", "name": "secondsPerLiquidityCumulative", "type": "uint160" },
{ "internalType": "uint112", "name": "volatilityCumulative", "type": "uint112" },
{ "internalType": "uint256", "name": "volumePerAvgLiquidity", "type": "uint256" }
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{ "internalType": "uint32", "name": "time", "type": "uint32" },
{ "internalType": "uint32[]", "name": "secondsAgos", "type": "uint32[]" },
{ "internalType": "int24", "name": "tick", "type": "int24" },
{ "internalType": "uint16", "name": "index", "type": "uint16" },
{ "internalType": "uint128", "name": "liquidity", "type": "uint128" }
],
"name": "getTimepoints",
"outputs": [
{ "internalType": "int56[]", "name": "tickCumulatives", "type": "int56[]" },
{ "internalType": "uint160[]", "name": "secondsPerLiquidityCumulatives", "type": "uint160[]" },
{ "internalType": "uint112[]", "name": "volatilityCumulatives", "type": "uint112[]" },
{ "internalType": "uint256[]", "name": "volumePerAvgLiquiditys", "type": "uint256[]" }
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{ "internalType": "uint32", "name": "time", "type": "uint32" },
{ "internalType": "int24", "name": "tick", "type": "int24" }
],
"name": "initialize",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"name": "timepoints",
"outputs": [
{ "internalType": "bool", "name": "initialized", "type": "bool" },
{ "internalType": "uint32", "name": "blockTimestamp", "type": "uint32" },
{ "internalType": "int56", "name": "tickCumulative", "type": "int56" },
{ "internalType": "uint160", "name": "secondsPerLiquidityCumulative", "type": "uint160" },
{ "internalType": "uint88", "name": "volatilityCumulative", "type": "uint88" },
{ "internalType": "int24", "name": "averageTick", "type": "int24" },
{ "internalType": "uint144", "name": "volumePerLiquidityCumulative", "type": "uint144" }
],
"stateMutability": "view",
"type": "function"
},
{ "inputs": [], "name": "window", "outputs": [{ "internalType": "uint32", "name": "", "type": "uint32" }], "stateMutability": "pure", "type": "function" },
{
"inputs": [
{ "internalType": "uint16", "name": "index", "type": "uint16" },
{ "internalType": "uint32", "name": "blockTimestamp", "type": "uint32" },
{ "internalType": "int24", "name": "tick", "type": "int24" },
{ "internalType": "uint128", "name": "liquidity", "type": "uint128" },
{ "internalType": "uint128", "name": "volumePerLiquidity", "type": "uint128" }
],
"name": "write",
"outputs": [{ "internalType": "uint16", "name": "indexUpdated", "type": "uint16" }],
"stateMutability": "nonpayable",
"type": "function"
}
]
Loading
Loading