from web3.auto.infura.rinkeby import w3
import json

with open("shift-abi.json", "r") as f:
    shift_abi = json.load(f)

with open("token-abi.json", "r") as f:
    token_abi = json.load(f)
    
priv_key = "0x0"

w3.eth.defaultAccount = w3.eth.account.privateKeyToAccount(priv_key)

shift_contract = w3.eth.contract(abi=shift_abi, address=w3.toChecksumAddress("0x6bE4C94052BF34e09d67C255D9Fe564c5abd4f95"))
tokenA = w3.eth.contract(abi=token_abi, address=w3.toChecksumAddress("0x1190fb1B00D20656549cdFF689E55D072BA764f3"))
tokenB = w3.eth.contract(abi=token_abi, address=w3.toChecksumAddress("0x0e1BFf82509994170026f47f1d0857fB275c3003"))

def approve(amount):
    txn = tokenA.functions.approve(shift_contract.address, int(amount*1e18)).buildTransaction({
        'chainId': 4, 
        'gas': 500000,
        'gasPrice': w3.eth.gasPrice,
        'nonce': w3.eth.getTransactionCount(w3.eth.defaultAccount.address)
        })
    signed_txn = w3.eth.account.signTransaction(txn, '0x047eb26808901c4fdf33262a3d9ea7e6a15f5683f9f60eef1b5a615fc2b3756a')
    txn_hash = w3.eth.sendRawTransaction(signed_txn.rawTransaction)
    tx_receipt = w3.eth.waitForTransactionReceipt(txn_hash)

def shift(amount):
    A_aggregator = "0xECe365B379E1dD183B20fc5f022230C044d51404";
    B_aggregator = "0xab4a352ac35dFE83221220D967Db41ee61A0DeFa";
    txn = shift_contract.functions.shift(int(amount*1e18), A_aggregator, B_aggregator).buildTransaction({
        'chainId': 4, 
        'gas': 500000,
        'gasPrice': w3.eth.gasPrice,#w3.eth.gasPrice,
        'nonce': w3.eth.getTransactionCount(w3.eth.defaultAccount.address)
        })
    signed_txn = w3.eth.account.signTransaction(txn, '0x047eb26808901c4fdf33262a3d9ea7e6a15f5683f9f60eef1b5a615fc2b3756a')
    txn_hash = w3.eth.sendRawTransaction(signed_txn.rawTransaction)
    tx_receipt = w3.eth.waitForTransactionReceipt(txn_hash)

def faucet():
    txn = shift_contract.functions.faucet().buildTransaction({
        'chainId': 4, 
        'gas': 500000,
        'gasPrice': w3.eth.gasPrice,#w3.eth.gasPrice,
        'nonce': w3.eth.getTransactionCount(w3.eth.defaultAccount.address)
        })
    signed_txn = w3.eth.account.signTransaction(txn, '0x047eb26808901c4fdf33262a3d9ea7e6a15f5683f9f60eef1b5a615fc2b3756a')
    txn_hash = w3.eth.sendRawTransaction(signed_txn.rawTransaction)
    tx_receipt = w3.eth.waitForTransactionReceipt(txn_hash)

def take_input():
    t = input("please select an option: ")
    if t == "1":
        n = input("amount of tokens to shift: ")
        approve(int(n))
        shift(int(n))
    elif t == "2":
        print("requesting funds")
        faucet()
        print("funds transfered :D")
    elif t == "3":
        print(tokenA.functions.balanceOf(w3.eth.defaultAccount.address).call({'from': w3.eth.defaultAccount.address}) / 1e18)
    elif t == "4":
        print(tokenB.functions.balanceOf(w3.eth.defaultAccount.address).call({'from': w3.eth.defaultAccount.address}) / 1e18)
    elif t == "0":
        exit(0);
    else:
        print("invalid option provided")
if __name__ == "__main__":
    help_menu = """
shift tokens: 1
request funds from the faucet: 2
check xftTM balance: 3
check xftTB balance: 4
exit : 0 
    """
    print(help_menu) 
    while True:
        take_input()