Skip to content

Commit

Permalink
Start to track campaign updated events
Browse files Browse the repository at this point in the history
  • Loading branch information
af-afk committed Aug 16, 2024
1 parent bc6a27e commit e91f279
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 8 deletions.
8 changes: 8 additions & 0 deletions cmd/ingestor.logs.ethereum/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var FilterTopics = [][]ethCommon.Hash{{ // Matches any of these in the first top
thirdweb.TopicAccountCreated,
leo.TopicCampaignBalanceUpdated,
leo.TopicCampaignCreated,
leo.TopicCampaignUpdated,
seawater.TopicMintPosition,
seawater.TopicBurnPosition,
seawater.TopicTransferPosition,
Expand Down Expand Up @@ -211,6 +212,13 @@ func handleLogCallback(seawaterAddr, thirdwebAddr, leoAddr ethCommon.Address, l
isSeawater = false
isLeo = true

case leo.TopicCampaignUpdated:
a, err = leo.UnpackCampaignUpdated(topic1, topic2, topic3, data)
logEvent("CampaignUpdated")
table = "events_leo_campaignupdated"
isSeawater = false
isLeo = true

case seawater.TopicMintPosition:
a, err = seawater.UnpackMintPosition(topic1, topic2, topic3, data)
logEvent("MintPosition")
Expand Down
20 changes: 20 additions & 0 deletions db/migrations/1723799478-events_leo_updated.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- migrate:up

CREATE TABLE events_leo_campaignupdated (
id SERIAL PRIMARY KEY,
created_by TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
block_hash HASH NOT NULL,
transaction_hash HASH NOT NULL,
block_number INTEGER NOT NULL,
emitter_addr ADDRESS NOT NULL,

identifier VARCHAR NOT NULL,
pool ADDRESS NOT NULL,
per_second HUGEINT NOT NULL,
tick_lower INTEGER NOT NULL,
tick_upper INTEGER NOT NULL,
starting TIMESTAMP WITHOUT TIME ZONE,
ending TIMESTAMP WITHOUT TIME ZONE
);

-- migrate:down
31 changes: 31 additions & 0 deletions lib/events/leo/abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,36 @@
}
],
"anonymous": false
},
{
"type": "event",
"name": "CampaignUpdated",
"inputs": [
{
"name": "identifier",
"type": "bytes8",
"indexed": true,
"internalType": "bytes8"
},
{
"name": "pool",
"type": "address",
"indexed": true,
"internalType": "address"
},
{
"name": "perSecond",
"type": "uint256",
"indexed": true,
"internalType": "uint256"
},
{
"name": "extras",
"type": "uint256",
"indexed": false,
"internalType": "uint256"
}
],
"anonymous": false
}
]
41 changes: 39 additions & 2 deletions lib/events/leo/leo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package leo
import (
"bytes"
_ "embed"
"time"
"fmt"
"math/big"

Expand All @@ -15,6 +16,7 @@ import (
var (
TopicCampaignBalanceUpdated = abi.Events["CampaignBalanceUpdated"].ID
TopicCampaignCreated = abi.Events["CampaignCreated"].ID
TopicCampaignUpdated = abi.Events["CampaignUpdated"].ID
)

//go:embed abi.json
Expand Down Expand Up @@ -58,8 +60,31 @@ func UnpackCampaignCreated(topic1, topic2, topic3 ethCommon.Hash, d []byte) (*Ca
TickLower: tickLower,
TickUpper: tickUpper,
Owner: owner,
Starting: starting,
Ending: ending,
Starting: time.Unix(int64(starting), 0),
Ending: time.Unix(int64(ending), 0),
}, nil
}

// UnpackCampaignUpdated happening with a new iteration of an existing
// campaign for a specific pool
func UnpackCampaignUpdated(topic1, topic2, topic3 ethCommon.Hash, d []byte) (*CampaignUpdated, error) {
i, err := abi.Unpack("CampaignCreated", d)
if err != nil {
return nil, err
}
extras, ok := i[0].(*big.Int)
if !ok {
return nil, fmt.Errorf("bad extras: %T", i[0])
}
tickLower, tickUpper, starting, ending := unpackExtras(extras)
return &CampaignUpdated{
Identifier: hashToBytes8Data(topic1),
Pool: hashToAddr(topic2),
PerSecond: hashToNumber(topic3),
TickLower: tickLower,
TickUpper: tickUpper,
Starting: time.Unix(int64(starting), 0),
Ending: time.Unix(int64(ending), 0),
}, nil
}

Expand All @@ -73,6 +98,10 @@ func hashToAddr(h ethCommon.Hash) types.Address {
return types.AddressFromString(v.String())
}

func hashToNumber(h ethCommon.Hash) types.Number {
return types.NumberFromBig(h.Big())
}

func unpackDetails(i *big.Int) (tickLower int32, tickUpper int32, owner types.Address) {
tickLower = int32(new(big.Int).Rsh(i, 32 + 160).Int64())
tickUpper = int32(new(big.Int).Rsh(i, 160).Int64())
Expand All @@ -85,3 +114,11 @@ func unpackTimes(i *big.Int) (starting uint64, ending uint64) {
ending = i.Uint64()
return
}

func unpackExtras(i *big.Int) (tickLower int32, tickUpper int32, starting uint64, ending uint64) {
tickLower = int32(new(big.Int).Rsh(i, 32 + 64 + 64).Int64())
tickUpper = int32(new(big.Int).Rsh(i, 64 + 64).Int64())
starting = new(big.Int).Rsh(i, 64).Uint64()
ending = i.Uint64()
return
}
14 changes: 14 additions & 0 deletions lib/events/leo/leo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,17 @@ func TestUnpackTimes(t *testing.T) {
assert.Equalf(t, uint64(5000), starting, "starting not equal")
assert.Equalf(t, uint64(545464), ending, "ending not equal")
}

func TestUnpackExtras(t *testing.T) {
//29230032814334249381340450918364660083204158393161
tickLower, tickUpper, starting, ending := unpackExtras(new(big.Int).SetBits([]big.Word{
2889,
1888,
85899346119,
0,
}))
assert.Equalf(t, int32(20), tickLower, "lower is wrong")
assert.Equalf(t, int32(199), tickUpper, "upper is wrong")
assert.Equalf(t, uint64(1888), starting, "starting is wrong")
assert.Equalf(t, uint64(2889), ending, "ending is wrong")
}
23 changes: 19 additions & 4 deletions lib/events/leo/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package leo

import (
"time"

"github.com/fluidity-money/long.so/lib/events"
"github.com/fluidity-money/long.so/lib/types"
)
Expand All @@ -9,8 +11,8 @@ type (
CampaignBalanceUpdated struct {
events.Event

Identifier types.Data `json:"identifier"`
NewMaximum types.UnscaledNumber `json:"newMaximum"`
Identifier types.Data `json:"identifier"`
NewMaximum types.UnscaledNumber `json:"newMaximum"`
}

// CampaignCreated, unpacked in the local function with some of
Expand All @@ -24,7 +26,20 @@ type (
TickLower int32 `json:"tickLower"`
TickUpper int32 `json:"tickUpper"`
Owner types.Address `json:"owner"`
Starting uint64 `json:"starting"`
Ending uint64 `json:"ending"`
Starting time.Time `json:"starting"`
Ending time.Time `json:"ending"`
}

CampaignUpdated struct {
events.Event

Identifier types.Data `json:"identifier"`
Pool types.Address `json:"pool"`
PerSecond types.Number `json:"perSecond"`
Token types.Address `json:"token"`
TickLower int32 `json:"tickLower"`
TickUpper int32 `json:"tickUpper"`
Starting time.Time `json:"starting"`
Ending time.Time `json:"ending"`
}
)
29 changes: 27 additions & 2 deletions pkg/leo/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//! Autogenerated structures for EVM events.
use stylus_sdk::{
alloy_primitives::{Address, FixedBytes, U160, U256},
alloy_sol_types::sol,
Expand Down Expand Up @@ -41,6 +39,28 @@ fn pack_times(starting: u64, ending: u64) -> U256 {
(U256::from(starting) << 64) | U256::from(ending)
}

pub fn emit_campaign_updated(
identifier: FixedBytes<8>,
pool: Address,
tick_lower: i32,
tick_upper: i32,
starting: u64,
ending: u64,
) {
evm::log(CampaignUpdated {
identifier: identifier.as_slice().try_into().unwrap(),
pool,
extras: pack_extras(tick_lower, tick_upper, starting, ending),
});
}

fn pack_extras(tick_lower: i32, tick_upper: i32, starting: u64, ending: u64) -> U256 {
let mut packed = U256::from(tick_lower as u32) << (32 + 64 + 64);
packed |= U256::from(tick_upper as u32) << (64 + 64);
packed |= U256::from(starting) << 64;
packed | U256::from(ending)
}

#[test]
fn test_pack_details() {
use crate::address;
Expand All @@ -56,3 +76,8 @@ fn test_pack_details() {
fn test_pack_times() {
dbg!(pack_times(5000, 545464));
}

#[test]
fn test_pack_extras() {
dbg!(pack_extras(20, 199, 1888, 2889));
}
2 changes: 2 additions & 0 deletions pkg/leo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ impl Leo {
});
}

events::emit_campaign_updated(identifier, pool, tick_lower, tick_upper, starting, ending);

Ok(())
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/sol/ILeoEvents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ interface ILeoEvents {
uint256 details, // [tick lower, tick upper, owner],
uint256 times // [starting, ending]
);

event CampaignUpdated(
bytes8 indexed identifier,
address indexed pool,
uint256 indexed perSecond,
uint256 extras // [tick lower, tick upper, starting, ending]
);
}

0 comments on commit e91f279

Please sign in to comment.