> ## Documentation Index
> Fetch the complete documentation index at: https://cobo.com/developers/llms.txt
> Use this file to discover all available pages before exploring further.

# 自动化收集 Farming 奖励

> 使用智能合约钱包自动化收集 Farming 奖励的指南，包含配置 MPC 钱包和 EOA 钱包的步骤。

<Tip>
  即刻安装 [Cobo WaaS Skill](/developers/v2_cn/guides/overview/cobo-waas-skill)，在 Claude Code、Cursor 等 AI 开发环境中使用自然语言集成 WaaS API，显著提升开发效率 🚀
</Tip>

对于某些低风险、高频率的交易，绕过多重签名要求可以帮助简化操作，尤其是在任务时间紧迫或需要频繁交互时。Cobo 的智能合约钱包（Safe{Wallet}）通过允许您授权一个委托人——可以是您的 [MPC 钱包](https://manuals.cobo.com/cn/portal/mpc-wallets/introduction) 地址或外部 EOA 钱包——以单一签名发起这些操作，从而简化执行而不影响控制。

在本指南中，我们将演示如何配置 MPC 钱包和外部 EOA 钱包以自动领取 Farming 奖励。

## 使用 MPC 钱包作为委托人

假设您在 Base 链上有一个 Safe{Wallet}，并在 Stargate 协议中存入了 ETH 进行收益农业。您可以使用 MPC 钱包通过 [WaaS API](/developers/v2_cn/guides/overview/introduction) 执行自动化、定期的奖励收集。

1. 在开始之前，请参考 [智能合约钱包入门](/developers/v2_cn/guides/smart-contract-wallets/get-started-scw) 完成初始设置，包括安装 WaaS SDK 和将 Safe{Wallet} 导入 Cobo Portal。

2. 配置链上风险控制策略，以自动批准通过 MPC 钱包地址发起的领取方法。

   1. 选择 MPC 钱包地址作为委托人。
   2. 将 **条件** 设置为 **自定义合约调用**。
   3. 选择 **方法级别**。
   4. 输入 Stargate 抵押合约的地址作为目标合约，并选择 **claim** 作为方法。
   5. 点击 **提交** 以发起多签交易。请求 Safe{Wallet} 签名人确认。

   <img src="https://mintcdn.com/cobo-docs/f8feZ1xOlBF47bOF/v2_cn/images/guides/policy-stargate.png?fit=max&auto=format&n=f8feZ1xOlBF47bOF&q=85&s=a0f06364ca1c9c4826dd79c70d5a7213" className="screenshot_full_screen" alt="为 Stargate 奖励收集设置 MPC 作为委托人" width="3712" height="1686" data-path="v2_cn/images/guides/policy-stargate.png" />

3. 执行以下代码以每天上午 9 点领取奖励。示例代码使用 [调用智能合约](/developers/v2/api-references/transactions/call-smart-contract) 操作与 Stargate 合约交互。

<Accordion title="Python 示例代码">
  ```py theme={null}
  import schedule
  import time
  from web3 import Web3
  import cobo_waas2

  # 指定 Stargate 合约地址
  STG_CLAIM_CONTRACT = "0xdfc47dcef7e8f9ab19a1b8af3eecf000c7ea0b80"
  # 从 Etherscan 获取 ABI
  STG_CLAIM_ABI = []
  # 指定 Stargate 池地址
  STG_ETH_POOL = "0x98fB8522d891F43B771e2d27367b41Ba138D0B80"
  w3 = Web3(Web3.HTTPProvider("<RPC_URL>")) 
  contract = w3.eth.contract(address=STG_CLAIM_CONTRACT, abi=STG_CLAIM_ABI)

  data = contract.encodeABI("claim", [[STG_ETH_POOL]])

  def get_delegate(wallet_id, request: cobo_waas2.SafeWalletDelegates):
  # Enter a context with an instance of the API client
      with cobo_waas2.ApiClient(configuration) as api_client:
          # Create an instance of the API class
          wallet_api_instance = cobo_waas2.WalletsSmartContractWalletsApi(api_client)
          try:
              # Call the List Delegates operation to retrieve available Delegates
              api_response = wallet_api_instance.list_safe_wallet_delegates(
                  wallet_id=wallet_id,
                  safe_wallet_delegates=request
              )
              if not api_response:
                  raise Exception("No delegate found")
              print("The response of WalletsApi->list_safe_wallet_delegates:")
              print(json.dumps(api_response[0].to_dict(), indent=2))
              return api_response[0]
          except Exception as e:
              print("Exception when calling WalletsApi: %s\n", e)

  def get_wallet(wallet_id) -> cobo_waas2.SafeWallet:
      with cobo_waas2.ApiClient(configuration) as api_client:
          # Create an instance of the API class
          wallet_api_instance = cobo_waas2.WalletsApi(api_client)
          try:
              # Call the Get wallet information operation to retrieve the wallet information, including the wallet address
              api_response = wallet_api_instance.get_wallet_by_id(
                  wallet_id=wallet_id
              )
              print("The response of WalletsApi->get_wallet_by_id:")
              print(json.dumps(api_response.to_dict(), indent=2))
              return api_response.actual_instance.actual_instance
          except Exception as e:
              print("Exception when calling WalletsApi: %s\n", e)


  def contract_call(wallet_id, contract_address, data, value):
      contract_call_request = cobo_waas2.SafeWalletDelegates(
              actual_instance=cobo_waas2.SafeWalletDelegatesContractCall(
                  request_type=cobo_waas2.EstimateFeeRequestType.CONTRACTCALL,
                  address=contract_address,
                  calldata=data,
                  value=value,
              )
          )
      delegate = get_delegate(wallet_id, contract_call_request)
      wallet = get_wallet(wallet_id)

      with cobo_waas2.ApiClient(configuration) as api_client:
          wallet_api_instance = cobo_waas2.TransactionsApi(api_client)
          try:
              # Use the Call smart contract operation to interact with the Stargate contract
              api_response = wallet_api_instance.create_contract_call_transaction(
                  cobo_waas2.ContractCallParams(
                      request_id=str(uuid.uuid4()),
                      chain_id=wallet.chain_id,
                      source=cobo_waas2.ContractCallSource(
                          actual_instance=cobo_waas2.SafeContractCallSource(
                              source_type=cobo_waas2.ContractCallSourceType.SAFE_WALLET,
                              wallet_id=wallet_id,
                              address=wallet.safe_address,
                              delegate=delegate,
                          )
                      ),
                      destination=cobo_waas2.ContractCallDestination(
                          actual_instance=cobo_waas2.EvmContractCallDestination(
                              destination_type=cobo_waas2.ContractCallDestinationType.EVM_CONTRACT,
                              address=contract_address,
                              calldata=data,
                              value=value,
                          ),
                      ),
                      category_names=["<CATEGORY_NAME>"],
                      description="<DESCRIPTION>",
                  )
              )
              print("The response of TransactionsApi->create_contract_call_transaction:")
              print(json.dumps(api_response.to_dict(), indent=2))
          except Exception as e:
              print("Exception when calling TransactionsApi: %s\n", e)


  def daily_contract_call():
      print("claim stargate")
      contract_call(
          wallet_id=wallet_id,
          contract_address=STG_CLAIM_CONTRACT,
          data=data,
          value="0",
      )

  # Schedule the contract call to run every day at 9:00 a.m.
  schedule.every().day.at("09:00").do(daily_contract_call)

  # Keep the script running
  while True:
      schedule.run_pending()
      time.sleep(1)
  ```
</Accordion>

## 使用外部 EOA 钱包作为委托人

您也可以使用外部 EOA 钱包来领取奖励。初始步骤与 \[MPC 钱包]\(#使用 MPC 钱包作为委托人) 相似，但您需要将 EOA 钱包地址设置为委托人，并相应地调整链上风险控制策略。

与使用 WaaS API 不同，EOA 钱包通过使用 Cobo Safe 提供的函数与 Safe{Wallet} 交互。该过程可以概述如下：

1. 构建领取交易以从 Stargate 收集 Farming 奖励。
2. 将领取交易封装在 Cobo Safe 交易中。
3. 通过 EOA 钱包发起 Cobo Safe 交易。
4. 使用 EOA 钱包签署交易并发送到网络。

<Accordion title="Python 示例代码">
  ```py theme={null}
  import json
  from web3 import Web3
  import cobo_waas2

  import os
  from eth_account import Account

  # 从环境变量获取私钥
  PRIVATE_KEY = os.getenv('ETH_PRIVATE_KEY')
  # 创建账户对象
  account = Account.from_key(PRIVATE_KEY)
  w3 = Web3(Web3.HTTPProvider("<RPC_URL>")) 

  def get_wallet(wallet_id) -> cobo_waas2.SafeWallet:
      with cobo_waas2.ApiClient(configuration) as api_client:
          # Create an instance of the API class
          wallet_api_instance = cobo_waas2.WalletsApi(api_client)
          try:
              # Call the Get wallet information operation to retrieve the wallet information, including the wallet address
              api_response = wallet_api_instance.get_wallet_by_id(
                  wallet_id=wallet_id
              )
              print("The response of WalletsApi->get_wallet_by_id:")
              print(json.dumps(api_response.to_dict(), indent=2))
              return api_response.actual_instance.actual_instance
          except Exception as e:
              print("Exception when calling WalletsApi: %s\n", e)


  # 输入钱包 ID，位于 Cobo Portal 中 
  wallet = get_wallet("<YOUR_WALLET_ID>")
  # 获取 Cobo Safe 地址
  cobo_safe_address = wallet.cobo_safe_address
  # 从 Etherscan 获取 ABI
  COBO_SAFE_ABI = [] 

  # 指定 Stargate 合约地址 
  STG_CLAIM_CONTRACT = "0xdfc47dcef7e8f9ab19a1b8af3eecf000c7ea0b80"
  # 从 Etherscan 获取 ABI
  STG_CLAIM_ABI = []
  STG_ETH_POOL = "0x98fB8522d891F43B771e2d27367b41Ba138D0B80"
  stg_contract = w3.eth.contract(address=STG_CLAIM_CONTRACT, abi=STG_CLAIM_ABI)
  # 构建 Stargate 领取交易
  data = stg_contract.encodeABI("claim", [[STG_ETH_POOL]])

  # 构建 Cobo Safe 交易
  cobo_safe_contract = w3.eth.contract(address=cobo_safe_address, abi=COBO_SAFE_ABI)
  call_datas = [
      [
          0,
          Web3.to_checksum_address(STARGATE_STAKING),
          0,
          Web3.to_bytes(hexstr=data),
          b"",
          b"",
      ]
  ]

  # 构建 EOA 钱包交易。该交易通过 EOA 账户发起 Cobo Safe 交易。
  transaction = cobo_safe_contract.functions.execTransactions(call_datas).build_transaction({
      'from': account.address,
      'nonce': w3.eth.get_transaction_count(account.address),
      # 设置 gas 费用限制
      'gas': 200000,
      'gasPrice': w3.eth.gas_price,
  })

  # 签署交易
  signed_tx = account.sign_transaction(transaction)
  # 发送交易 
  tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
  tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
  print(f"Transaction sent with hash: {tx_hash.hex()}")
  ```
</Accordion>
