Skip to content

Commit 3a7c5b2

Browse files
authored
[CAL][3] - Implement GetChainFeeComponents (#985)
* [CAL][3] - Implement GetChainFeeComponents * [CAL][4] - Implement MsgsBetweenSeqNums() function in accessor (#987)
1 parent 2f73064 commit 3a7c5b2

File tree

6 files changed

+182
-169
lines changed

6 files changed

+182
-169
lines changed

pkg/chainaccessor/legacy_accessor.go

Lines changed: 84 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ import (
77

88
"github.com/smartcontractkit/chainlink-common/pkg/logger"
99
"github.com/smartcontractkit/chainlink-common/pkg/types"
10+
"github.com/smartcontractkit/chainlink-common/pkg/types/query"
11+
"github.com/smartcontractkit/chainlink-common/pkg/types/query/primitives"
1012

13+
"github.com/smartcontractkit/chainlink-ccip/pkg/consts"
1114
"github.com/smartcontractkit/chainlink-ccip/pkg/contractreader"
15+
"github.com/smartcontractkit/chainlink-ccip/pkg/logutil"
1216
cciptypes "github.com/smartcontractkit/chainlink-ccip/pkg/types/ccipocr3"
1317
)
1418

@@ -59,16 +63,13 @@ func (l *LegacyAccessor) GetContractAddress(contractName string) ([]byte, error)
5963
return addressBytes, nil
6064
}
6165

62-
func (l *LegacyAccessor) GetChainFeeComponents(
63-
ctx context.Context,
64-
) map[cciptypes.ChainSelector]cciptypes.ChainFeeComponents {
65-
// TODO(NONEVM-1865): implement
66-
panic("implement me")
67-
}
66+
func (l *LegacyAccessor) GetChainFeeComponents(ctx context.Context) (cciptypes.ChainFeeComponents, error) {
67+
fc, err := l.contractWriter.GetFeeComponents(ctx)
68+
if err != nil {
69+
return cciptypes.ChainFeeComponents{}, fmt.Errorf("get fee components: %w", err)
70+
}
6871

69-
func (l *LegacyAccessor) GetDestChainFeeComponents(ctx context.Context) (types.ChainFeeComponents, error) {
70-
// TODO(NONEVM-1865): implement
71-
panic("implement me")
72+
return *fc, nil
7273
}
7374

7475
func (l *LegacyAccessor) Sync(ctx context.Context, contracts cciptypes.ContractAddresses) error {
@@ -78,11 +79,82 @@ func (l *LegacyAccessor) Sync(ctx context.Context, contracts cciptypes.ContractA
7879

7980
func (l *LegacyAccessor) MsgsBetweenSeqNums(
8081
ctx context.Context,
81-
dest cciptypes.ChainSelector,
82+
destChainSelector cciptypes.ChainSelector,
8283
seqNumRange cciptypes.SeqNumRange,
8384
) ([]cciptypes.Message, error) {
84-
// TODO(NONEVM-1865): implement
85-
panic("implement me")
85+
lggr := logutil.WithContextValues(ctx, l.lggr)
86+
seq, err := l.contractReader.ExtendedQueryKey(
87+
ctx,
88+
consts.ContractNameOnRamp,
89+
query.KeyFilter{
90+
Key: consts.EventNameCCIPMessageSent,
91+
Expressions: []query.Expression{
92+
query.Comparator(consts.EventAttributeSourceChain, primitives.ValueComparator{
93+
Value: l.chainSelector,
94+
Operator: primitives.Eq,
95+
}),
96+
query.Comparator(consts.EventAttributeDestChain, primitives.ValueComparator{
97+
Value: destChainSelector,
98+
Operator: primitives.Eq,
99+
}),
100+
query.Comparator(consts.EventAttributeSequenceNumber, primitives.ValueComparator{
101+
Value: seqNumRange.Start(),
102+
Operator: primitives.Gte,
103+
}, primitives.ValueComparator{
104+
Value: seqNumRange.End(),
105+
Operator: primitives.Lte,
106+
}),
107+
query.Confidence(primitives.Finalized),
108+
},
109+
},
110+
query.LimitAndSort{
111+
SortBy: []query.SortBy{
112+
query.NewSortBySequence(query.Asc),
113+
},
114+
Limit: query.Limit{
115+
Count: uint64(seqNumRange.End() - seqNumRange.Start() + 1),
116+
},
117+
},
118+
&SendRequestedEvent{},
119+
)
120+
if err != nil {
121+
return nil, fmt.Errorf("failed to query onRamp: %w", err)
122+
}
123+
124+
lggr.Infow("queried messages between sequence numbers",
125+
"numMsgs", len(seq),
126+
"sourceChainSelector", l.chainSelector,
127+
"seqNumRange", seqNumRange.String(),
128+
)
129+
130+
onRampAddress, err := l.GetContractAddress(consts.ContractNameOnRamp)
131+
if err != nil {
132+
return nil, fmt.Errorf("failed to get onRamp contract address: %w", err)
133+
}
134+
135+
msgs := make([]cciptypes.Message, 0)
136+
for _, item := range seq {
137+
msg, ok := item.Data.(*SendRequestedEvent)
138+
if !ok {
139+
return nil, fmt.Errorf("failed to cast %v to Message", item.Data)
140+
}
141+
142+
if err := ValidateSendRequestedEvent(msg, l.chainSelector, destChainSelector, seqNumRange); err != nil {
143+
lggr.Errorw("validate send requested event", "err", err, "message", msg)
144+
continue
145+
}
146+
147+
msg.Message.Header.OnRamp = onRampAddress
148+
msgs = append(msgs, msg.Message)
149+
}
150+
151+
lggr.Infow("decoded messages between sequence numbers",
152+
"msgs", msgs,
153+
"sourceChainSelector", l.chainSelector,
154+
"seqNumRange", seqNumRange.String(),
155+
)
156+
157+
return msgs, nil
86158
}
87159

88160
func (l *LegacyAccessor) LatestMsgSeqNum(ctx context.Context, dest cciptypes.ChainSelector) (cciptypes.SeqNum, error) {

pkg/chainaccessor/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package chainaccessor
2+
3+
import cciptypes "github.com/smartcontractkit/chainlink-ccip/pkg/types/ccipocr3"
4+
5+
// SendRequestedEvent represents the contents of the event emitted by the CCIP onramp when a message is sent.
6+
type SendRequestedEvent struct {
7+
DestChainSelector cciptypes.ChainSelector
8+
SequenceNumber cciptypes.SeqNum
9+
Message cciptypes.Message
10+
}

pkg/chainaccessor/validators.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package chainaccessor
2+
3+
import (
4+
"fmt"
5+
6+
cciptypes "github.com/smartcontractkit/chainlink-ccip/pkg/types/ccipocr3"
7+
)
8+
9+
func ValidateSendRequestedEvent(
10+
ev *SendRequestedEvent, source, dest cciptypes.ChainSelector, seqNumRange cciptypes.SeqNumRange,
11+
) error {
12+
if ev == nil {
13+
return fmt.Errorf("send requested event is nil")
14+
}
15+
16+
if ev.Message.Header.DestChainSelector != dest {
17+
return fmt.Errorf("msg dest chain is not the expected queried one")
18+
}
19+
if ev.DestChainSelector != dest {
20+
return fmt.Errorf("dest chain is not the expected queried one")
21+
}
22+
23+
if ev.Message.Header.SourceChainSelector != source {
24+
return fmt.Errorf("source chain is not the expected queried one")
25+
}
26+
27+
if ev.SequenceNumber != ev.Message.Header.SequenceNumber {
28+
return fmt.Errorf("event sequence number does not match the message sequence number %d != %d",
29+
ev.SequenceNumber, ev.Message.Header.SequenceNumber)
30+
}
31+
32+
if ev.SequenceNumber < seqNumRange.Start() || ev.SequenceNumber > seqNumRange.End() {
33+
return fmt.Errorf("send requested event sequence number is not in the expected range")
34+
}
35+
36+
if ev.Message.Header.MessageID.IsEmpty() {
37+
return fmt.Errorf("message ID is zero")
38+
}
39+
40+
if len(ev.Message.Receiver) == 0 {
41+
return fmt.Errorf("empty receiver address: %s", ev.Message.Receiver.String())
42+
}
43+
44+
if ev.Message.Sender.IsZeroOrEmpty() {
45+
return fmt.Errorf("invalid sender address: %s", ev.Message.Sender.String())
46+
}
47+
48+
if ev.Message.FeeTokenAmount.IsEmpty() {
49+
return fmt.Errorf("fee token amount is zero")
50+
}
51+
52+
if ev.Message.FeeToken.IsZeroOrEmpty() {
53+
return fmt.Errorf("invalid fee token: %s", ev.Message.FeeToken.String())
54+
}
55+
56+
return nil
57+
}

0 commit comments

Comments
 (0)