def fundFromAbove(self, num_wei: int): #Give the this wallet ETH to pay gas fees #Use funds given to 'TEST_PRIVATE_KEY1' from ganache (see deploy.py) network = web3util.get_network() god_key = web3util.confFileValue(network, 'TEST_PRIVATE_KEY1') god_wallet = Web3Wallet(god_key) god_wallet.sendEth(self.address, num_wei)
def testSendEth(): _web3 = web3util.get_web3() network = web3util.get_network() #wallet1 should have got ETH from ganache startup (see deploy.py) private_key1 = web3util.confFileValue(network, 'TEST_PRIVATE_KEY1') wallet1 = web3wallet.Web3Wallet(private_key1) orig_bal1_base = wallet1.ETH_base() print("orig bal1 = %s" % web3util.fromBase18(orig_bal1_base)) assert orig_bal1_base > web3util.toBase18(1.0) #wallet2 should have 0 ETH wallet2 = web3wallet.randomWeb3Wallet() orig_bal2_base = wallet2.ETH_base() print("orig bal2 = %s" % web3util.fromBase18(orig_bal2_base)) assert orig_bal2_base == 0 #wallet1 gives wallet2 1.0 ETH sent_base = web3util.toBase18(1.0) wallet1.sendEth(wallet2.address, sent_base) new_bal1_base = wallet1.ETH_base() new_bal2_base = wallet2.ETH_base() print("new bal1 = %s" % web3util.fromBase18(new_bal1_base)) print("new bal2 = %s" % web3util.fromBase18(new_bal2_base)) assert new_bal2_base == sent_base assert (orig_bal1_base - sent_base*1.1) < new_bal1_base < (orig_bal1_base - sent_base)
def test_ETHbalance1(): _web3 = web3util.get_web3() network = web3util.get_network() private_key = web3util.confFileValue(network, 'TEST_PRIVATE_KEY1') wallet = web3wallet.Web3Wallet(private_key) assert wallet.ETH_base() > 1.0 #should have got ETH from ganache startup assert wallet.ETH_base() == _web3.eth.getBalance(wallet.address)
def __init__(self, symbol: str): #A random wallet won't have ETH for gas fees. So, use # 'TEST_PRIVATE_KEY1' which got funds in ganache startup (see deploy.py) network = web3util.get_network() key1 = web3util.confFileValue(network, 'TEST_PRIVATE_KEY1') self._web3_wallet = web3wallet.Web3Wallet(key1) factory = dtfactory.DTFactory() token_address = factory.createToken('', symbol, symbol, constants.HUGEINT, self._web3_wallet) self._token = datatoken.Datatoken(token_address)
def _deployAndMintToken(symbol: str, to_address: str) -> datatoken.Datatoken: network = web3util.get_network() private_key = web3util.confFileValue(network, 'TEST_PRIVATE_KEY1') from_wallet = Web3Wallet(private_key=private_key) factory = dtfactory.DTFactory() amount_base = web3util.toBase18(1000.0) dt_address = factory.createToken('', symbol, symbol, amount_base, from_wallet) dt = datatoken.Datatoken(dt_address) dt.mint(account=to_address, value_base=amount_base, from_wallet=from_wallet) return dt
def _make_info(private_key_name: str): class _Info: pass info = _Info() network = web3util.get_network() info.private_key = web3util.confFileValue(network, private_key_name) info.agent_wallet = AgentWallet.AgentWallet(OCEAN=_OCEAN_INIT, private_key=info.private_key) info.web3wallet = info.agent_wallet._web3wallet info.DT = _createDT(info.web3wallet) info.pool = _createPool(DT=info.DT, web3_w=info.web3wallet) return info
def make_info(name, private_key_name): # assume that this account has ETH with gas. class _Info: pass info = _Info() network = web3util.get_network() info.private_key = web3util.confFileValue(network, private_key_name) info.address = account.privateKeyToAddress(info.private_key) info.account = account.Account(private_key=info.private_key) info.wallet = Web3Wallet(private_key=info.private_key) info.T1 = _deployAndMintToken('TOK1', info.address) info.T2 = _deployAndMintToken('TOK2', info.address) return info
def _make_wallet(private_key_name:str, cache: bool): global _CACHED_WALLET if _CACHED_WALLET is None and cache: class _Wallet: pass wallet = _Wallet() network = web3util.get_network() wallet.private_key = web3util.confFileValue(network, private_key_name) wallet.agent_wallet = AgentWallet.AgentWallet( OCEAN=_OCEAN_INIT,private_key=wallet.private_key) wallet.web3wallet = wallet.agent_wallet._web3wallet wallet.DT = _createDT(wallet.web3wallet) wallet.pool = _createPool(DT=wallet.DT, web3_w=wallet.web3wallet) _CACHED_WALLET = wallet return _CACHED_WALLET return _CACHED_WALLET
def _make_agent(private_key_name: str, cache: bool=True) -> str: global _CACHED_AGENT if _CACHED_AGENT is None and cache: class _Agent(BaseAgent.BaseAgent): def takeStep(self, state): pass network = web3util.get_network() private_key = web3util.confFileValue(network, private_key_name) alice = _Agent( name="agent1", USD=0.0,OCEAN=_OCEAN_INIT, private_key=private_key) alice.web3wallet = alice._wallet._web3wallet alice.DT = _createDT(alice.web3wallet) alice.pool = _createPool(alice.DT, alice.web3wallet) _CACHED_AGENT = alice return _CACHED_AGENT return _CACHED_AGENT
def _make_info(private_key_name: str): class _Info: def __init__(self): self.private_key: Union[str, None] = None self.agent_wallet: Union[AgentWallet.AgentWallet, None] = None self.web3wallet: Union[web3wallet, None] = None self.DT: Union[datatoken, None] = None self.pool: Union[bool, None] = None info = _Info() network = web3util.get_network() info.private_key = web3util.confFileValue(network, private_key_name) info.agent_wallet = AgentWallet.AgentWallet(OCEAN=_OCEAN_INIT, private_key=info.private_key) info.web3wallet = info.agent_wallet._web3wallet info.DT = _createDT(info.web3wallet) info.pool = _createPool(DT=info.DT, web3_w=info.web3wallet) return info
def buildAndSendTx(function, from_wallet: Web3Wallet, gaslimit: int = constants.GASLIMIT_DEFAULT, num_wei: int = 0, to_address=None): assert isinstance(from_wallet.address, str) #assert isinstance(from_wallet.private_key, str) _web3 = web3util.get_web3() nonce = _web3.eth.getTransactionCount(from_wallet.address) network = web3util.get_network() gas_price = int(web3util.confFileValue(network, 'GAS_PRICE')) tx_params = { "from": from_wallet.address, "value": num_wei, "nonce": nonce, "gas": gaslimit, "gasPrice": gas_price, } if function is None: #just send ETH, versus smart contract call? assert to_address is not None assert isinstance(to_address, str) tx = tx_params tx["to"] = to_address else: assert to_address is None tx = function.buildTransaction(tx_params) signed_tx = _web3.eth.account.sign_transaction( tx, private_key=from_wallet.private_key) tx_hash = _web3.eth.sendRawTransaction(signed_tx.rawTransaction) tx_receipt = _web3.eth.waitForTransactionReceipt(tx_hash) if tx_receipt['status'] == 0: # did tx fail? raise Exception("The tx failed. tx_receipt: {tx_receipt}") return (tx_hash, tx_receipt)
def __init__(self, agent_name: str, network_name: str, _OCEAN_INIT: float): self.private_key = web3util.confFileValue(network_name, agent_name) self.wallet = AgentWallet(OCEAN=_OCEAN_INIT, private_key=self.private_key) self.web3wallet = self.wallet._web3wallet self.datatokens_created: Dict[str, Datatoken] = {} self._CACHED_DT = None
token_address=datatoken.address, balance_base=dt_stake, weight_base=pool_weight_dt, from_wallet=self.web3wallet) pool.bind( token_address=OCEAN.address, balance_base=ocean_stake, weight_base=pool_weight_ocean, from_wallet=self.web3wallet) pool.finalize(from_wallet=self.web3wallet) return pool private_key_name: str='TEST_PRIVATE_KEY1' network: str = web3util.confFileValue('general', 'NETWORK') alice = AgentTEST(agent_name=private_key_name, network_name=network, _OCEAN_INIT=_OCEAN_INIT) alice_datatoken = alice.create_datatoken(blob="", name="Alice Coin", symbol="ALC", cap_base=_DT_INIT, use_cache=False) alice_pool = alice_pool = alice.create_pool( datatoken=alice_datatoken, _DT_STAKE=_DT_STAKE, _OCEAN_STAKE=_OCEAN_STAKE, POOL_WEIGHT_DT=POOL_WEIGHT_DT, POOL_WEIGHT_OCEAN=POOL_WEIGHT_OCEAN ) # test DT alice_DT_amt: float = alice.wallet.DT(alice_datatoken) print(alice_DT_amt, _DT_INIT, _DT_STAKE) assert alice_DT_amt == (_DT_INIT - _DT_STAKE)
#!/usr/bin/env python import eth_utils import os from web3tools.web3util import confFileValue #Port in ganache_url must match ganache-cli call port_number = '8545' ganache_url = confFileValue('general', 'GANACHE_URL') assert port_number in ganache_url ###get ganache-cli going, populating accounts found in conf file # grab private keys alice_private_key = confFileValue('ganache', 'TEST_PRIVATE_KEY1') bob_private_key = confFileValue('ganache', 'TEST_PRIVATE_KEY2') factory_deployer_private_key = confFileValue('ganache', 'FACTORY_DEPLOYER_PRIVATE_KEY') # launch ganache-cli and give each private account 100 eth. amount_eth = 100 amount_wei = eth_utils.to_wei(amount_eth, 'ether') os.system( f'ganache-cli --port {port_number} --gasLimit 10000000000 --gasPrice 1 ---hardfork istanbul --account="{alice_private_key},{amount_wei}" --account="{bob_private_key},{amount_wei}" --account="{factory_deployer_private_key},{amount_wei}"' )