首页
欢迎来到Solidity社区论坛
帖子
39- 专家问答Mar 29, 2025
How to generate random Number between 1&2 without VRF?
So I was making a coinflip game which assigns head- 1 and tails -2 and when user places bet on polygon chain.... smart contract generates a random number between 1 and 2 and if the outcome is same...user gets double - (5 % as fee to the owner of contract) ! from a pool and if outcome is different then user loses all and the lost amount is deposited in the pool - (3% as fee to the owner) (to fund winners)... but any way to make genrate a random number? I have no funds at all so I can't afford chainlink VRF to generate. So is there any other secure way for this? plus I am pritty new to solidity so I made this code with help of chat gpt but I don't it works....can anyone help to make the code better? code- // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract CoinFlip { address public owner; uint256 public contractBalance; event BetPlaced(address indexed player, uint256 amount, bool choice); event BetResult(address indexed player, uint256 amount, bool won); constructor() { owner = msg.sender; } function placeBet(bool choice) external payable { require(msg.value > 0, "Bet amount must be greater than 0"); bool outcome = (block.timestamp % 2 == 0); // Simple pseudo-random outcome if (outcome == choice) { uint256 winAmount = (msg.value * 2 * 95) / 100; require(address(this).balance >= winAmount, "Insufficient contract balance"); payable(msg.sender).transfer(winAmount); emit BetResult(msg.sender, winAmount, true); } else { uint256 lostAmount = (msg.value * 97) / 100; contractBalance += lostAmount; emit BetResult(msg.sender, msg.value, false); } emit BetPlaced(msg.sender, msg.value, choice); } function depositFunds() external payable { require(msg.sender == owner, "Only owner can deposit funds"); contractBalance += msg.value; } function withdrawFunds(uint256 amount) external { require(msg.sender == owner, "Only owner can withdraw funds"); require(amount <= address(this).balance, "Insufficient contract balance"); payable(owner).transfer(amount); contractBalance -= amount; } } `
- Smart Contract
- Solidity
00 - 专家问答Mar 29, 2025
如何在没有VRF的情况下生成介于1和2之间的随机数?
所以我正在做一个投币游戏,它分配正面-1和反面-2,当用户在多边形链上下注时... 智能合约会生成一个介于1到2之间的随机数,如果结果相同... 用户将获得双倍奖励-(向合约所有者收取5%的费用)!如果结果不同,则用户将全部输掉,损失的金额将存入池中-(向所有者收取3%的费用)(向获胜者提供资金)... 但是有什么办法可以生成一个随机数吗?我根本没有资金,所以我负担不起 chainlink VRF 来生成. 那么还有其他安全的方法可以解决这个问题吗?再加上我对 solidity 还很陌生所以我在聊天 gpt 的帮助下制作了这个代码但我没用... 有人能帮忙把代码做得更好吗?代码- // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract CoinFlip { address public owner; uint256 public contractBalance; event BetPlaced(address indexed player, uint256 amount, bool choice); event BetResult(address indexed player, uint256 amount, bool won); constructor() { owner = msg.sender; } function placeBet(bool choice) external payable { require(msg.value > 0, "Bet amount must be greater than 0"); bool outcome = (block.timestamp % 2 == 0); // Simple pseudo-random outcome if (outcome == choice) { uint256 winAmount = (msg.value * 2 * 95) / 100; require(address(this).balance >= winAmount, "Insufficient contract balance"); payable(msg.sender).transfer(winAmount); emit BetResult(msg.sender, winAmount, true); } else { uint256 lostAmount = (msg.value * 97) / 100; contractBalance += lostAmount; emit BetResult(msg.sender, msg.value, false); } emit BetPlaced(msg.sender, msg.value, choice); } function depositFunds() external payable { require(msg.sender == owner, "Only owner can deposit funds"); contractBalance += msg.value; } function withdrawFunds(uint256 amount) external { require(msg.sender == owner, "Only owner can withdraw funds"); require(amount <= address(this).balance, "Insufficient contract balance"); payable(owner).transfer(amount); contractBalance -= amount; } } `
- Smart Contract
- Solidity
00 - 讨论Feb 24, 2025
大家好,我是新来的. 我希望有人能给我链接来有效地学习 solidity.
//SDPX 许可证标识符:MIT pragma solidity 0.8.24; 合同《你好世界》 { 字符串公开 Hello World; } //只需要在这里添加一些东西
- Smart Contract
- Solidity
02 - 讨论Dec 09, 2024
Solidity 初学者
在上了 cyfrin updraft 的初学者课程之后,我正在尝试建造一些东西,这样我就可以巩固知识了,但好像我什么都不知道. 真的让我难过. 你们会给什么建议
- Smart Contract
- Solidity
23最佳答案 - 讨论Sep 13, 2024
为初学者 Solidity 开发人员寻求自由职业机会”
我已经学习 Solidity 几个星期了,以前没有 IT 背景. 我是否有机会获得自由职业或用我目前的技能赚钱?
- Solidity
- Use cases
00 - 讨论Jul 24, 2024
格式化/你好
我是 Solidity 的新手!我想向这个很棒的社区打个招呼,希望它能让我摆脱困境,希望结识新朋友. 我的编程经验为零,但喜欢加密. 在这些论坛上发布问题时,我应该以某种格式发布问题,和/或在这些论坛中我应该遵循什么不太明显的规则? 感谢您抽出时间阅读并留下回复!
- Smart Contract
- Solidity
- Use cases
01 - 讨论Jul 13, 2024
WEB3 审计的职业道路:首先是项目建设还是学习审计?
大家好, 我最近用 Cyfrin Updraft 完成了 Solidity 和 Foundry 的基础和高级课程. 我正在考虑下一步行动,并考虑在 Solidity 工作. 我应该在这个阶段开始学习智能合约审计,还是先创建一些项目来巩固我目前的知识会更有益? 期待你的建议!
- Smart Contract
- Solidity
01 - 专家问答Jun 04, 2024
solidity 语法多久更改一次
我想知道 solidity 语法规则多久更改一次,以及跟进最新更新的最佳时事通讯
- Smart Contract
- Solidity
- Language Grammar
- Solidity Compiler
11 - 讨论May 31, 2024
重入漏洞
大家好,我注意到优惠函数使用.call {value: amount} (“”) 方法将以太币发送到收件人的地址. 谁能深入了解该功能是否确实容易受到重入的影响,并提出任何最佳做法或修改以降低此类风险吗? function _offer(address to, uint256 amount) internal { balance -= amount; (bool success, ) = to.call{value: amount}(""); if (!success) { revert TransferFailed(address(0), address(this), to, amount); } } `
- Smart Contract
- Solidity
- Solidity Compiler
01
- 76
- 50
- 46
- 37
- 36
- 25
- 25
- 25
- 20
- 18
- Solidity
- Smart Contract
- Solidity Compiler
- Use cases
- Yul
- Optimizer
- Language Grammar
- Internals
- Private