Upgradable Proxy on BSC #1486
Answered
by
cromewar
Parham-dev
asked this question in
Q&A
-
Hi, from scripts.helpful_scripts import get_account, encode_function_data, upgrade, get_contract, fund_with_link
from brownie import network, Lottery, ProxyAdmin, TransparentUpgradeableProxy, Contract, LotteryV2, config, accounts
def deploy_lottery():
account = get_account(id="bsc_test")
lottery = Lottery.deploy(
get_contract("bnb_usd_price_feed").address,
get_contract("vrf_coordinator").address,
get_contract("link_token").address,
config["networks"][network.show_active()]["fee"],
config["networks"][network.show_active()]["keyhash"],
{"from": account},
publish_source=config["networks"][network.show_active()].get("verify", False),
)
print("Deployed lottery!")
return lottery
def main():
account = get_account(id="bsc_test")
# print(f"Deploying to {network.show_active()}")
lottery = Lottery.deploy(
get_contract("bnb_usd_price_feed").address,
get_contract("vrf_coordinator").address,
get_contract("link_token").address,
config["networks"][network.show_active()]["fee"],
config["networks"][network.show_active()]["keyhash"],
{"from": account},
#publish_source=config["networks"][network.show_active()].get("verify", False),
)
proxy_admin = ProxyAdmin.deploy({"from": account})
#initialiser = box.store, 1
box_encoded_initialiser_function = encode_function_data()
proxy = TransparentUpgradeableProxy.deploy(
lottery.address,
proxy_admin.address,
box_encoded_initialiser_function,
{"from": account, "gas_limit": 1000000})
print(f"proxy deployed to {proxy}, we can now upgrade to v2.")
print(f"Lottery {Lottery}")
proxy_lottery = Contract.from_abi("Lottery", proxy.address, Lottery[-1].abi)
# enter lottery
print("before enter for first time")
account = get_account(id="bsc_test")
print(proxy_lottery.poo())
value = proxy_lottery.getEntranceFee() + 100000000
tx = proxy_lottery.enter({"from": account, "value": value})
tx.wait(1)
print("You entered the lottery!")
print("after enter for first time")
# proxy_lottery.store(1, {"from": account})
# print(proxy_lottery.retreive())
lottery_v2 = LotteryV2.deploy(
get_contract("bnb_usd_price_feed").address,
get_contract("vrf_coordinator").address,
get_contract("link_token").address,
config["networks"][network.show_active()]["fee"],
config["networks"][network.show_active()]["keyhash"],
{"from": account},
publish_source=config["networks"][network.show_active()].get("verify", False),
)
upgrade_transaction = upgrade(account, proxy, lottery_v2.address, proxy_admin_contract=proxy_admin)
upgrade_transaction.wait(1)
print("proxy has been upgraded!")
proxy_lottery_v2 = Contract.from_abi("BoxV2", proxy.address, lottery_v2.abi)
print("before enter for second time")
enter_lottery(proxy_lottery_v2)
print("after enter for second time")
# proxy = TransparentUpgradeableProxy[0]
# proxy_lottery_v2 = Contract.from_abi("BoxV2", proxy.address, LotteryV2[0].abi)
# proxy_lottery_v2.balance()
print(proxy_lottery_v2.parham()) ` and here is my lottery implmentation: // SPDX-License-Identifier: MIT
pragma solidity ^0.8.6;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "@chainlink/contracts/src/v0.8/VRFConsumerBase.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract Lottery is VRFConsumerBase, Ownable {
address payable[] public players;
address payable public recentWinner;
uint256 public usdEntryFee;
uint256 public randomness;
uint256 public fee;
bytes32 public keyhash;
AggregatorV3Interface internal bnbUsdPriceFeed;
enum LOTTERY_STATE {
OPEN,
CLOSED,
CALCULATING_WINNER
}
LOTTERY_STATE public lotteryState;
event RequestedRandomness(bytes32 requestId);
constructor(
address _priceFeedAddress,
address _vrfCoordinator,
address _link,
uint256 _fee,
bytes32 _keyhash
) public VRFConsumerBase(_vrfCoordinator, _link) {
usdEntryFee = 50 * (10**18);
bnbUsdPriceFeed = AggregatorV3Interface(_priceFeedAddress);
lotteryState = LOTTERY_STATE.CLOSED;
fee = _fee;
keyhash = _keyhash;
}
function enter() public payable {
require(lotteryState == LOTTERY_STATE.OPEN, "This lottery is closed/Calculating the winner Now. Please check for another one.");
require(msg.value >= getEntranceFee(), "Not Enough Money");
players.push(payable(msg.sender));
}
function getEntranceFee() public view returns (uint256) {
(,int256 price,,,) = bnbUsdPriceFeed.latestRoundData();
uint256 adjustedPrice = uint256(price) * 10**10; // 18 decimals
uint256 costToEnter = (usdEntryFee * 10**18) / adjustedPrice;
return costToEnter;
}
function getPlayers() public view returns (address payable[] memory) {
return players;
}
function poo() public view returns (uint256) {
(,int256 price,,,) = bnbUsdPriceFeed.latestRoundData();
return uint256(price);
}
function startLottery() public {
require(lotteryState == LOTTERY_STATE.CLOSED, "This is an active lottery you cannot start it again.");
lotteryState = LOTTERY_STATE.OPEN;
}
function endLottery() public {
lotteryState = LOTTERY_STATE.CALCULATING_WINNER;
bytes32 requestId = requestRandomness(keyhash, fee);
emit RequestedRandomness(requestId);
}
function fulfillRandomness(bytes32 _requestId, uint256 _randomness)
internal
override
{
require(
lotteryState == LOTTERY_STATE.CALCULATING_WINNER,
"This lottery is closed/Calculating the winner Now. Please check for another one."
);
require(_randomness > 0, "random-not-found");
uint256 indexOfWinner = _randomness % players.length;
recentWinner = players[indexOfWinner];
recentWinner.transfer(address(this).balance);
// Reset
players = new address payable[](0);
lotteryState = LOTTERY_STATE.CLOSED;
randomness = _randomness;
}
}`
and here is my config file: dependencies:
- smartcontractkit/[email protected]
- OpenZeppelin/[email protected]
compiler:
solc:
remappings:
- '@chainlink=smartcontractkit/[email protected]'
- '@openzeppelin=OpenZeppelin/[email protected]'
dotenv: .env
networks:
default: development
development:
keyhash: '0x2ed0feb3e7fd2022120aa84fab1945545a9f2ffc9076fd6156fa96eaff4c1311'
fee: 100000000000000000
rinkeby:
vrf_coordinator: '0xb3dCcb4Cf7a26f6cf6B120Cf5A73875B7BBc655B'
bnb_usd_price_feed: '0x8A753747A1Fa494EC906cE90E9f37563A8AF630e'
link_token: '0x01BE23585060835E02B77ef475b0Cc51aA1e0709'
keyhash: '0x2ed0feb3e7fd2022120aa84fab1945545a9f2ffc9076fd6156fa96eaff4c1311'
fee: 100000000000000000
verify: True
mainnet-fork:
bnb_usd_price_feed: '0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419'
verify: False
bsc-test:
bnb_usd_price_feed: '0x2514895c72f50D8bd4B4F9b1110F0D6bD2c97526'
vrf_coordinator: '0x6A2AAd07396B36Fe02a22b33cf443582f682c82f'
link_token: '0x84b9B910527Ad5C03A9Ca831909E21e236EA7b06'
keyhash: '0xd4bb89654db74673a187bd804519e65e3f71a52bc55f11da7601a13dcf505314'
fee: 100000000000000000
verify: True
wallets:
from_key: ${PRIVATE_KEY} |
Beta Was this translation helpful? Give feedback.
Answered by
cromewar
May 17, 2022
Replies: 1 comment 7 replies
-
Hello @Parham-dev |
Beta Was this translation helpful? Give feedback.
7 replies
Answer selected by
cromewar
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @Parham-dev
Please also share the complete error log in order to find the course of the problem, thanks.