# ft基本功能示例 **Published by:** [0xcodinnnng](https://paragraph.com/@0xcodinnnng/) **Published on:** 2023-09-14 **URL:** https://paragraph.com/@0xcodinnnng/ft ## Content from loguru import logger from web3 import Web3 import json def get_buy_price(shares_subject, amount): try: buy_price = contract.functions.getBuyPriceAfterFee(shares_subject, amount).call() return buy_price except Exception as e: logger.error(f'get buy price error:{e}') def trade(from_address, private_key, buy_price, amount): try: gas_price = w3.eth.gas_price * 1.1 gas_price = Web3.from_wei(int(gas_price), 'gwei') transaction = contract.functions.buyShares(shares_subject, amount).build_transaction({ 'from': from_address, 'value': buy_price, 'gas': 2000000, 'nonce': w3.eth.get_transaction_count(from_address), 'gasPrice': w3.to_wei(gas_price, 'gwei'), }) signed_transaction = w3.eth.account.sign_transaction(transaction, private_key) transaction_hash = w3.eth.send_raw_transaction(signed_transaction.rawTransaction) logger.info(f"Transaction:{w3.to_hex(transaction_hash)}") except Exception as e: logger.error(f'{from_address}:transaction error:{e}') if __name__ == '__main__': w3 = Web3(Web3.HTTPProvider("https://mainnet.base.org")) abi_file = ''' [{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"trader","type":"address"},{"indexed":false,"internalType":"address","name":"subject","type":"address"},{"indexed":false,"internalType":"bool","name":"isBuy","type":"bool"},{"indexed":false,"internalType":"uint256","name":"shareAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"ethAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"protocolEthAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"subjectEthAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"supply","type":"uint256"}],"name":"Trade","type":"event"},{"inputs":[{"internalType":"address","name":"sharesSubject","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"buyShares","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"sharesSubject","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"getBuyPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"sharesSubject","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"getBuyPriceAfterFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"supply","type":"uint256"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"getPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"sharesSubject","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"getSellPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"sharesSubject","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"getSellPriceAfterFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"protocolFeeDestination","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"protocolFeePercent","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sharesSubject","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"sellShares","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"_feeDestination","type":"address"}],"name":"setFeeDestination","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_feePercent","type":"uint256"}],"name":"setProtocolFeePercent","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_feePercent","type":"uint256"}],"name":"setSubjectFeePercent","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"sharesBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"sharesSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"subjectFeePercent","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"}] ''' contract_abi = json.loads(abi_file) contract_address = '0xCF205808Ed36593aa40a44F10c7f7C2F67d4A4d4' contract = w3.eth.contract(address=contract_address, abi=contract_abi) # 自己ft账号的地址和密钥 from_address = "" private_key = "" # 输入要买keys的地址 shares_subject = Web3.to_checksum_address("") # 买一个 amount_1 = 1 buy_price_1 = get_buy_price(shares_subject, amount_1) print(f'buy_price:{buy_price_1}') trade(from_address, private_key, buy_price_1, amount_1) ## Publication Information - [0xcodinnnng](https://paragraph.com/@0xcodinnnng/): Publication homepage - [All Posts](https://paragraph.com/@0xcodinnnng/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@0xcodinnnng): Subscribe to updates - [Twitter](https://twitter.com/0xcodinnnng): Follow on Twitter