Skip to content

Instantly share code, notes, and snippets.

@Kcrong Kcrong/ask.py Secret
Last active May 9, 2018

Embed
What would you like to do?
import json
import time
from eth_utils import to_checksum_address
from web3 import Web3, HTTPProvider
from web3.contract import ConciseContract
from web3.middleware import geth_poa_middleware
INFURA_API_KEY = "this is key"
ACCOUNT_PASSWORD = "this is account password"
w3 = Web3(HTTPProvider('https://ropsten.infura.io/{}'.format(INFURA_API_KEY)))
w3.middleware_stack.inject(geth_poa_middleware, layer=0)
from eth_utils import to_checksum_address
contract_address = to_checksum_address('0xFb294910d8193DeB9a294B51F22D8878ad15f2E8')
# Instantiate and deploy contract
contract_instance = w3.eth.contract(abi=contract_abi, address=contract_address, ContractFactoryClass=ConciseContract)
account_a = "0xCa2d22Cb8ff54f2D1DCfDBb75DD6411a5A0ee6f1"
account_b = "0x8F8d1bc97E8939e3932BfBeb923A1B2B972D5A9A"
# Unlock account
w3.personal.unlockAccount(account_a, ACCOUNT_PASSWORD) # Raise the below error!!!
"""
requests.exceptions.HTTPError: 405 Client Error: Method Not Allowed for url: https://ropsten.infura.io/API_KEY
"""
print("Contract: {}".format(contract_address))
print("Before give token")
print('My Balance: {}'.format(contract_instance.myBalance()))
print('{} Balance: {}'.format(account_a, contract_instance.getBalance(account_a)))
print('{} Balance: {}'.format(account_b, contract_instance.getBalance(account_b)))
token_amount = 1000
# Make transaction
contract_instance.giveToken(account_b, token_amount, transact={'from': account_a}) # Raise the below error!!!
"""
requests.exceptions.HTTPError: 405 Client Error: Method Not Allowed for url: https://ropsten.infura.io/API_KEY
"""
try:
assert contract_instance.getBalance(account_b) == token_amount
except AssertionError as e:
print("Test Error: {}".format(e))
print("Amount: {}\t Expected: {}".format(contract_instance.getBalance(account_b), token_amount))
else:
print("After give token")
print('My Balance: {}'.format(contract_instance.myBalance()))
print('{} Balance: {}'.format(account_a, contract_instance.getBalance(account_a)))
print('{} Balance: {}'.format(account_b, contract_instance.getBalance(account_b)))
finally:
print('Done')
pragma solidity ^0.4.0;
contract BasicToken {
// Owner
address public owner;
// For manage account and balance
mapping(address => uint) userAccount;
constructor() public {
owner = msg.sender;
}
// Example
event increaseToken(address sender, address receiver, uint amount);
function getBalance(address addr) public view returns (uint) {
return userAccount[addr];
}
function myBalance() public view returns (uint) {
return userAccount[msg.sender];
}
function giveToken(address dest, uint amount) public returns (bool) {
emit increaseToken(msg.sender, dest, amount);
userAccount[dest] += amount;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.