Skip to content

Commit

Permalink
add test for trigger with contract read func binding
Browse files Browse the repository at this point in the history
  • Loading branch information
v9n committed Dec 27, 2024
1 parent 76c2bcd commit 53dd37d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
1 change: 0 additions & 1 deletion core/taskengine/trigger/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ func (evt *EventTrigger) Evaluate(event *types.Log, program string) (bool, error
jsvm.Set(k, v)
}

//result, err := expr.Run(program, envs)
result, err := jsvm.RunString(program)

if err != nil {
Expand Down
36 changes: 35 additions & 1 deletion core/taskengine/trigger/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package trigger
import (
"testing"

"github.com/AvaProtocol/ap-avs/core/taskengine/macros"
"github.com/AvaProtocol/ap-avs/core/testutil"
)

func TestChainlinkLatestAnswer(t *testing.T) {
func TestTriggerExpression(t *testing.T) {
event, err := testutil.GetEventForTx("0x8f7c1f698f03d6d32c996b679ea1ebad45bbcdd9aa95d250dda74763cc0f508d", 82)

if err != nil {
Expand Down Expand Up @@ -39,3 +40,36 @@ func TestChainlinkLatestAnswer(t *testing.T) {
t.Errorf("expect expression to be not match, but got match: error: %v", err)
}
}

func TestTriggerWithContractReadBindingInExpression(t *testing.T) {
// This event is transfering usdc
event, err := testutil.GetEventForTx("0x4bb728dfbe58d7c641c02a214cac6156a0d6a0fe648cb27a7de229a3160e91b1", 145)

macros.SetRpc(testutil.GetTestRPCURL())
eventTrigger := NewEventTrigger(&RpcOption{
RpcURL: testutil.GetTestRPCURL(),
WsRpcURL: testutil.GetTestRPCURL(),
}, make(chan TriggerMetadata[EventMark], 1000))

// USDC pair from chainlink, usually USDC price is ~99cent but never approach $1
// for an unknow reason the decimal is 8 instead of 6
program := `trigger1.data.topics[0] == "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" && bigGt(chainlinkPrice("0xA2F78ab2355fe2f984D808B5CeE7FD0A93D5270E"), toBigInt("1000000000"))`

result, err := eventTrigger.Evaluate(event, program)
if err != nil {
t.Errorf("expected no error when evaluate program but got error: %s", err)
}
if result {
t.Errorf("expect expression to be false, but got true: error: %v", err)
}

program = `trigger1.data.topics[0] == "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" && bigGt(chainlinkPrice("0xA2F78ab2355fe2f984D808B5CeE7FD0A93D5270E"), toBigInt("95000000"))`

result, err = eventTrigger.Evaluate(event, program)
if err != nil {
t.Errorf("expected no error when evaluate program but got error: %s", err)
}
if !result {
t.Errorf("expect expression to be false, but got true: error: %v", err)
}
}

0 comments on commit 53dd37d

Please sign in to comment.