• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OpShin / opshin / 806

pending completion
806

push

travis-ci-com

nielstron
Update docs

3519 of 3781 relevant lines covered (93.07%)

3.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

39.02
/opshin/prelude.py
1
from hashlib import sha256, sha3_256, blake2b
4✔
2
from opshin.ledger.api_v2 import *
4✔
3

4

5
@dataclass(unsafe_hash=True)
4✔
6
class Token(PlutusData):
4✔
7
    """
8
    A token, represented by policy id and token name
9
    """
10

11
    policy_id: PolicyId
4✔
12
    token_name: TokenName
4✔
13

14

15
# Used to indicate that this contract does not expect a redeemer
16
NoRedeemer = Nothing
4✔
17

18
### Optimized methods for handling tokens at addresses
19

20

21
def all_tokens_unlocked_from_address(
4✔
22
    txins: List[TxInInfo], address: Address, token: Token
23
) -> int:
24
    """Returns how many tokens of specified type are unlocked from given address"""
25
    return sum(
×
26
        [
27
            txi.resolved.value.get(token.policy_id, {b"": 0}).get(token.token_name, 0)
28
            for txi in txins
29
            if txi.resolved.address == address
30
        ]
31
    )
32

33

34
def all_tokens_locked_at_address_with_datum(
4✔
35
    txouts: List[TxOut], address: Address, token: Token, output_datum: OutputDatum
36
) -> int:
37
    """Returns how many tokens of specified type are locked at then given address with the specified datum"""
38
    return sum(
×
39
        [
40
            txo.value.get(token.policy_id, {b"": 0}).get(token.token_name, 0)
41
            for txo in txouts
42
            if txo.address == address and txo.datum == output_datum
43
        ]
44
    )
45

46

47
def all_tokens_locked_at_address(
4✔
48
    txouts: List[TxOut], address: Address, token: Token
49
) -> int:
50
    """Returns how many tokens of specified type are locked at the given address"""
51
    return sum(
×
52
        [
53
            txo.value.get(token.policy_id, {b"": 0}).get(token.token_name, 0)
54
            for txo in txouts
55
            if txo.address == address
56
        ]
57
    )
58

59

60
def resolve_spent_utxo(txins: List[TxInInfo], p: Spending) -> TxOut:
4✔
61
    """Returns the UTxO whose spending should be validated"""
62
    return [txi.resolved for txi in txins if txi.out_ref == p.tx_out_ref][0]
×
63

64

65
def resolve_datum_unsafe(txout: TxOut, tx_info: TxInfo) -> BuiltinData:
4✔
66
    """
67
    Returns the datum attached to a given transaction output, independent of whether it was inlined or embedded.
68
    Raises an exception if no datum was attached.
69
    """
70
    attached_datum = txout.datum
×
71
    if isinstance(attached_datum, SomeOutputDatumHash):
×
72
        res = tx_info.data[attached_datum.datum_hash]
×
73
    elif isinstance(attached_datum, SomeOutputDatum):
×
74
        res = attached_datum.datum
×
75
    else:
76
        # no datum attached
77
        assert False, "No datum was attached to the given transaction output"
×
78
    return res
×
79

80

81
def resolve_datum(
4✔
82
    txout: TxOut, tx_info: TxInfo
83
) -> Union[SomeOutputDatum, NoOutputDatum]:
84
    """
85
    Returns SomeOutputDatum with the datum attached to a given transaction output,
86
    independent of whether it was inlined or embedded, if there was an attached datum.
87
    Otherwise it returns NoOutputDatum.
88
    """
89
    attached_datum = txout.datum
×
90
    if isinstance(attached_datum, SomeOutputDatumHash):
×
91
        res: Union[SomeOutputDatum, NoOutputDatum] = SomeOutputDatum(
×
92
            tx_info.data[attached_datum.datum_hash]
93
        )
94
    else:
95
        res: Union[SomeOutputDatum, NoOutputDatum] = attached_datum
×
96
    return res
×
97

98

99
def own_spent_utxo(txins: List[TxInInfo], p: Spending) -> TxOut:
4✔
100
    # obtain the resolved txout that is going to be spent from this contract address
101
    for txi in txins:
×
102
        if txi.out_ref == p.tx_out_ref:
×
103
            own_txout = txi.resolved
×
104
    # This throws a name error if the txout was not found
105
    return own_txout
×
106

107

108
def own_policy_id(own_spent_utxo: TxOut) -> PolicyId:
4✔
109
    # obtain the policy id for which this contract can validate minting/burning
110
    cred = own_spent_utxo.address.payment_credential
×
111
    if isinstance(cred, ScriptCredential):
×
112
        policy_id = cred.credential_hash
×
113
    # This throws a name error if the credential is not a ScriptCredential instance
114
    return policy_id
×
115

116

117
def own_address(own_policy_id: PolicyId) -> Address:
4✔
118
    return Address(ScriptCredential(own_policy_id), NoStakingCredential())
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc