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

New shape for AMM #2684

Merged
merged 1 commit into from
Sep 2, 2024
Merged
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
34 changes: 24 additions & 10 deletions common/ethereum/amm/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import (
"fmt"
"math/big"

ethAbi "github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
ethCommon "github.com/fluidity-money/fluidity-app/common/ethereum"
"github.com/fluidity-money/fluidity-app/lib/types/ethereum"
ethTypes "github.com/fluidity-money/fluidity-app/lib/types/ethereum"
"github.com/fluidity-money/fluidity-app/lib/types/misc"

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

const ammAbiString = `[
{
"anonymous": false,
"inputs": [
{ "indexed": true, "internalType": "uint256", "name": "id", "type": "uint256" },
{ "indexed": false, "internalType": "int128", "name": "delta", "type": "int128" }
{ "indexed": false, "internalType": "int256", "name": "token0", "type": "int256" },
{ "indexed": false, "internalType": "int256", "name": "token1", "type": "int256" }
],
"name": "UpdatePositionLiquidity",
"type": "event"
Expand Down Expand Up @@ -82,13 +84,14 @@ var AmmAbi ethAbi.ABI
type (
AmmEventPositionMint struct {
Id misc.BigInt `json:"id"`
Pool ethereum.Address `json:"pool"`
Lower int32 `json:"lower_tick"`
Upper int32 `json:"upper_tick"`
Pool ethereum.Address `json:"pool"`
}
AmmEventPositionUpdate struct {
Id misc.BigInt `json:"id"`
Delta misc.BigInt `json:"liquidity_delta"`
Id misc.BigInt `json:"id"`
Token0 misc.BigInt `json:"token0"`
Token1 misc.BigInt `json:"token1"`
}
AmmEventCollectFees struct {
Id misc.BigInt
Expand Down Expand Up @@ -187,15 +190,26 @@ func DecodeUpdatePosition(log ethTypes.Log) (update AmmEventPositionUpdate, err
return
}

delta, err := ethCommon.CoerceBoundContractResultsToInt(data)
token0, ok := data[0].(*big.Int)

if !ok {
err = fmt.Errorf("Failed to decode token0! %T", data[0])

return
}

token1, ok := data[1].(*big.Int)

if !ok {
err = fmt.Errorf("Failed to decode token1! %T", data[1])

if err != nil {
return
}

update = AmmEventPositionUpdate{
Id: misc.NewBigIntFromInt(*id),
Delta: misc.NewBigIntFromInt(*delta),
Id: misc.NewBigIntFromInt(*id),
Token0: misc.NewBigIntFromInt(*token0),
Token1: misc.NewBigIntFromInt(*token1),
}

return update, nil
Expand Down
48 changes: 48 additions & 0 deletions common/ethereum/amm/abi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package amm

import (
"testing"
"encoding/hex"

"github.com/fluidity-money/fluidity-app/lib/types/ethereum"
"github.com/fluidity-money/fluidity-app/lib/types/misc"

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

func TestDecodeUpdatePosition(t *testing.T) {
b, err := hex.DecodeString("0000000000000000000000000000000000000000000000000dc051fbd4b4a1ca00000000000000000000000000000000000000000000000000000000d47a19cc")
assert.Nil(t, err)
u, err := DecodeUpdatePosition(ethereum.Log{
Topics: []ethereum.Hash{
ethereum.HashFromString("0x555c5816cc24ae6b4a85b2e02a07ebc514a04639a07694f29ff9a0de9b650987"),
ethereum.HashFromString("0x0000000000000000000000000000000000000000000000000000000000000000"),
},
Data: misc.Blob(b),
})
assert.Nil(t, err)
//assert.Equalf(t, misc.BigIntFromInt(0), u.Id, "id not equal")
assert.Equalf(t, misc.BigIntFromInt64(990882060068757962), u.Token0, "token0 not equal")
assert.Equalf(t, misc.BigIntFromInt64(3564771788), u.Token1, "token1 not equal")
}

func TestMintPosition(t *testing.T) {
b, err := hex.DecodeString("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd00e4fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd08a0")
assert.Nil(t, err)
m, err := DecodeMint(ethereum.Log{
Topics: []ethereum.Hash{
ethereum.HashFromString("0x7b0f5059c07211d90c2400fc99ac93e0e56db5168afa91f60d178bb6dc1c73f0"),
ethereum.HashFromString("0x0000000000000000000000000000000000000000000000000000000000000000"),
ethereum.HashFromString("0x000000000000000000000000feb6034fc7df27df18a3a6bad5fb94c0d3dcb6d5"),
ethereum.HashFromString("0x00000000000000000000000022b9fa698b68bba071b513959794e9a47d19214c"),
},
Data: misc.Blob(b),
})
assert.Nil(t, err)
//assert.Equalf(t, misc.BigIntFromInt(0), m.Id, "id not the same")
assert.Equalf(t,
ethereum.AddressFromString("0x22b9fa698b68bba071b513959794e9a47d19214c"),
m.Pool,
"pool not correct",
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ contract AutoIncreaseLiquidityProvider is ILiquidityProvider {
/// @dev amountTaken_ from the pool so far
uint256 public amountTaken_;

uint256 public amountAdded_;

/**
* @notice initialiser function
*
Expand All @@ -57,7 +59,8 @@ contract AutoIncreaseLiquidityProvider is ILiquidityProvider {
}

/// @inheritdoc ILiquidityProvider
function addToPool(uint /* _amount */) external view {
function addToPool(uint _amount) external {
amountAdded_ += _amount;
}

/// @inheritdoc ILiquidityProvider
Expand All @@ -71,7 +74,7 @@ contract AutoIncreaseLiquidityProvider is ILiquidityProvider {
function totalPoolAmount() external view returns (uint) {
uint secsSince = block.timestamp - deploymentTs_;
uint256 fluidBalance = (secsSince * increaseAmount_) - amountTaken_;
return fluidBalance;
return fluidBalance + amountAdded_;
}

function owner_() public view returns (address) {
Expand Down
Loading