Skip to content

Instantly share code, notes, and snippets.

@jsyqrt
Last active June 13, 2018 09:14
Show Gist options
  • Save jsyqrt/fc3a6345742d9aca2adfc23db0e49fa8 to your computer and use it in GitHub Desktop.
Save jsyqrt/fc3a6345742d9aca2adfc23db0e49fa8 to your computer and use it in GitHub Desktop.
  1. create a proxy agent.
from cpchain import chain, config
from cpchain.chain.models import OrderInfo
from cpchain.crypto import RSACipher, get_addr_from_public_key
from cpchain.account import Accounts

w3 = chain.utils.default_w3
bin_path = chain.utils.join_with_root(config.chain.contract_bin_path)
contract_name = config.chain.contract_name

chain.utils.deploy_contract(bin_path, contract_name, w3)

proxyAccount = get_addr_from_public_key(Accounts()[0].public_key)

proxyAgent = chain.agents.ProxyAgent(w3, bin_path, contract_name, account=proxyAccount)
  1. place an order and confirm it.
buyerAccount = get_addr_from_public_key(Accounts()[1].public_key)
buyerAgent = chain.agents.BuyerAgent(w3, bin_path, contract_name, account=buyerAccount)

sellerAccount = get_addr_from_public_key(Accounts()[2].public_key)
sellerAgent = chain.agents.SellerAgent(w3, bin_path, contract_name, account=sellerAccount)

order_id = 0
time_allowed = 2000

def test_place_order(btrans):
    buyer_rsa_pubkey = RSACipher.load_public_key()
    order_info = OrderInfo(
        desc_hash=bytes([0, 1, 2, 3] * 8),
        buyer_rsa_pubkey=buyer_rsa_pubkey,
        seller=btrans.web3.toChecksumAddress(sellerAccount),
        proxy=btrans.web3.toChecksumAddress(proxyAccount),
        secondary_proxy=btrans.web3.toChecksumAddress(proxyAccount),
        proxy_value=10,
        value=20,
        time_allowed=time_allowed
    )
    global order_id
    order_id = btrans.place_order(order_info)
    #assert order_id == 1
    test_record = btrans.query_order(order_id)
    assert test_record[0] == bytes([0, 1, 2, 3] * 8)
    assert test_record[2] == btrans.account
    # Check state is Created
    assert test_record[10] == 0

def test_seller_confirm_order(strans):
    strans.confirm_order(order_id)
    test_record = strans.query_order(order_id)
    assert test_record[10] == 1

test_place_order(buyerAgent)
test_seller_confirm_order(sellerAgent)

  1. deposit and withdraw. (now optional.)
value = 100
proxyAgent.deposit(value)
proxyAgent.query_deposit()
proxyAgent.withdraw(value)
  1. check order is ready.(if seller has deposited.)
assert proxyAgent.check_order_is_ready(order_id)
  1. claim product fetched.(after proxy fetched product.)
proxyAgent.claim_fetched(order_id)
# test if proxyAgent.claim_fetch succeed.
test_record = proxyAgent.query_order(order_id)
assert test_record[10] == 2
  1. claim product delivered.(after proxy delivered product.)
hash = bytes([1, 2, 3, 4] * 4)
proxyAgent.claim_delivered(order_id, hash)
# test if proxyAgent
test_record = proxyAgent.query_order(order_id)
assert test_record[10] == 3
@jsyqrt
Copy link
Author

jsyqrt commented Jun 12, 2018

  1. change your local config file in ~/.cpchain/cpchain.toml, change mode variable from "dummy" to "local".
  2. go to cpchain/bin
  3. ./eth-init-chain
  4. ./eth-run-geth
  5. see tests/chain/test_normal_process.py for more information.

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment