AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题

问题[solidity](coding)

Martin Hope
Phc Chu Vaky Ramadan
Asked: 2025-04-02 11:38:53 +0800 CST

Chainlink VRF:链上和仪表板之间创建订阅 ID 不匹配

  • 5

我遇到一个问题,我的脚本返回的订阅 ID 与 Sepolia 网络的 vrf.chain.link 上显示的订阅 ID 不同,当我尝试从我的脚本为订阅 ID 提供资金时,将导致 InvalidSubscriptionId。

测试创建订阅.s.sol:

// SPDX-License-Identifier: MIT

pragma solidity  ^0.8.19;
import {Script} from "forge-std/Script.sol";
import {CodeConstants} from "script/HelperConfig.s.sol";
import {IVRFCoordinatorV2Plus} from "@chainlink/contracts/src/v0.8/vrf/dev/interfaces/IVRFCoordinatorV2Plus.sol";
import {console} from "forge-std/console.sol";

contract CreateSubscription is Script,CodeConstants{
    function run() public{
        createsubscription_onsepolia();
    }

    function createsubscription_onsepolia() public {
        if(block.chainid == ETH_SEPOLIA_CHAN_ID){
            vm.startBroadcast(0x57099326F00d72E00dc4416FF638136853E5330e);
            uint256 subid = IVRFCoordinatorV2Plus(0x9DdfaCa8183c41ad55329BdeeD9F6A8d53168B1B).createSubscription();
            vm.stopBroadcast();
            console.log("subid on sepolia: ",subid);
        }
    }
}

当我运行 Forge 脚本 script/Testcreatesubscription.s.sol:CreateSubscription --rpc-url $SEPOLIA_RPC_URL --account sepolia_testing_account --broadcast -vvvv

sepolia_testing_account 是我的 metamask 帐户,我将其存储在密钥库中。

日志结果:

== 日志 == sepolia 上的 subid: 1756636573672805867076888413.....

设置 1 个 EVM。

模拟链上轨迹:

[88720] VRFCoordinatorV2_5::createSubscription() ═─ 发出 SubscriptionCreated(subId: 175663657367280586707688841388762055..... [1.756e76], 所有者: 0x57099....) └─ ← [返回] 1756636573672805867076888413887... [1.756e76]

==========================

链条 11155111

预计天然气价格:0.724514652 gwei

脚本预计消耗的 gas 总量:151638

预计所需金额:0.000109863952799976 ETH

============================ 输入密钥库密码:⠂ sepolia 上的序列 #1

sepolia ✅ [成功] 哈希:0x93a569514837d5d7dd8f525e1fdf105b541fde506522cf9c6b7d0173779c0da7 区块:8028451 已付款:0.000043162465117744 ETH(109784 gas * 0.393158066 gwei)

✅ sepolia 上的序列#1 | 总付款:0.000043162465117744 ETH(109784 gas * 平均 0.393158066 gwei)

尽管脚本日志显示我的订阅 ID 是 175663657367280586707688841388762055...,但当我在 Sepolia 网络上检查 vrf.chain.link 时,我看到一个完全不同的订阅 ID:426994294831235659204431318963817900788974.....

我已经为此苦苦挣扎了好几个小时,甚至连 ChatGPT 都搞不清楚。有人知道我可能做错了什么吗?任何帮助都将不胜感激!

solidity
  • 1 个回答
  • 28 Views
Martin Hope
Spectrecoder
Asked: 2024-12-20 23:37:16 +0800 CST

Chainlink 函数合约中 Fulfill 函数无法调用外部 API

  • 5

现在,我正在尝试在 Chainlink Function Contract 上调用外部 API。我创建了一个 API,并使用 Postman 对其进行了测试。效果很好。

这是 API 的响应。

{ “价格”:11192.32 }

现在我正在尝试调用 chainlink 合约中的 API。

这是合约代码。

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

import {Chainlink, ChainlinkClient} from "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
import {ConfirmedOwner} from "@chainlink/contracts/src/v0.8/shared/access/ConfirmedOwner.sol";
import {LinkTokenInterface} from "@chainlink/contracts/src/v0.8/shared/interfaces/LinkTokenInterface.sol";

contract RolexPriceConsumer is ChainlinkClient, ConfirmedOwner {
    using Chainlink for Chainlink.Request;

    uint256 public price;
    address private oracle;
    bytes32 private jobId;
    uint256 private fee;

    event RequestPrice(bytes32 indexed requestId, uint256 price);

    constructor() ConfirmedOwner(msg.sender) {
        _setChainlinkToken(0x779877A7B0D9E8603169DdbD7836e478b4624789);
        _setChainlinkOracle(0x6090149792dAAeE9D1D568c9f9a6F6B46AA29eFD);
        jobId = "ca98366cc7314957b8c012c72f05aeeb";
        fee = 0.1 * 10 ** 18;
    }

    function requestRolexPrice(
        string memory _url
    ) public returns (bytes32 requestId) {
        Chainlink.Request memory req = _buildChainlinkRequest(
            jobId,
            address(this),
            this.fulfill.selector  
        );

        req._add("get", _url);
        req._add("multiply", "100000000");
        req._add("path", "price");

        requestId = _sendChainlinkRequest(req, fee);
    }

    function fulfill(
        bytes32 _requestId,
        uint256 _price
    ) public recordChainlinkFulfillment(_requestId) {
        emit RequestPrice(_requestId, _price);
        price = _price;
    }

    function getLatestPrice() public view returns (uint256) {
        return price;
    }

    function withdrawLink() public onlyOwner {
        LinkTokenInterface link = LinkTokenInterface(_chainlinkTokenAddress());
        require(
            link.transfer(msg.sender, link.balanceOf(address(this))),
            "Unable to transfer"
        );
    }
}

我在Sepolia Testnet上部署了这个合约,并验证了它。当我部署这个合约时,API被调用。但是Sepolia Scan中的getlatestPrice返回了0。

我认为 finish 函数调用不正确。但我不太确定。如果您能帮助我,我将不胜感激。

我运行了这个代码

function requestRolexPrice(
    string memory _url
) public returns (bytes32) {
    Chainlink.Request memory req = _buildChainlinkRequest(
        jobId,
        address(this),
        this.fulfill.selector  
    );

    req.add("get", _url);        // Use `add` instead of `_add`
    req.add("path", "price");    // Correct method call
    req.add("multiply", "100000000");

    // Send the request and store the requestId
    bytes32 requestId = _sendChainlinkRequest(req, fee);

    // Return the requestId
    return requestId;
}

solidity
  • 1 个回答
  • 21 Views
Martin Hope
ZettaZet YT
Asked: 2024-08-03 23:36:10 +0800 CST

用于提取余额的 Solidity 简单合约

  • 6

我写了一个 solidity 合约,用于提取合约余额。有人能告诉我它是否有效吗?withdrawSafe应该可以防止重入攻击,但我不知道它是否有效。只需检查一切是否正常,然后请提出改进​​建议。可能会犯一些愚蠢的错误。

// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.19;

event Response(bool success, bytes data);


interface IVault {
    function deposit() external payable;

    function withdrawSafe(address payable holder) external;

    function withdrawUnsafe(address payable holder) external;
}

interface IAttacker {
    function depositToVault(address vault) external payable;

    function attack(address vault) external;
}

contract Vault is IVault {

    bool private _entered;

     modifier nonReentrant {
        require(!_entered, "re-entrant call");
        _entered = true;
        _;
        _entered = false;
    }

    mapping(address => uint256) public balance;

    function deposit() external payable {
        balance[msg.sender] += msg.value;
    }

    function withdrawSafe(address payable holder) external nonReentrant {
        (bool success, bytes memory data) = holder.call{value: balance[msg.sender], gas: 5000}
        (abi.encodeWithSignature("withdrawSafe(string,uint256)", "call WS", 123));
        emit Response(success, data);
    }

    function withdrawUnsafe(address payable holder) external {
        (bool success, bytes memory data) = holder.call{value: balance[msg.sender], gas: 5000}
        (abi.encodeWithSignature("withdrawSafe(string,uint256)", "call WS", 123));
        emit Response(success, data);
    }
}

此外,如果有人知道如何在没有任何测试网或主网链的情况下在 Remix 中测试合约,或者可能推荐另一个 IDE,我会很高兴听到任何建议。

solidity
  • 1 个回答
  • 15 Views
Martin Hope
János Barna
Asked: 2023-12-07 19:41:33 +0800 CST

在我的智能合约中看不到(Chainlink)请求的数据

  • 5

我正在尝试从 Chainlink 外部适配器获取智能合约中的数据:

获取桥 "{"data":{"result":{"id":"UCAl9Ld79qaZxp9JzEOwd3aA","statistics":{"subscriberCount":"209000"}}},"jobRunId":"0x84aae6f7b7144fbc8196230158918797","statusCode": 200}”

姓名:玉兔

requestData: {"id": $(jobSpec.externalJobID), "data": { "chid": $(decode_cbor.chid)},"key": $(decode_cbor.key)}

解析 jsonparse "209000"

路径:数据、结果、统计、订阅者计数

数据:$(获取)

编码数据ethabiencode“0x0000000000000000000000000000000000000000000000000000000000033068”

abi:(uint256 值)

数据:{“值”:$(解析)}

编码_tx ethabiencode“0x4ab0d1902a02a37be17119894c2d9400f9bcf563bd4be47496af9a493a96eb412676916c000000000000000000000000000000000000000000000 00000de0b6b3a76400000000000000000000000000002e495ea15b191aff2dea171abba537dc597d6f117642d3750000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000006571ad1800000000000000000000000000000000000000000000 000000000000000033068"

abi:fullOracleRequest(bytes32 requestId,uint256付款,地址callbackAddress,bytes4callbackFunctionId,uint256到期,bytes32数据)

数据:{“requestId”:$(decode_log.requestId),“付款”:$(decode_log. payment),“callbackAddress”:$(decode_log.callbackAddr),“callbackFunctionId”:$(decode_log.callbackFunctionId),“到期” :$(decode_log.cancelExpiration),“数据”:$(encode_data)}

将 tx ethtx 提交至:0x34b33d85A6a642A6Eaa1fE554f576c6122FF64b6

数据:$(encode_tx)

我正在尝试获取以下智能合约中的订阅者数量: // SPDX-License-Identifier: MIT pragma Solidity ^0.8.7;

导入“@chainlink/contracts/src/v0.8/ChainlinkClient.sol”;导入“@chainlink/contracts/src/v0.8/ConfirmedOwner.sol”;

/**

  • 这是使用未经审核代码的示例合同。
  • 请勿在生产中使用此代码。*/

合约 ConsumerContract 是 ChainlinkClient,ConfirmedOwner { 使用 Chainlink for Chainlink.Request;

uint256 private constant ORACLE_PAYMENT = 1 * LINK_DIVISIBILITY; // 1 * 10**18
uint256 public lastRetrievedSubscriberCount;

event RequestForSubscriberCountFulfilled(
    bytes32 indexed requestId,
    uint256 indexed subscriberCount
);

/**
 *  Goerli
 * @dev LINK address in Sepholia network: 0x779877A7B0D9E8603169DdbD7836e478b4624789
 * @dev Check https://docs.chain.link/docs/link-token-contracts/ for LINK address for the right network
 */
constructor() ConfirmedOwner(msg.sender) {
    setChainlinkToken(0x779877A7B0D9E8603169DdbD7836e478b4624789);
}

function requestSubscriberCount(
    address _oracle,
    string memory _jobId,
    string memory channelId,
    string memory apiKey
) public onlyOwner {
    Chainlink.Request memory req = buildOperatorRequest(
        stringToBytes32(_jobId),
        this.fulfillRequestInfo.selector
    );

    req.add("chid", channelId);
    req.add("key", apiKey);
    sendOperatorRequestTo(_oracle, req, ORACLE_PAYMENT);
}

function fulfillRequestInfo(bytes32 _requestId, string memory _info)
    public
    recordChainlinkFulfillment(_requestId)
{
    // Parse the JSON data to extract the subscriberCount
    (uint256 subscriberCount, ) = abi.decode(bytes(_info), (uint256, uint256));

    emit RequestForSubscriberCountFulfilled(_requestId, subscriberCount);
    lastRetrievedSubscriberCount = subscriberCount;
}

function getSubscriberCount() public view returns (uint256) {
    return lastRetrievedSubscriberCount;
}

/*
========= UTILITY FUNCTIONS ==========
*/

function contractBalances()
    public
    view
    returns (uint256 eth, uint256 link)
{
    eth = address(this).balance;

    LinkTokenInterface linkContract = LinkTokenInterface(
        chainlinkTokenAddress()
    );
    link = linkContract.balanceOf(address(this));
}

function getChainlinkToken() public view returns (address) {
    return chainlinkTokenAddress();
}

function withdrawLink() public onlyOwner {
    LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
    require(
        link.transfer(msg.sender, link.balanceOf(address(this))),
        "Unable to transfer Link"
    );
}

function withdrawBalance() public onlyOwner {
    payable(msg.sender).transfer(address(this).balance);
}

function cancelRequest(
    bytes32 _requestId,
    uint256 _payment,
    bytes4 _callbackFunctionId,
    uint256 _expiration
) public onlyOwner {
    cancelChainlinkRequest(
        _requestId,
        _payment,
        _callbackFunctionId,
        _expiration
    );
}

function stringToBytes32(string memory source)
    private
    pure
    returns (bytes32 result)
{
    bytes memory tempEmptyStringTest = bytes(source);
    if (tempEmptyStringTest.length == 0) {
        return 0x0;
    }

    assembly {
        // solhint-disable-line no-inline-assembly
        result := mload(add(source, 32))
    }
}

}

solidity
  • 1 个回答
  • 21 Views
Martin Hope
Liam Ryan
Asked: 2023-08-25 04:50:14 +0800 CST

安全帽安装正在下降安装

  • 5

我正在尝试运行cmd

npm install --save-dev "hardhat@^2.17.1" "@nomicfoundation/hardhat-toolbox@^3.0.0"

运行时我收到此错误日志

 npm install --save-dev "hardhat@^2.17.1" "@nomicfoundation/hardhat-toolbox@^3.0.0"
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/ethers
npm ERR!   ethers@"^5.7.2" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer ethers@"^6.1.0" from @nomicfoundation/[email protected]
npm ERR! node_modules/@nomicfoundation/hardhat-ethers
npm ERR!   peer @nomicfoundation/hardhat-ethers@"^3.0.0" from @nomicfoundation/[email protected]
npm ERR!   node_modules/@nomicfoundation/hardhat-toolbox
npm ERR!     dev @nomicfoundation/hardhat-toolbox@"3.0.0" from the root project
npm ERR!   peer @nomicfoundation/hardhat-ethers@"^3.0.0" from @nomicfoundation/[email protected]
npm ERR!   node_modules/@nomicfoundation/hardhat-chai-matchers
npm ERR!     peer @nomicfoundation/hardhat-chai-matchers@"^2.0.0" from @nomicfoundation/[email protected]
npm ERR!     node_modules/@nomicfoundation/hardhat-toolbox
npm ERR!       dev @nomicfoundation/hardhat-toolbox@"3.0.0" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!
npm ERR!
npm ERR! For a full report see:
npm ERR! C:\Users\\AppData\Local\npm-cache\_logs\2023-08-24T20_39_45_411Z-eresolve-report.txt

npm ERR! A complete log of this run can be found in: C:\Users\liamr\AppData\Local\npm-cache\_logs\2023-08-24T20_39_45_411Z-debug-0.log
PS C:\Users\liamr\tracking>

我首先做了npm i hardhat,没有任何问题,然后我做了npx hardahat,满足了所有要求,然后它说 You need to install these dependencies to run the sample project: npm install --save-dev "hardhat@^2.17.1" "@nomicfoundation/hardhat-toolbox@^3.0.0",我不小心做了 npm install --save-dev "hardhat@^2.17.1" 没有最后一个安全帽工具箱,然后我意识到我的错误,我现在被困在这里。

solidity
  • 1 个回答
  • 16 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve