Beispiel #1
0
def test_new(Uxxx, byte_length):
    assert Uxxx(16) == (Uxxx("16"))
    assert Uxxx(16) == (Uxxx("0x10"))
    with pytest.raises(ValueError):
        assert Uxxx(TOO_LARGE)

    assert Uxxx(Uxxx(16)) == (Uxxx(16))
    if type(Uxxx) is U256:
        assert Uxxx(16) == (U256(U64(16)))
        assert Uxxx(16) == (U256(U128(16)))
    elif type(Uxxx) is U128:
        assert Uxxx(16) == (U128(U64(16)))
Beispiel #2
0
    def create_mint_asset_transaction(
        self,
        recipient: AssetAddress,
        scheme: AssetScheme = None,
        approvals: List[str] = None,
        network_id: str = None,
        shard_id: int = None,
        metadata=None,
        approver: PlatformAddress = None,
        registrar: PlatformAddress = None,
        allowed_script_hashes: List[H160] = None,
        supply: U64 = None,
    ):
        if approvals is None:
            approvals = []
        if scheme is None and (shard_id is None or metadata is None):
            raise ValueError(
                f"Either scheme params or proper arguments should be provided {scheme}"
            )

        network_id = self.network_id if network_id is None else network_id
        shard_id = shard_id
        supply = U64(U64.MAX_VALUE) if supply is None else supply

        check_metadata(metadata)
        metadata = metadata if isinstance(metadata, str) else str(metadata)
        check_asset_address_recipient(recipient)
        check_network_id(network_id)
        if shard_id is None:
            raise ValueError("shard_id is None")
        check_shard_id(shard_id)
        check_approver(approver)
        check_registrar(registrar)
        check_amount(supply)

        return MintAsset(
            network_id,
            shard_id,
            metadata,
            AssetMintOutput(U64(supply), None, None,
                            AssetAddress.ensure(recipient)),
            None if approver is None else PlatformAddress.ensure(approver),
            None if registrar is None else PlatformAddress.ensure(registrar),
            [] if allowed_script_hashes is None else allowed_script_hashes,
            approvals,
        )
Beispiel #3
0
 def from_json(data: AssetTransferOutputJSON):
     return AssetTransferOutput(
         H160(data["lockScriptHash"]),
         list(map(lambda x: bytes.fromhex(x), data["parameters"])),
         H160(data["assetType"]),
         data["shardId"],
         U64(data["quantity"]),
     )
Beispiel #4
0
def test_ensure(Uxxx, byte_length):
    assert Uxxx("10") == Uxxx(10)
    assert Uxxx("0xa") == Uxxx(10)
    assert Uxxx(Uxxx(10))

    if byte_length >= 32:
        assert Uxxx(U256(10)) == (Uxxx(10))
    if byte_length >= 16:
        assert Uxxx(U128(10)) == (Uxxx(10))
    assert Uxxx(U64(10)) == (Uxxx(10))
Beispiel #5
0
 def from_json(data):
     return OrderOnTranser(
         Order.from_json(data["order"]),
         U64(data["spentQuantity"]),
         data["inputFromIndices"],
         data["inputFeeIndices"],
         data["outputFromIndices"],
         data["outputToIndices"],
         data["outputOwnedFeeIndices"],
         data["outputTransferredFeeIndices"],
     )
Beispiel #6
0
 def from_json(data: OrderJSON):
     return Order(
         H160(data["assetTypeFrom"]),
         H160(data["assetTypeTo"]),
         H160(data["assetTypeFee"]),
         data["shardIdFrom"],
         data["shardIdTo"],
         data["shardIdFee"],
         U64(data["assetQuantityFrom"]),
         U64(data["assetQuantityTo"]),
         U64(data["assetQuantityFee"]),
         list(
             map(lambda x: AssetOutPoint.from_json(x),
                 data["originOutputs"])),
         U64(data["expiration"]),
         H160(data["lockScriptHashFrom"]),
         list(map(lambda x: bytes.fromhex(x), data["parametersFrom"])),
         H160(data["lockSCriptHashFee"]),
         list(map(lambda x: bytes.fromhex(x), data["parametersFee"])),
     )
Beispiel #7
0
 def from_json(data):
     return Asset(
         H256(data["tracker"]),
         data["transactionOutputIndex"],
         H160(data["assetType"]),
         data["shardId"],
         U64(data["quantity"]),
         H160(data["lockScriptHash"]),
         list(map(lambda x: bytes.fromhex(x), data["parameters"])),
         None if data["orderHash"] is None else H256(data["orderHash"]),
     )
Beispiel #8
0
 def from_json(data: AssetOutPointJSON):
     return AssetOutPoint(
         H256(data.tracker),
         data.index,
         H160(data.asset_type),
         data.shard_id,
         U64(data.quantity),
         None if data.lock_script_hash is None else H160(data.lock_script_hash),
         None
         if data.parameters is None
         else list(map(lambda x: bytes.fromhex(x), data.parameters)),
     )
Beispiel #9
0
    def create_asset_scheme(
        self,
        shard_id: int,
        metadata,
        supply: U64,
        approver: PlatformAddress = None,
        registrar: PlatformAddress = None,
        allowed_script_hashes: List[H160] = None,
        pool: List[object] = None,
    ):
        if pool is None:
            pool = []

        check_metadata(metadata)
        metadata = metadata if isinstance(metadata, str) else str(metadata)
        check_shard_id(shard_id)
        check_amount(supply)
        check_approver(approver)
        check_registrar(registrar)

        from .assetscheme import AssetScheme

        return AssetScheme(
            metadata,
            U64(supply),
            None if approver is None else PlatformAddress.ensure(approver),
            None if registrar is None else PlatformAddress.ensure(registrar),
            [] if allowed_script_hashes is None else allowed_script_hashes,
            list(
                map(
                    lambda x: {
                        "assetType": H160(x["assetType"]),
                        "quantity": U64(x["quantity"]),
                    },
                    pool,
                )),
            self.network_id,
            shard_id,
        )
 def from_json(data: AssetSchemeJSON):
     return AssetScheme(
         data["metadata"],
         U64(data["supply"]),
         None if data["approver"] is None else PlatformAddress.ensure(
             data["approver"]),
         None if data["registrar"] is None else PlatformAddress.ensure(
             data["registrar"]),
         [] if data["allowedScriptHashes"] is None else list(
             map(lambda x: H160(x), data["allowedScriptHashes"])),
         list(
             map(
                 lambda x: {
                     "assetType": H160(x["assetType"]),
                     "quantity": U64(x["quantity"]),
                 },
                 data["pool"],
             )),
         None,
         None,
         data["seq"],
     )
Beispiel #11
0
def test_check(Uxxx, byte_length):
    assert Uxxx.check(-1) is False
    assert Uxxx.check(0.5) is False

    assert Uxxx.check(0) is True
    assert Uxxx.check("0") is True
    assert Uxxx.check("0x0") is True

    if byte_length >= 32:
        assert Uxxx.check(U256(0)) is True
    if byte_length >= 16:
        assert Uxxx.check(U128(0)) is True
    assert Uxxx.check(U64(0)) is True
Beispiel #12
0
    def consume(self, quantity: U64):
        quantity_from = U64(quantity)
        if quantity_from > self.asset_quantity_from:
            raise ValueError(
                f"The given quantity is too big: {quantity_from} > {self.asset_quantity_from}"
            )

        remain_quantity_from = self.asset_quantity_from - quantity_from
        if not (((remain_quantity_from * self.asset_quantity_to) %
                 self.asset_quantity_from) == 0):
            raise ValueError(
                f"The given quantity does not fit to the ratio: {self.asset_quantity_from}:{self.asset_quantity_to}"
            )

        remain_quantity_to = (remain_quantity_from * self.asset_quantity_to /
                              self.asset_quantity_from)
        remain_quantity_fee = (remain_quantity_from * self.asset_quantity_fee /
                               self.asset_quantity_from)

        return Order(
            self.asset_type_from,
            self.asset_type_to,
            self.asset_type_fee,
            self.shard_id_from,
            self.shard_id_to,
            self.shard_id_fee,
            U64(remain_quantity_from),
            U64(remain_quantity_to),
            U64(remain_quantity_fee),
            self.origin_outputs,
            self.expiration,
            self.lock_script_hash_from,
            self.parameters_from,
            self.lock_script_hash_fee,
            self.parameters_fee,
        )
Beispiel #13
0
    def sign_transaction(
        self,
        tx: Transaction,
        account: Union[PlatformAddress, str],
        fee: Union[U64, str, int],
        seq: int,
        keystore=None,
        passphrase="",
    ):
        if not isinstance(tx, Transaction):
            raise ValueError(
                f"Expected the first argument of signTransaction to be a Transaction instance but found {tx}"
            )

        keystore = self.ensure_keystore()
        if not is_keystore(keystore):
            raise ValueError(
                f"Expected keyStore param to be a KeyStore instance but found {keystore}"
            )
        if not PlatformAddress.check(account):
            raise ValueError(
                f"Expected account param to be a PlatformAddress value but found {account}"
            )
        if not U64.check(fee):
            raise ValueError(
                f"Expected fee param to be a U64 value but found {fee}")
        if not isinstance(seq, int):
            raise ValueError(
                f"Expected seq param to be a number value but found {seq}")
        tx.fee = fee
        tx.seq = seq
        account_id = PlatformAddress.ensure(account).account_id
        sig = keystore.platform.sign(account_id.to_string(),
                                     tx.unsigned_hash(), passphrase)

        return SignedTransaction(tx, sig)
Beispiel #14
0
def check_amount(amount: U64):
    if not U64.check(amount):
        raise ValueError(
            f"Expected amount param to be a U64 value but found ${amount}")
Beispiel #15
0
 def from_json(data: AssetMintOutputJSON):
     return AssetMintOutput(
         U64(data["supply"]),
         H160(data["lockScriptHash"]),
         list(map(lambda x: bytes.fromhex(x), data["parameters"])),
     )
Beispiel #16
0
    def __init__(
        self,
        asset_type_from: H160,
        asset_type_to: H160,
        asset_type_fee: H160,
        shard_id_from: int,
        shard_id_to: int,
        shard_id_fee: int,
        asset_quantity_from: U64,
        asset_quantity_to: U64,
        asset_quantity_fee: U64,
        origin_outputs: List[AssetOutPoint],
        expiration: U64,
        lock_script_hash_from: H160 = None,
        parameters_from: List[bytes] = None,
        recipient_from: AssetAddress = None,
        lock_script_hash_fee: H160 = None,
        parameters_fee: List[bytes] = None,
        recipient_fee: AssetAddress = None,
    ):
        if recipient_from is not None:
            self.lock_script_hash_from, self.parameters_from = decompose_recipient(
                recipient_from)
        else:
            if lock_script_hash_from is None or parameters_from is None:
                raise ValueError("recipient_from should not be None")
            self.lock_script_hash_from = lock_script_hash_from
            self.parameters_from = parameters_from

        if recipient_fee is not None:
            self.lock_script_hash_fee, self.parameters_fee = decompose_recipient(
                recipient_fee)
        else:
            if lock_script_hash_fee is None or parameters_fee is None:
                raise ValueError("recipient_fee should not be None")
            self.lock_script_hash_fee = lock_script_hash_fee
            self.parameters_fee = parameters_fee

        self.asset_type_from = asset_type_from
        self.asset_type_to = asset_type_to
        self.asset_type_fee = (H160(H160.ZERO)
                               if asset_type_fee is None else asset_type_fee)
        self.shard_id_from = shard_id_from
        self.shard_id_to = shard_id_to
        self.shard_id_fee = 0 if shard_id_fee is None else shard_id_fee
        self.asset_quantity_from = asset_quantity_from
        self.asset_quantity_to = asset_quantity_to
        self.asset_quantity_fee = (U64(0) if asset_quantity_fee is None else
                                   asset_quantity_fee)
        self.origin_outputs = origin_outputs
        self.expiration = expiration

        asset_quantity_from_is_zero = asset_quantity_from == 0
        asset_quantity_to_is_zero = asset_quantity_to == 0
        asset_quantity_fee_is_zero = asset_quantity_fee == 0

        if asset_type_from == asset_type_to and shard_id_from == shard_id_to:
            raise ValueError(
                f"assetTypeFrom and assetTypeTo is same: {asset_type_from}(shard {shard_id_from})"
            )
        elif not asset_quantity_fee_is_zero:
            if asset_type_from == asset_type_fee and shard_id_from == shard_id_fee:
                raise ValueError(
                    f"assetTypeFrom and assetTypeFee is same: {asset_type_from}(shard {shard_id_from})"
                )
            if asset_type_to == asset_type_fee and shard_id_to == shard_id_fee:
                raise ValueError(
                    f"assetTypeTo and assetTypeFee is same: {asset_type_to}(shard {shard_id_to})"
                )

        if ((asset_quantity_from_is_zero and not asset_quantity_to_is_zero) or
            (not asset_quantity_from_is_zero and asset_quantity_to_is_zero)
                or (asset_quantity_from_is_zero and asset_quantity_fee_is_zero)
                or (not asset_quantity_from_is_zero
                    and not (asset_quantity_fee % asset_quantity_from) == 0)):
            raise ValueError(
                f"The given quantity ratio is invalid: {asset_quantity_from}:{asset_quantity_to}:{asset_quantity_fee}"
            )

        if len(origin_outputs) == 0:
            raise ValueError(f"originOutputs is empty")