# 引入必要的库
from bscscan import BscScanAPI
from web3 import Web3
# 初始化BSCscan API和Web3
bsc_scan = BscScanAPI()
web3 = Web3(Web3.HTTPProvider('https://bsc-api.binance.org/'))
# 获取BNB智能合约
bnb_contract = bsc_scan.get_contract('0x9fc6d59a80e405a03999d4e8545a7a259a24dc6f')
# 定义质押函数
def stake_lp(sender, receiver, amount):
nonce = web3.eth.get_transaction_count(sender)
data = bnb_contract.functions.stake(amount).encodeABI(【更全面的开发源码搭建可看我昵称】)
tx = {
'to': bnb_contract.address,
'data': data,
'from': sender,
'value': 0,
'gas': 2000000,
'gasPrice': 2000000000,
'nonce': nonce
}
signed_tx = web3.eth.account.sign_transaction(tx, sender)
tx_hash = web3.eth.send_raw_transaction(signed_tx.rawTransaction)
tx_receipt = web3.eth.get_transaction_receipt(tx_hash)
return tx_receipt['logs'][0]['topics'][0]
# 调用质押函数并获取LP代币的地址
lp_token_address = bnb_contract.functions.lpToken().call()
# 发送LP代币到质押合约 【更全面的开发源码搭建可看我昵称】
lp_token = web3.eth.contract(address=lp_token_address, abi=None)
nonce = web3.eth.get_transaction_count(sender)
tx = {
'to': bnb_contract.address,
'data': lp_token.functions.approve(bnb_contract.address, amount).encodeABI(),
'from': sender,
'value': 0,
'gas': 2000000,
'gasPrice': 2000000000,
'nonce': nonce
}
signed_tx = web3.eth.account.sign_transaction(tx, sender)
tx_hash = web3.eth.send_raw_transaction(signed_tx.rawTransaction)
tx_receipt = web3.eth.get_transaction_receipt(tx_hash)
print(tx_receipt['logs'][0]['topics']
评论