Smart Contract Lottery throws "VirtualMachineError: revert" when ending the lottery - solidity

I have been following Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial (https://www.youtube.com/watch?v=M576WGiDBdQ&t=28658s). When I run deploy_lottery.py, it breaks down while ending the lottery. Here is the snippet of code in Lottery.sol
function endLottery() public onlyOwner {
lottery_state = LOTTERY_STATE.CALCULATING_WINNER;
bytes32 requestId = requestRandomness(keyhash, fee);
emit RequestedRandomness(requestId);
}
Here is the code snippet in deploy_lottery.py
def end_lottery():
account = get_account()
lottery = Lottery[-1]
# fund the contract
# then end the lottery
tx = fund_with_link(lottery.address)
tx.wait(1)
print("Here")
ending_transaction = lottery.endLottery({"from": account})
print("ended transaction")
ending_transaction.wait(1)
time.sleep(180)
print(f"{lottery.recentWinner()} is the new winner!")
I have also attached snapshot of error. Thanks in advance.
https://i.stack.imgur.com/yU8jC.png

I have been stuck with the same error. The solution was to fund the contract with sufficient token. For testing I did
amount=5000000000000000000 in fund_with_link()
def fund_with_link(contract_address, account=None, link_token=None, amount=5000000000000000000):
account = account if account else get_account()
link_token = link_token if link_token else get_contract("link_token")
tx = link_token.transfer(contract_address, amount, {"from": account})
tx.wait(1)
print("Contract Funded")
return tx

Related

UniswapV2 pairFor gives wrong contract address

When i try to add liquidity function fails because pairFor returns wrong pair address
original code (not working on ganache, hardhat and testnets)
(address token0, address token1) = sortTokens(tokenA, tokenB);
pair = address(uint(keccak256(abi.encodePacked(
hex'ff',
factory,
keccak256(abi.encodePacked(token0, token1)),
hex'96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f' // init code hash
))));
}
found some similar issues on stackoverflow and they suggested changing init code hash
therefore i used bytes32 public constant INIT_CODE_PAIR_HASH = keccak256(abi.encodePacked(type(UniswapV2Pair).creationCode))
and it still doesn't work
My modified code with new init hash
(address token0, address token1) = sortTokens(tokenA, tokenB);
pair = address(uint(keccak256(abi.encodePacked(
hex'ff',
factory,
keccak256(abi.encodePacked(token0, token1)),
'0x932b4f40ffd7547443affda5e84a39f3efc69f2ca17d46f0f9427e015f0fb178' // init code hash
))));
}```

how to listen for buy events of my bsc token?

I was just trying to create something which will listen to buy(pancakeswap) events of a specific token like SafeMoon & notify me when someone buys it on pancakeswap.
My progress so far.
The Way I Am Doing Now Is Finding Pancakeswap Pair Address Of A Token And Listen For Its Swap Events
pair_address = '0xBc9d0929c5a1D21BbAaB8826c0a6a78e096702A4' #Pair Address Of ORAKLER/WBNB On Pancakeswap
contract = web3.eth.contract(address=web3.toChecksumAddress(pair_address), abi=helper.getTokenAbi(pair_address))
def handle_event(event):
result = Web3.toJSON(event)
main_base = json.loads(result)
txn_hash = main_base['transactionHash']
print(result)
async def log_loop(event_filter, poll_interval):
while True:
for PairCreated in event_filter.get_new_entries():
handle_event(PairCreated)
await asyncio.sleep(poll_interval)
def main():
event_filter = contract.events.Swap.createFilter(fromBlock='latest')
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(
asyncio.gather(
log_loop(event_filter, 2)))
finally:
loop.close()
if __name__ == "__main__":
main()
In the above code, I am listing for Swap Event Of A Smart Contract And The Output I Am Getting Is
{"args": {"sender": "0x10ED43C718714eb63d5aA57B78B54704E256024E", "to": "0x4C7369b0615125481E2D6Fcd39e4d8c70DB2e830", "amount0In": 0, "amount1In": 4957805606627501, "amount0Out": 200000000000000000, "amount1Out": 0}, "event": "Swap", "logIndex": 339, "transactionIndex": 102, "transactionHash": "0x694f61f705d2fa49d6b16f9d56902f6e4b50c88e9d3adb4ab6fbea6632b0eb1b", "address": "0xBc9d0929c5a1D21BbAaB8826c0a6a78e096702A4", "blockHash": "0x6aedadf8d3618a1d21a48890d7bcfd9968df575a1a56323830f5dd242c79cdd3", "blockNumber": 14269884}
It contains Swap Event Parameter And They Look Like That
Swap (
index_topic_1 address sender,
uint256 amount0In,
uint256 amount1In,
uint256 amount0Out,
uint256 amount1Out,
index_topic_2 address to
)
I am just confused about how to determine if it's a sold ORAKLER or Just bought & if he did buy how much money In BNB did he spend.
If anyone knows any other solution to do it or anything wrong I am doing here please tell me
I have figured out what I was doing wrong.
First I was using pancake factory address instead of my token lp address.
Second I have to use Swap not PairCreated In The code

Crowd Sale Contract Issue while minting token

I am currently working on a crowd sale contract, But I am having an issue with a function as the list of the error and the code of the function is attached, I need someone to tell me that what is happening in the code and how can i resolve the error that I'm facing in the code. Solidity version ^0.5.0
Code:
function _finalization() internal {
if (goalReached()) {
ERC20Mintable erc20Mintable = ERC20Mintable(token);
//getting the total tokens that are been minted yet
uint256 alreadyMintedToken = erc20Mintable.totalSupply();
//tokens of the final total supply
uint256 finalTotalTokenSupply = alreadyMintedToken
.div(tokenSalePercentage)
.mul(100);
foundersTimelock = new TokenTimelock(
token,
foundersFund,
releaseTime
);
partnersTimelock = new TokenTimelock(
token,
foundersFund,
releaseTime
);
foundationTimelock = new TokenTimelock(
token,
foundersFund,
releaseTime
);
//we will have the tokens that the founder will get
erc20Mintable.mint(
address(foundersTimelock),
finalTotalTokenSupply.mul(foundersPercentage).div(100)
);
erc20Mintable.mint(
address(partnersTimelock),
finalTotalTokenSupply.mul(partnersPercentage).div(100)
);
erc20Mintable.mint(
address(foundationTimelock),
finalTotalTokenSupply.mul(foundationPercentage).div(100)
);
erc20Mintable.renounceMinter();
// Unpause the token
ERC20Pausable erc20Pausable = new ERC20Pausable(token);
erc20Pausable.unpause();
erc20Pausable.renounceOwnership(wallet);
}
super._finalization();
}
Error 1:
Explicit type conversion not allowed from "function () view returns
(contract IERC20)" to "contract ERC20Mintable". ERC20Mintable
erc20Mintable = ERC20Mintable(token);
Error 2:
Crowdsale.sol:179:46: TypeError: Invalid type for argument in function call. Invalid implicit conversion from function () view returns (contract IERC20) to contract IERC20 requested.
foundersTimelock = new TokenTimelock(token,foundersFund,releaseTime);
^---^
Error 3:
Crowdsale.sol:179:28: TypeError: Type contract TokenTimelock is not
implicitly convertible to expected type address. foundersTimelock =
new TokenTimelock(token,foundersFund,releaseTime);

Get Transaction Id after completing Payment Objective c?

I was trying to get transation Id when paypal payment is done that is on didCompletePayment method, i can get only these details
Confirmation: {
client = {
environment = sandbox;
"paypal_sdk_version" = "2.12.2";
platform = iOS;
"product_name" = "PayPal iOS SDK";
};
response = {
"create_time" = "2017-02-01T07:40:43Z";
id = "PAY-4RK70135CF912010FLCIZB5A";
intent = sale;
state = approved;
};
"response_type" = payment;
}
I don't find transation ID here. Can any one suggest me how to get transation Id in detail so that i can save that to database.
I found some Curl concepts but i'm not sure where to start with.Please give me some suggestions.
Thanks in advance

bitcoinj Connect P2SH input transaction to the output transaction

I have created P2SH address and send coins to the address
https://www.blocktrail.com/tBTC/address/2N8Xu6rNAwssXtP2XPjSTuT2ViWQoPeHr3r
Next I want to send coins from 2N8Xu6rNAwssXtP2XPjSTuT2ViWQoPeHr3r address.
How to prepare P2SH transaction and connect it to the output script?
public static void sendFromP2SH(WalletAppKit kit, Address destAdd, Coin coin) throws AddressFormatException, InsufficientMoneyException, ExecutionException, InterruptedException {
Transaction tx = new Transaction(TestNet3Params.get());
tx.addOutput(coin, destAdd); //prepare destination output
Wallet.SendRequest req = Wallet.SendRequest.forTx(tx);
//TODO prepare P2SH input for output //https://www.blocktrail.com/tBTC/address/2N8Xu6rNAwssXtP2XPjSTuT2ViWQoPeHr3r
Script script = P2SHScript(kit); //2N8Xu6rNAwssXtP2XPjSTuT2ViWQoPeHr3r
TransactionOutput t = null;//... HOW TO CONNECT P2SH input transaction to the output ?
tx.addInput(t);
kit.wallet().completeTx(req);
kit.wallet().commitTx(req.tx);
kit.peerGroup().broadcastTransaction(req.tx).get();
}
prepare script for the P2SH address 2N8Xu6rNAwssXtP2XPjSTuT2ViWQoPeHr3r
public static Script P2SHScript(WalletAppKit kit) {
ECKey pubClientKey = kit.wallet().getImportedKeys().get(0);
ECKey pubServerKey = kit.wallet().getImportedKeys().get(1);
return ScriptBuilder.createP2SHOutputScript(1, ImmutableList.of(pubClientKey, pubServerKey));
}
Thank you.
What about the following constructor?
public TransactionOutput(NetworkParameters params, Transaction parent, BigInteger value, Address to)
Inside the code it speculates over the 'to' address to check if it is multisig and creates the output script appropriately.