Ejemplo n.º 1
0
def test3():
    print('Transfer with flags')

    # Deploy the sender's contract and register nickname to be used in the output
    sender = ts4.BaseContract('tutorial09', {}, nickname='Sender')
    balance_sender = sender.balance

    # Deploy the another one recipient's contract and register nickname to be used in the output
    recipient = ts4.BaseContract('tutorial09', {}, nickname='Recipient')
    addr_recipient = recipient.address
    balance_recipient = recipient.balance

    # Send grams to the recipient (regular transfer)
    amount = 3 * ts4.GRAM
    params = dict(addr=addr_recipient, amount=amount, flags=0)
    sender.call_method('send_grams_with_flags', params)

    # Dispatch created message
    ts4.dispatch_one_message()

    # Сheck the current balance of the sender and recipient
    sender.ensure_balance(balance_sender - amount)
    recipient.ensure_balance(balance_recipient + amount)

    # Send remainig balance and self-destroy sender's contract
    params = dict(addr=addr_recipient, amount=0, flags=160)
    sender.call_method('send_grams_with_flags', params)
    ts4.dispatch_one_message()

    # Сheck the current balance of the recipient, it's should be increased by sender's balance
    recipient.ensure_balance(balance_recipient + balance_sender)
    # Balance of the sender should be None, because of the contract destroyed
    assert eq(None, ts4.get_balance(sender.address))
Ejemplo n.º 2
0
def test2():
    print('Transfer with payload')
    # Deploy the sender's contract and register nickname to be used in the output
    sender = ts4.BaseContract('tutorial09', {}, nickname='Sender')
    balance_sender = sender.balance

    # Deploy the another one recipient's contract and register nickname to be used in the output
    recipient = ts4.BaseContract('tutorial09', {}, nickname='Recipient')
    addr_recipient = recipient.address
    balance_recipient = recipient.balance

    # Send grams to the recipient without payload
    amount = 2 * ts4.GRAM
    comment = 'some comment'
    params = dict(addr=addr_recipient,
                  amount=amount,
                  comment=ts4.str2bytes(comment))
    sender.call_method('send_grams_with_payload', params)

    # Dispatch created message
    ts4.dispatch_one_message()

    # Сheck the current balance of the sender and recipient
    sender.ensure_balance(balance_sender - amount)
    recipient.ensure_balance(balance_recipient + amount)

    # Pick up event that was created by called method of the called contract
    event = ts4.pop_event()
    decoded = recipient.decode_event(event)
    # Check correctness of the received data
    assert eq(comment, decoded.comment)
    assert eq(amount, decoded.amount)
Ejemplo n.º 3
0
def test1():
    # In this scenario we are processing messages step by step

    print('Starting call chain (step by step)...')
    t_value = 4276994270
    contract1.call_method('ping_neighbor',
                          dict(neighbor=neighbor2, value=t_value))

    # Get internal message that was created by previous call
    msg_ping = ts4.peek_msg()
    assert eq(neighbor1, msg_ping.src)
    assert eq(neighbor2, msg_ping.dst)
    assert msg_ping.is_call('ping')
    assert eq(t_value, int(msg_ping.params['request']))

    # Dispatch created message
    ts4.dispatch_one_message()

    # Pick up event that was created by called method of the callee contract
    msg_event1 = ts4.pop_event()

    # Check correctness of event addresses
    assert msg_event1.is_event('ReceivedRequest',
                               src=neighbor2,
                               dst=ts4.Address(None))
    assert eq(t_value, int(msg_event1.params['request']))

    # Get internal message that was created by last call
    msg_pong = ts4.peek_msg()
    assert eq(neighbor2, msg_pong.src)
    assert eq(neighbor1, msg_pong.dst)
    assert msg_pong.is_call('pong')
    assert eq(t_value, int(msg_pong.params['reply']))

    # Dispatch next message
    ts4.dispatch_one_message()

    # Pick up last event and check its parameters
    msg_event2 = ts4.pop_event()
    assert msg_event2.is_event('ReceivedReply',
                               src=neighbor1,
                               dst=ts4.Address(None))
    assert eq(t_value, int(msg_event2.params['reply']))

    # Working with raw JSON data is not always convenient. That's why we
    # provide a way to decode data:
    event2 = contract1.decode_event(msg_event2)
    assert eq(t_value, event2.reply)
Ejemplo n.º 4
0
    def test_exchanger(self):
        ts4.reset_all()  # reset all data
        ts4.init('./', verbose=True)
        key1 = ts4.make_keypair()
        self.public1 = key1[1]
        self.secret1 = key1[0]
        now = int(time.time())
        ts4.core.set_now(now)
        test = ts4.BaseContract('test',
                                dict(),
                                pubkey=self.public,
                                private_key=self.secret,
                                balance=150_000_000_000,
                                nickname="test")
        now += 5
        ts4.core.set_now(now)
        main = ts4.BaseContract('main',
                                dict(),
                                pubkey=self.public,
                                private_key=self.secret,
                                balance=150_000_000_000,
                                nickname="main")
        now += 5
        ts4.core.set_now(now)
        test.call_method("change_address",
                         dict(_adr=main.addr),
                         private_key=self.secret)
        now += 5
        ts4.core.set_now(now)
        test.call_method("createTimer",
                         dict(_payload=1, _time=20),
                         private_key=self.secret)

        while len(ts4.globals.QUEUE) > 0:
            now += 5
            ts4.core.set_now(now)
            ts4.dispatch_one_message()
        print(test.call_getter("results"))
Ejemplo n.º 5
0
def test1():
    print('Transfer with bounce')
    # Deploy the sender's contract and register nickname to be used in the output
    sender = ts4.BaseContract('tutorial09', {}, nickname='Sender')
    addr_sender = sender.address
    balance_sender = 100 * ts4.GRAM

    # Сheck the sender's initial balance. There are 100 grams by default
    sender.ensure_balance(balance_sender)

    # The contract address of the recipient
    addr_recipient = ts4.Address(
        '0:c4a31362f0dd98a8cc9282c2f19358c888dfce460d93adb395fa138d61ae5069')

    # Register nickname to be used in the output
    ts4.register_nickname(addr_recipient, 'Recipient1')

    # Сheck the recipient's balance. Until is not deployed it has no balance
    assert eq(None, ts4.get_balance(addr_recipient))

    # Send grams to the recipient with bounce flag
    amount = ts4.GRAM
    params = dict(addr=addr_recipient, amount=amount, bounce=True)
    sender.call_method('send_grams', params)

    # Pick up internal message that was created by `send_grams()` call
    msg_transfer = ts4.peek_msg()
    assert eq(addr_sender, msg_transfer.src)
    assert eq(addr_recipient, msg_transfer.dst)
    assert eq(amount, msg_transfer.value)

    # Dispatch created message
    ts4.dispatch_one_message()

    # Сheck the sender's current balance
    sender.ensure_balance(balance_sender - amount)

    # Pick up internal message that was bounced
    msg_bounced = ts4.peek_msg()
    assert eq(addr_recipient, msg_bounced.src)
    assert eq(addr_sender, msg_bounced.dst)
    assert eq(amount, msg_bounced.value)
    assert eq(True, msg_bounced.bounced)

    # Dispatch bounced message
    ts4.dispatch_one_message()

    # Balance of the recipient should stay empty
    assert eq(None, ts4.get_balance(addr_recipient))

    print('Transfer without bounce')
    # Send grams to the recipient without bounce flag
    params = dict(addr=addr_recipient, amount=amount, bounce=False)
    sender.call_method('send_grams', params)

    # Dispatch created message
    ts4.dispatch_one_message()

    # Check balance of the recipient, it should be equal to transferred amount
    assert eq(amount, ts4.get_balance(addr_recipient))

    # Сhecking the sender's balance, it should be decreased by the amount of the transfer
    sender.ensure_balance(balance_sender - amount)
Ejemplo n.º 6
0
# Initialize TS4 by specifying where the artifacts of the used contracts are located
# verbose: toggle to print additional execution info
ts4.init('contracts/', verbose=True)

# Deploy a contract (encoder/sender)
sender = ts4.BaseContract('tutorial10_1', {})

# Register nickname to be used in the output
ts4.register_nickname(sender.address, 'Sender')

# Deploy a contract (receiver)
receiver = ts4.BaseContract('tutorial10_2', {})
ts4.register_nickname(receiver.address, 'Receiver')

# Ensure that current value in the receiver contract is default
assert eq(0, receiver.call_getter('m_value'))

value = 0xbeaf
# Encode calling of the receiver contract
payload = sender.call_getter('encode', {'value': value})

# Call receiver contract's method via sender contract
sender.call_method('call_it', {'dest': receiver.address, 'payload': payload})

# Dispatch created internal message from sender to receiver
ts4.dispatch_one_message()

# Ensure that current value was set
assert eq(value, receiver.call_getter('m_value'))
Ejemplo n.º 7
0
                "chunkSize": 0,
                "size": 0,
                "meta": {
                    "height": 0,
                    "width": 0,
                    "duration": 0,
                    "extra": "",
                    "json": "",
                },
            },
        ),
    )


mint(smc_wallet)
ts4.dispatch_one_message()
ts4.dispatch_messages()
mint(smc_wallet_random)
ts4.dispatch_one_message(100)
ts4.dispatch_messages(cb_false)
mint(smc_wallet_random, 8 * 10**9)
ts4.dispatch_one_message(100)
ts4.dispatch_messages()

# Test only fee minting

smc_nft_root = init_smc_nft_root(smc_wallet.address, 1, 5 * 10**9)

mint(smc_wallet)
ts4.dispatch_one_message(100)
ts4.dispatch_messages(cb_false)