Пример #1
0
def keyreg_txn_params(context, fee, fv, lv, gh, votekey, selkey, votefst,
                      votelst, votekd, gen, note):
    context.fee = int(fee)
    context.fv = int(fv)
    context.lv = int(lv)
    context.gh = gh
    context.votekey = encoding.encode_address(base64.b64decode(votekey))
    context.selkey = encoding.encode_address(base64.b64decode(selkey))
    context.votefst = int(votefst)
    context.votelst = int(votelst)
    context.votekd = int(votekd)
    if gen == "none":
        context.gen = None
    else:
        context.gen = gen
    context.params = transaction.SuggestedParams(context.fee, context.fv,
                                                 context.lv, context.gh,
                                                 context.gen)

    if note == "none":
        context.note = None
    else:
        context.note = base64.b64decode(note)
    if gen == "none":
        context.gen = None
    else:
        context.gen = gen
Пример #2
0
def encode_addr(context):
    context.pk = encoding.encode_address(context.pk)
Пример #3
0
def parse_pending_txns(context, length, idx, sender):
    context.response = json.loads(context.response)
    assert len(context.response["top-transactions"]) == int(length)
    assert encoding.encode_address(
        base64.b64decode(context.response["top-transactions"][int(idx)]["txn"]
                         ["snd"])) == sender
Пример #4
0
def parse_pending_txn(context, sender):
    context.response = json.loads(context.response)
    assert encoding.encode_address(
        base64.b64decode(context.response["txn"]["txn"]["snd"])) == sender
Пример #5
0
 def app_address(self, app_id: int):
     return enc.encode_address(enc.checksum(b"appID" + (app_id).to_bytes(8, "big")))
Пример #6
0
 def logic_address(self, bytecode: bytes):
     return enc.encode_address(enc.checksum(b"Program" + bytecode))
Пример #7
0
import base64
import binascii
import string
from dataclasses import dataclass
from typing import List, Union

from algosdk.constants import payment_txn, appcall_txn
from algosdk.future import transaction
from algosdk.encoding import encode_address, msgpack_encode
from algosdk.v2client.models import DryrunRequest, DryrunSource, \
    Application, ApplicationParams, ApplicationStateSchema, Account, \
    TealKeyValue


ZERO_ADDRESS = encode_address(bytes(32))
PRINTABLE = frozenset(string.printable)

@dataclass
class LSig:
    """Logic Sig program parameters"""
    args: List[bytes] = None


@dataclass
class App:
    """Application program parameters"""
    creator: str = ZERO_ADDRESS
    round: int = None
    app_idx: int = 0
    on_complete: int = 0
    args: List[bytes] = None
Пример #8
0
def get_token1_refund():
    print("Attempting to get refund of Token 1 from Escrow...")

    encoded_app_args = [bytes("r", "utf-8")]

    # Calculate unused_token1
    unused_token1 = 0
    account_info = algod_client.account_info(TEST_ACCOUNT_ADDRESS)
    local_state = account_info['apps-local-state']
    for block in local_state:
        if block['id'] == MANAGER_INDEX:
            for kvs in block['key-value']:
                decoded_key = base64.b64decode(kvs['key'])
                prefix_bytes = decoded_key[:2]
                prefix_key = prefix_bytes.decode('utf-8')
                addr_bytes = decoded_key[2:]
                b32_encoded_addr = base64.b32encode(addr_bytes).decode('utf-8')
                escrow_addr = encoding.encode_address(
                    base64.b32decode(b32_encoded_addr))

                if (prefix_key == "U1" and ESCROW_ADDRESS == escrow_addr):
                    unused_token1 = int(kvs['value']['uint'])

    print(f"User unused Token 1 is {unused_token1}")

    if unused_token1 != 0:
        # Transaction to Validator
        txn_1 = transaction.ApplicationCallTxn(
            sender=TEST_ACCOUNT_ADDRESS,
            sp=algod_client.suggested_params(),
            index=VALIDATOR_INDEX,
            on_complete=transaction.OnComplete.NoOpOC,
            accounts=[ESCROW_ADDRESS],
            app_args=encoded_app_args)

        # Transaction to Manager
        txn_2 = transaction.ApplicationCallTxn(
            sender=TEST_ACCOUNT_ADDRESS,
            sp=algod_client.suggested_params(),
            index=MANAGER_INDEX,
            on_complete=transaction.OnComplete.NoOpOC,
            accounts=[ESCROW_ADDRESS],
            app_args=encoded_app_args)

        # Make LogicSig
        program = base64.b64decode(ESCROW_LOGICSIG)
        lsig = transaction.LogicSig(program)

        # Transaction to get refund of Token 1 from Escrow
        txn_3 = transaction.AssetTransferTxn(
            sender=ESCROW_ADDRESS,
            sp=algod_client.suggested_params(),
            receiver=TEST_ACCOUNT_ADDRESS,
            amt=unused_token1,
            index=TOKEN1_INDEX)

        # Get group ID and assign to transactions
        gid = transaction.calculate_group_id([txn_1, txn_2, txn_3])
        txn_1.group = gid
        txn_2.group = gid
        txn_3.group = gid

        # Sign transactions
        stxn_1 = txn_1.sign(TEST_ACCOUNT_PRIVATE_KEY)
        stxn_2 = txn_2.sign(TEST_ACCOUNT_PRIVATE_KEY)
        stxn_3 = transaction.LogicSigTransaction(txn_3, lsig)

        # Broadcast the transactions
        signed_txns = [stxn_1, stxn_2, stxn_3]
        tx_id = algod_client.send_transactions(signed_txns)

        print(
            f"Got refund of Token 1 from AlgoSwap to User successfully! Tx ID: https://testnet.algoexplorer.io/tx/{tx_id}"
        )

    print()
Пример #9
0
 def test_encode_decode(self):
     sk, pk = account.generate_account()
     self.assertEqual(pk,
                      encoding.encode_address(encoding.decode_address(pk)))
     self.assertEqual(pk, account.address_from_private_key(sk))