Skip to content

Commit

Permalink
support native token (#13714)
Browse files Browse the repository at this point in the history
* support native token

* split the tests and add mercury related tests
  • Loading branch information
shileiwill authored Jul 2, 2024
1 parent 3a96fa0 commit 4b19e37
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 75 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-bananas-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": patch
---

add native billing in smoke test #added
2 changes: 1 addition & 1 deletion integration-tests/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"crypto/ecdsa"
"fmt"
"math"
"math/big"
"strings"
"sync"
Expand Down Expand Up @@ -40,7 +41,6 @@ import (
gethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
"github.com/test-go/testify/require"
"math"

ctfconfig "github.com/smartcontractkit/chainlink-testing-framework/config"
"github.com/smartcontractkit/chainlink-testing-framework/utils/testcontext"
Expand Down
15 changes: 9 additions & 6 deletions integration-tests/actions/automation_ocr_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,12 @@ func DeployAutoOCRRegistryAndRegistrar(

// DeployConsumers deploys and registers keeper consumers. If ephemeral addresses are enabled, it will deploy and register the consumers from ephemeral addresses, but each upkpeep will be registered with root key address as the admin. Which means
// that functions like setting upkeep configuration, pausing, unpausing, etc. will be done by the root key address. It deploys multicall contract and sends link funds to each deployment address.
func DeployConsumers(t *testing.T, chainClient *seth.Client, registry contracts.KeeperRegistry, registrar contracts.KeeperRegistrar, linkToken contracts.LinkToken, numberOfUpkeeps int, linkFundsForEachUpkeep *big.Int, upkeepGasLimit uint32, isLogTrigger bool, isMercury bool) ([]contracts.KeeperConsumer, []*big.Int) {
err := DeployMultiCallAndFundDeploymentAddresses(chainClient, linkToken, numberOfUpkeeps, linkFundsForEachUpkeep)
require.NoError(t, err, "Sending link funds to deployment addresses shouldn't fail")
func DeployConsumers(t *testing.T, chainClient *seth.Client, registry contracts.KeeperRegistry, registrar contracts.KeeperRegistrar, linkToken contracts.LinkToken, numberOfUpkeeps int, linkFundsForEachUpkeep *big.Int, upkeepGasLimit uint32, isLogTrigger bool, isMercury bool, isBillingTokenNative bool, wethToken contracts.WETHToken) ([]contracts.KeeperConsumer, []*big.Int) {
// Fund deployers with LINK, no need to do this for Native token
if !isBillingTokenNative {
err := DeployMultiCallAndFundDeploymentAddresses(chainClient, linkToken, numberOfUpkeeps, linkFundsForEachUpkeep)
require.NoError(t, err, "Sending link funds to deployment addresses shouldn't fail")
}

upkeeps := DeployKeeperConsumers(t, chainClient, numberOfUpkeeps, isLogTrigger, isMercury)
require.Equal(t, numberOfUpkeeps, len(upkeeps), "Number of upkeeps should match")
Expand All @@ -285,7 +288,7 @@ func DeployConsumers(t *testing.T, chainClient *seth.Client, registry contracts.
upkeepsAddresses = append(upkeepsAddresses, upkeep.Address())
}
upkeepIds := RegisterUpkeepContracts(
t, chainClient, linkToken, linkFundsForEachUpkeep, upkeepGasLimit, registry, registrar, numberOfUpkeeps, upkeepsAddresses, isLogTrigger, isMercury,
t, chainClient, linkToken, linkFundsForEachUpkeep, upkeepGasLimit, registry, registrar, numberOfUpkeeps, upkeepsAddresses, isLogTrigger, isMercury, isBillingTokenNative, wethToken,
)
require.Equal(t, numberOfUpkeeps, len(upkeepIds), "Number of upkeepIds should match")
return upkeeps, upkeepIds
Expand Down Expand Up @@ -318,7 +321,7 @@ func DeployPerformanceConsumers(
for _, upkeep := range upkeeps {
upkeepsAddresses = append(upkeepsAddresses, upkeep.Address())
}
upkeepIds := RegisterUpkeepContracts(t, chainClient, linkToken, linkFundsForEachUpkeep, upkeepGasLimit, registry, registrar, numberOfUpkeeps, upkeepsAddresses, false, false)
upkeepIds := RegisterUpkeepContracts(t, chainClient, linkToken, linkFundsForEachUpkeep, upkeepGasLimit, registry, registrar, numberOfUpkeeps, upkeepsAddresses, false, false, false, nil)
return upkeeps, upkeepIds
}

Expand All @@ -344,7 +347,7 @@ func DeployPerformDataCheckerConsumers(
for _, upkeep := range upkeeps {
upkeepsAddresses = append(upkeepsAddresses, upkeep.Address())
}
upkeepIds := RegisterUpkeepContracts(t, chainClient, linkToken, linkFundsForEachUpkeep, upkeepGasLimit, registry, registrar, numberOfUpkeeps, upkeepsAddresses, false, false)
upkeepIds := RegisterUpkeepContracts(t, chainClient, linkToken, linkFundsForEachUpkeep, upkeepGasLimit, registry, registrar, numberOfUpkeeps, upkeepsAddresses, false, false, false, nil)
return upkeeps, upkeepIds
}

Expand Down
11 changes: 10 additions & 1 deletion integration-tests/actions/automationv2/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,8 @@ func (a *AutomationTest) SetConfigOnRegistry() error {
} else if a.RegistrySettings.RegistryVersion == ethereum.RegistryVersion_2_3 {
ocrConfig.TypedOnchainConfig23 = a.RegistrySettings.Create23OnchainConfig(a.Registrar.Address(), a.UpkeepPrivilegeManager, a.Registry.ChainModuleAddress(), a.Registry.ReorgProtectionEnabled())
ocrConfig.BillingTokens = []common.Address{
common.HexToAddress(a.LinkToken.Address()), // TODO add more billing tokens
common.HexToAddress(a.LinkToken.Address()),
common.HexToAddress(a.WETHToken.Address()),
}

ocrConfig.BillingConfigs = []i_automation_registry_master_wrapper_2_3.AutomationRegistryBase23BillingConfig{
Expand All @@ -577,6 +578,14 @@ func (a *AutomationTest) SetConfigOnRegistry() error {
FallbackPrice: big.NewInt(1000),
MinSpend: big.NewInt(200),
},
{
GasFeePPB: 100,
FlatFeeMilliCents: big.NewInt(500),
PriceFeed: common.HexToAddress(a.EthUSDFeed.Address()), // ETH/USD feed and LINK/USD feed are the same
Decimals: 18,
FallbackPrice: big.NewInt(1000),
MinSpend: big.NewInt(200),
},
}
}
err = a.Registry.SetConfigTypeSafe(ocrConfig)
Expand Down
113 changes: 69 additions & 44 deletions integration-tests/actions/keeper_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"testing"

"github.com/ethereum/go-ethereum/core/types"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/smartcontractkit/seth"
Expand Down Expand Up @@ -115,7 +116,7 @@ func DeployKeeperContracts(
}

registrar := DeployKeeperRegistrar(t, client, registryVersion, linkToken, registrarSettings, registry)
upkeeps, upkeepIds := DeployConsumers(t, client, registry, registrar, linkToken, numberOfUpkeeps, linkFundsForEachUpkeep, upkeepGasLimit, false, false)
upkeeps, upkeepIds := DeployConsumers(t, client, registry, registrar, linkToken, numberOfUpkeeps, linkFundsForEachUpkeep, upkeepGasLimit, false, false, false, nil)

return registry, registrar, upkeeps, upkeepIds
}
Expand Down Expand Up @@ -178,7 +179,7 @@ func DeployPerformanceKeeperContracts(
upkeepsAddresses = append(upkeepsAddresses, upkeep.Address())
}

upkeepIds := RegisterUpkeepContracts(t, chainClient, linkToken, linkFundsForEachUpkeep, upkeepGasLimit, registry, registrar, numberOfContracts, upkeepsAddresses, false, false)
upkeepIds := RegisterUpkeepContracts(t, chainClient, linkToken, linkFundsForEachUpkeep, upkeepGasLimit, registry, registrar, numberOfContracts, upkeepsAddresses, false, false, false, nil)

return registry, registrar, upkeeps, upkeepIds
}
Expand Down Expand Up @@ -236,7 +237,7 @@ func DeployPerformDataCheckerContracts(
upkeepsAddresses = append(upkeepsAddresses, upkeep.Address())
}

upkeepIds := RegisterUpkeepContracts(t, chainClient, linkToken, linkFundsForEachUpkeep, upkeepGasLimit, registry, registrar, numberOfContracts, upkeepsAddresses, false, false)
upkeepIds := RegisterUpkeepContracts(t, chainClient, linkToken, linkFundsForEachUpkeep, upkeepGasLimit, registry, registrar, numberOfContracts, upkeepsAddresses, false, false, false, nil)

return registry, registrar, upkeeps, upkeepIds
}
Expand All @@ -259,14 +260,14 @@ func DeployKeeperRegistrar(
return registrar
}

func RegisterUpkeepContracts(t *testing.T, client *seth.Client, linkToken contracts.LinkToken, linkFunds *big.Int, upkeepGasLimit uint32, registry contracts.KeeperRegistry, registrar contracts.KeeperRegistrar, numberOfContracts int, upkeepAddresses []string, isLogTrigger bool, isMercury bool) []*big.Int {
func RegisterUpkeepContracts(t *testing.T, client *seth.Client, linkToken contracts.LinkToken, fundsForEachUpkeep *big.Int, upkeepGasLimit uint32, registry contracts.KeeperRegistry, registrar contracts.KeeperRegistrar, numberOfContracts int, upkeepAddresses []string, isLogTrigger bool, isMercury bool, isBillingTokenNative bool, wethToken contracts.WETHToken) []*big.Int {
checkData := make([][]byte, 0)
for i := 0; i < numberOfContracts; i++ {
checkData = append(checkData, []byte("0"))
}
return RegisterUpkeepContractsWithCheckData(
t, client, linkToken, linkFunds, upkeepGasLimit, registry, registrar,
numberOfContracts, upkeepAddresses, checkData, isLogTrigger, isMercury)
t, client, linkToken, fundsForEachUpkeep, upkeepGasLimit, registry, registrar,
numberOfContracts, upkeepAddresses, checkData, isLogTrigger, isMercury, isBillingTokenNative, wethToken)
}

type upkeepRegistrationResult struct {
Expand All @@ -284,7 +285,7 @@ type upkeepConfig struct {

type UpkeepId = *big.Int

func RegisterUpkeepContractsWithCheckData(t *testing.T, client *seth.Client, linkToken contracts.LinkToken, linkFunds *big.Int, upkeepGasLimit uint32, registry contracts.KeeperRegistry, registrar contracts.KeeperRegistrar, numberOfContracts int, upkeepAddresses []string, checkData [][]byte, isLogTrigger bool, isMercury bool) []*big.Int {
func RegisterUpkeepContractsWithCheckData(t *testing.T, client *seth.Client, linkToken contracts.LinkToken, fundsForEachUpkeep *big.Int, upkeepGasLimit uint32, registry contracts.KeeperRegistry, registrar contracts.KeeperRegistrar, numberOfContracts int, upkeepAddresses []string, checkData [][]byte, isLogTrigger bool, isMercury bool, isBillingTokenNative bool, wethToken contracts.WETHToken) []*big.Int {
l := logging.GetTestLogger(t)

concurrency, err := GetAndAssertCorrectConcurrency(client, 1)
Expand All @@ -300,45 +301,69 @@ func RegisterUpkeepContractsWithCheckData(t *testing.T, client *seth.Client, lin
var registerUpkeepFn = func(resultCh chan upkeepRegistrationResult, errorCh chan error, executorNum int, config upkeepConfig) {
id := uuid.New().String()
keyNum := executorNum + 1 // key 0 is the root key
var tx *types.Transaction

if isBillingTokenNative {
// register upkeep with native token
tx, err = registrar.RegisterUpkeepFromKey(
keyNum,
fmt.Sprintf("upkeep_%s", id),
[]byte("test@mail.com"),
config.address,
upkeepGasLimit,
client.MustGetRootKeyAddress().Hex(), // upkeep Admin
config.data,
fundsForEachUpkeep,
wethToken.Address(),
isLogTrigger,
isMercury,
)
if err != nil {
errorCh <- errors.Wrapf(err, "[id: %s] Failed to register upkeep at %s", id, config.address)
return
}
} else {
// register upkeep with LINK
req, err := registrar.EncodeRegisterRequest(
fmt.Sprintf("upkeep_%s", id),
[]byte("test@mail.com"),
config.address,
upkeepGasLimit,
client.MustGetRootKeyAddress().Hex(), // upkeep Admin
config.data,
fundsForEachUpkeep,
0,
client.Addresses[keyNum].Hex(),
isLogTrigger,
isMercury,
linkToken.Address(),
)

if err != nil {
errorCh <- errors.Wrapf(err, "[id: %s] Failed to encode register request for upkeep at %s", id, config.address)
return
}

req, err := registrar.EncodeRegisterRequest(
fmt.Sprintf("upkeep_%s", id),
[]byte("test@mail.com"),
config.address,
upkeepGasLimit,
client.MustGetRootKeyAddress().Hex(), // upkeep Admin
config.data,
linkFunds,
0,
client.Addresses[keyNum].Hex(),
isLogTrigger,
isMercury,
linkToken.Address(),
)

if err != nil {
errorCh <- errors.Wrapf(err, "[id: %s] Failed to encode register request for upkeep at %s", id, config.address)
return
}

balance, err := linkToken.BalanceOf(context.Background(), client.Addresses[keyNum].Hex())
if err != nil {
errorCh <- errors.Wrapf(err, "[id: %s]Failed to get LINK balance of %s", id, client.Addresses[keyNum].Hex())
return
}
balance, err := linkToken.BalanceOf(context.Background(), client.Addresses[keyNum].Hex())
if err != nil {
errorCh <- errors.Wrapf(err, "[id: %s]Failed to get LINK balance of %s", id, client.Addresses[keyNum].Hex())
return
}

// not stricly necessary, but helps us to avoid an errorless revert if there is not enough LINK
if balance.Cmp(linkFunds) < 0 {
errorCh <- fmt.Errorf("[id: %s] Not enough LINK balance for %s. Has: %s. Needs: %s", id, client.Addresses[keyNum].Hex(), balance.String(), linkFunds.String())
return
}
// not strictly necessary, but helps us to avoid an errorless revert if there is not enough LINK
if balance.Cmp(fundsForEachUpkeep) < 0 {
errorCh <- fmt.Errorf("[id: %s] Not enough LINK balance for %s. Has: %s. Needs: %s", id, client.Addresses[keyNum].Hex(), balance.String(), fundsForEachUpkeep.String())
return
}

tx, err := linkToken.TransferAndCallFromKey(registrar.Address(), linkFunds, req, keyNum)
if err != nil {
errorCh <- errors.Wrapf(err, "[id: %s] Failed to register upkeep at %s", id, config.address)
return
tx, err = linkToken.TransferAndCallFromKey(registrar.Address(), fundsForEachUpkeep, req, keyNum)
if err != nil {
errorCh <- errors.Wrapf(err, "[id: %s] Failed to register upkeep at %s", id, config.address)
return
}
}

// parse txn to get upkeep ID
receipt, err := client.Client.TransactionReceipt(context.Background(), tx.Hash())
if err != nil {
errorCh <- errors.Wrapf(err, "[id: %s] Failed to get receipt for upkeep at %s and tx hash %s", id, config.address, tx.Hash())
Expand Down Expand Up @@ -405,10 +430,10 @@ func DeployKeeperConsumers(t *testing.T, client *seth.Client, numberOfContracts
// v2.1 only: Conditional based contract with Mercury enabled
keeperConsumerInstance, err = contracts.DeployAutomationStreamsLookupUpkeepConsumerFromKey(client, keyNum, big.NewInt(1000), big.NewInt(5), false, true, false) // 1000 block test range
} else if isLogTrigger {
// v2.1 only: Log triggered based contract without Mercury
// v2.1+: Log triggered based contract without Mercury
keeperConsumerInstance, err = contracts.DeployAutomationLogTriggerConsumerFromKey(client, keyNum, big.NewInt(1000)) // 1000 block test range
} else {
// v2.0 and v2.1: Conditional based contract without Mercury
// v2.0+: Conditional based contract without Mercury
keeperConsumerInstance, err = contracts.DeployUpkeepCounterFromKey(client, keyNum, big.NewInt(999999), big.NewInt(5))
}

Expand Down Expand Up @@ -580,7 +605,7 @@ func RegisterNewUpkeeps(
err = SendLinkFundsToDeploymentAddresses(chainClient, concurrency, numberOfNewUpkeeps, operationsPerAddress, multicallAddress, linkFundsForEachUpkeep, linkToken)
require.NoError(t, err, "Sending link funds to deployment addresses shouldn't fail")

newUpkeepIDs := RegisterUpkeepContracts(t, chainClient, linkToken, linkFundsForEachUpkeep, upkeepGasLimit, registry, registrar, numberOfNewUpkeeps, addressesOfNewUpkeeps, false, false)
newUpkeepIDs := RegisterUpkeepContracts(t, chainClient, linkToken, linkFundsForEachUpkeep, upkeepGasLimit, registry, registrar, numberOfNewUpkeeps, addressesOfNewUpkeeps, false, false, false, nil)

return newlyDeployedUpkeeps, newUpkeepIDs
}
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/chaos/automation_chaos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ func TestAutomationChaos(t *testing.T) {
}
require.NoError(t, err, "Error setting OCR config")

consumersConditional, upkeepidsConditional := actions.DeployConsumers(t, chainClient, registry, registrar, linkToken, numberOfUpkeeps, big.NewInt(defaultLinkFunds), defaultUpkeepGasLimit, false, false)
consumersLogtrigger, upkeepidsLogtrigger := actions.DeployConsumers(t, chainClient, registry, registrar, linkToken, numberOfUpkeeps, big.NewInt(defaultLinkFunds), defaultUpkeepGasLimit, true, false)
consumersConditional, upkeepidsConditional := actions.DeployConsumers(t, chainClient, registry, registrar, linkToken, numberOfUpkeeps, big.NewInt(defaultLinkFunds), defaultUpkeepGasLimit, false, false, false, nil)
consumersLogtrigger, upkeepidsLogtrigger := actions.DeployConsumers(t, chainClient, registry, registrar, linkToken, numberOfUpkeeps, big.NewInt(defaultLinkFunds), defaultUpkeepGasLimit, true, false, false, nil)

consumers := append(consumersConditional, consumersLogtrigger...)
upkeepIDs := append(upkeepidsConditional, upkeepidsLogtrigger...)
Expand Down
83 changes: 83 additions & 0 deletions integration-tests/contracts/ethereum_contracts_automation.go
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,87 @@ func (v *EthereumKeeperRegistrar) Fund(_ *big.Float) error {
panic("do not use this function, use actions.SendFunds instead")
}

// register Upkeep with native token, only available from v2.3
func (v *EthereumKeeperRegistrar) RegisterUpkeepFromKey(keyNum int, name string, email []byte, upkeepAddr string, gasLimit uint32, adminAddr string, checkData []byte, amount *big.Int, wethTokenAddr string, isLogTrigger bool, isMercury bool) (*types.Transaction, error) {
if v.registrar23 == nil {
return nil, fmt.Errorf("RegisterUpkeepFromKey with native token is only supported in registrar version v2.3")
}

registrarABI = cltypes.MustGetABI(registrar23.AutomationRegistrarABI)
txOpts := v.client.NewTXKeyOpts(keyNum, seth.WithValue(amount))

if isLogTrigger {
var topic0InBytes [32]byte
// bytes representation of 0x0000000000000000000000000000000000000000000000000000000000000000
bytes0 := [32]byte{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}
if isMercury {
// bytes representation of 0xd1ffe9e45581c11d7d9f2ed5f75217cd4be9f8b7eee6af0f6d03f46de53956cd
topic0InBytes = [32]byte{209, 255, 233, 228, 85, 129, 193, 29, 125, 159, 46, 213, 247, 82, 23, 205, 75, 233, 248, 183, 238, 230, 175, 15, 109, 3, 244, 109, 229, 57, 86, 205}
} else {
// bytes representation of 0x3d53a39550e04688065827f3bb86584cb007ab9ebca7ebd528e7301c9c31eb5d
topic0InBytes = [32]byte{
61, 83, 163, 149, 80, 224, 70, 136,
6, 88, 39, 243, 187, 134, 88, 76,
176, 7, 171, 158, 188, 167, 235,
213, 40, 231, 48, 28, 156, 49, 235, 93,
}
}

logTriggerConfigStruct := acutils.IAutomationV21PlusCommonLogTriggerConfig{
ContractAddress: common.HexToAddress(upkeepAddr),
FilterSelector: 0,
Topic0: topic0InBytes,
Topic1: bytes0,
Topic2: bytes0,
Topic3: bytes0,
}
encodedLogTriggerConfig, err := compatibleUtils.Methods["_logTriggerConfig"].Inputs.Pack(&logTriggerConfigStruct)
if err != nil {
return nil, err
}

params := registrar23.AutomationRegistrar23RegistrationParams{
UpkeepContract: common.HexToAddress(upkeepAddr),
Amount: amount,
AdminAddress: common.HexToAddress(adminAddr),
GasLimit: gasLimit,
TriggerType: uint8(1), // trigger type
BillingToken: common.HexToAddress(wethTokenAddr), // native
Name: name,
EncryptedEmail: email,
CheckData: checkData,
TriggerConfig: encodedLogTriggerConfig, // log trigger upkeep
OffchainConfig: []byte{},
}

decodedTx, err := v.client.Decode(v.registrar23.RegisterUpkeep(txOpts,
params,
))
return decodedTx.Transaction, err
}

params := registrar23.AutomationRegistrar23RegistrationParams{
UpkeepContract: common.HexToAddress(upkeepAddr),
Amount: amount,
AdminAddress: common.HexToAddress(adminAddr),
GasLimit: gasLimit,
TriggerType: uint8(0), // trigger type
BillingToken: common.HexToAddress(wethTokenAddr), // native
Name: name,
EncryptedEmail: email,
CheckData: checkData,
TriggerConfig: []byte{}, // conditional upkeep
OffchainConfig: []byte{},
}

decodedTx, err := v.client.Decode(v.registrar23.RegisterUpkeep(txOpts,
params,
))
return decodedTx.Transaction, err
}

// EncodeRegisterRequest encodes register request to call it through link token TransferAndCall
func (v *EthereumKeeperRegistrar) EncodeRegisterRequest(name string, email []byte, upkeepAddr string, gasLimit uint32, adminAddr string, checkData []byte, amount *big.Int, source uint8, senderAddr string, isLogTrigger bool, isMercury bool, linkTokenAddr string) ([]byte, error) {
if v.registrar20 != nil {
Expand Down Expand Up @@ -2056,9 +2137,11 @@ func DeployKeeperRegistrar(client *seth.Client, registryVersion eth_contracts.Ke

billingTokens := []common.Address{
common.HexToAddress(linkAddr),
common.HexToAddress(registrarSettings.WETHTokenAddr),
}
minRegistrationFees := []*big.Int{
big.NewInt(10),
big.NewInt(10),
}

data, err := client.DeployContract(client.NewTXOpts(), "KeeperRegistrar2_3", *abi, common.FromHex(registrar23.AutomationRegistrarMetaData.Bin),
Expand Down
Loading

0 comments on commit 4b19e37

Please sign in to comment.