Skip to content

Commit

Permalink
Merge pull request #99 from taikoxyz/duration
Browse files Browse the repository at this point in the history
feat: add duration
  • Loading branch information
bennettyong authored Oct 10, 2024
2 parents 8722c6c + 907e137 commit a1515ac
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
3 changes: 2 additions & 1 deletion adapters/adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ type Lock struct {
TokenAmount *big.Int
TokenDecimals uint8
Token common.Address
Time uint64
Duration uint64
BlockTime uint64
BlockNumber uint64
TxHash common.Hash
}
Expand Down
21 changes: 12 additions & 9 deletions adapters/projects/drips/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
// https://taikoscan.io/address/0x46f0a2e45bee8e9ebfdb278ce06caa6af294c349
LockAddress string = "0x46f0a2e45bee8e9ebfdb278ce06caa6af294c349"

logDepositSignature string = "Deposit(address,uint256)"
logDepositWithDurationSignature string = "DepositWithDuration(address,uint256,uint256,uint256)"
)

type LockIndexer struct {
Expand All @@ -43,12 +43,14 @@ func (indexer *LockIndexer) Index(ctx context.Context, logs ...types.Log) ([]ada
var locks []adapters.Lock

for _, l := range logs {
if !indexer.isDeposit(l) {
if !indexer.isDepositWithDuration(l) {
continue
}

var depositEvent struct {
Assets *big.Int
var depositWithDurationEvent struct {
LockStart *big.Int
Amount *big.Int
Duration *big.Int
}

user := common.BytesToAddress(l.Topics[1].Bytes()[12:])
Expand All @@ -58,7 +60,7 @@ func (indexer *LockIndexer) Index(ctx context.Context, logs ...types.Log) ([]ada
return nil, err
}

err = dripsABI.UnpackIntoInterface(&depositEvent, "Deposit", l.Data)
err = dripsABI.UnpackIntoInterface(&depositWithDurationEvent, "DepositWithDuration", l.Data)
if err != nil {
return nil, err
}
Expand All @@ -70,10 +72,11 @@ func (indexer *LockIndexer) Index(ctx context.Context, logs ...types.Log) ([]ada

lock := &adapters.Lock{
User: user,
TokenAmount: depositEvent.Assets,
TokenAmount: depositWithDurationEvent.Amount,
TokenDecimals: adapters.TaikoTokenDecimals,
Token: common.HexToAddress(adapters.TaikoTokenAddress),
Time: block.Time(),
Duration: depositWithDurationEvent.Duration.Uint64(),
BlockTime: block.Time(),
BlockNumber: block.NumberU64(),
TxHash: l.TxHash,
}
Expand All @@ -84,6 +87,6 @@ func (indexer *LockIndexer) Index(ctx context.Context, logs ...types.Log) ([]ada
return locks, nil
}

func (indexer *LockIndexer) isDeposit(l types.Log) bool {
return l.Topics[0].Hex() == crypto.Keccak256Hash([]byte(logDepositSignature)).Hex()
func (indexer *LockIndexer) isDepositWithDuration(l types.Log) bool {
return l.Topics[0].Hex() == crypto.Keccak256Hash([]byte(logDepositWithDurationSignature)).Hex()
}
3 changes: 2 additions & 1 deletion adapters/projects/drips/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func TestLockIndexer(t *testing.T) {
assert.Equal(t, big.NewInt(2000000000000000000), locks[0].TokenAmount)
assert.Equal(t, adapters.TaikoTokenDecimals, locks[0].TokenDecimals)
assert.Equal(t, common.HexToAddress(adapters.TaikoTokenAddress), locks[0].Token)
assert.Equal(t, uint64(1728390191), locks[0].Time)
assert.Equal(t, uint64(1728390191), locks[0].BlockTime)
assert.Equal(t, uint64(86400), locks[0].Duration)
assert.Equal(t, uint64(blocknumber), locks[0].BlockNumber)
assert.Equal(t, common.HexToHash("0x95f528b52f0a75176543f516014bbba26e003f1c17c9b9413e936240e3f44650"), locks[0].TxHash)
}
9 changes: 6 additions & 3 deletions adapters/projects/symmetric/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func (indexer *LockIndexer) processDepositLog(ctx context.Context, l types.Log)
}

user := common.BytesToAddress(l.Topics[1].Bytes()[12:])
lockEndTime := l.Topics[2].Big()
duration := new(big.Int).Sub(lockEndTime, depositEvent.Ts)

block, err := indexer.client.BlockByNumber(ctx, big.NewInt(int64(l.BlockNumber)))
if err != nil {
Expand Down Expand Up @@ -103,7 +105,7 @@ func (indexer *LockIndexer) processDepositLog(ctx context.Context, l types.Log)
return nil, errors.Join(err, errors.New("fetching pool tokens"))
}

return indexer.createLocks(user, depositEvent.Value, totalSupply, poolTokens, block, l.TxHash), nil
return indexer.createLocks(user, duration, depositEvent.Value, totalSupply, poolTokens, block, l.TxHash), nil
}

func (indexer *LockIndexer) parseDepositEvent(l types.Log) (*struct {
Expand Down Expand Up @@ -151,7 +153,7 @@ func (indexer *LockIndexer) fetchBalancerTokenInfo(balancerToken *balancer_token
return totalSupply, poolId, vault, nil
}

func (indexer *LockIndexer) createLocks(user common.Address, depositValue *big.Int, totalSupply *big.Int, poolTokens struct {
func (indexer *LockIndexer) createLocks(user common.Address, duration, depositValue *big.Int, totalSupply *big.Int, poolTokens struct {
Tokens []common.Address
Balances []*big.Int
LastChangeBlock *big.Int
Expand All @@ -168,7 +170,8 @@ func (indexer *LockIndexer) createLocks(user common.Address, depositValue *big.I
TokenAmount: userAmount,
TokenDecimals: adapters.TaikoTokenDecimals,
Token: token,
Time: block.Time(),
Duration: duration.Uint64(),
BlockTime: block.Time(),
BlockNumber: block.NumberU64(),
TxHash: txHash,
}
Expand Down
10 changes: 7 additions & 3 deletions adapters/projects/symmetric/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ func TestLockIndexer(t *testing.T) {
txHash := common.HexToHash("0x27058b3cfbd15e3466cdd1b86ab387a473f2ca01c0697eb6b47ae7b33fdf97b0")
user := common.HexToAddress("0x7255Db0d1C1B93Fb756157074fa0613Aa6878F31")
time := 1728397319
duration := 4959481
expectedLocks := []adapters.Lock{
{
User: user,
TokenAmount: big.NewInt(45884668717994),
TokenDecimals: adapters.TaikoTokenDecimals,
Token: common.HexToAddress(adapters.WETHAddress),
Time: uint64(time),
Duration: uint64(duration),
BlockTime: uint64(time),
BlockNumber: uint64(blocknumber),
TxHash: txHash,
},
Expand All @@ -48,7 +50,8 @@ func TestLockIndexer(t *testing.T) {
TokenAmount: big.NewInt(287649277362130342),
TokenDecimals: adapters.TaikoTokenDecimals,
Token: common.HexToAddress(adapters.TaikoTokenAddress),
Time: uint64(time),
Duration: uint64(duration),
BlockTime: uint64(time),
BlockNumber: uint64(blocknumber),
TxHash: txHash,
},
Expand All @@ -58,8 +61,9 @@ func TestLockIndexer(t *testing.T) {
assert.Equal(t, expectedLocks[i].User, lock.User)
assert.Equal(t, expectedLocks[i].TokenAmount, lock.TokenAmount)
assert.Equal(t, expectedLocks[i].TokenDecimals, lock.TokenDecimals)
assert.Equal(t, expectedLocks[i].Duration, lock.Duration)
assert.Equal(t, expectedLocks[i].Token, lock.Token)
assert.Equal(t, expectedLocks[i].Time, lock.Time)
assert.Equal(t, expectedLocks[i].BlockTime, lock.BlockTime)
assert.Equal(t, expectedLocks[i].BlockNumber, lock.BlockNumber)
assert.Equal(t, expectedLocks[i].TxHash, lock.TxHash)
}
Expand Down

0 comments on commit a1515ac

Please sign in to comment.