def test(): alice = sp.test_account("Alice") bob = sp.test_account("Robert") scenario = sp.test_scenario() scenario.h1("Atomic Swap") # Here, two AtomicSwap contracts are created. One with Alice as the owner # and Bob as the counterparty, and the second with the identities reversed. # They are both secured with the same hash secret, so if the secret gets # revealed, then both swaps can happen. hashSecret = sp.blake2b(sp.bytes("0x12345678aabb")) c1 = AtomicSwap(sp.mutez(12), sp.timestamp(50), hashSecret, alice.address, bob.address) c2 = AtomicSwap(sp.mutez(20), sp.timestamp(50), hashSecret, bob.address, alice.address) scenario.h1("c1") scenario += c1 scenario += c1.knownSecret(secret = sp.bytes("0x12345678aa")).run(sender = bob, valid = False) scenario += c1.knownSecret(secret = sp.bytes("0x12345678aabb")).run(sender = bob) scenario.h1("c2") scenario += c2 scenario.h2("C2.export()") scenario.p(c2.export())
def test(): scenario = sp.test_scenario() scenario.h1("Chainlink Oracles") scenario.table_of_contents() scenario.h2("Accounts") admin = sp.test_account("Administrator") bob = sp.address("tz1WdHqcAo7gjtFDPMgD6yN8pk1sq67MJRjH") oracle1 = sp.test_account("Oracle1") client1_admin = sp.test_account("Client1 admin") client2_admin = sp.test_account("Client2 admin") scenario.show([admin, oracle1, client1_admin, client2_admin]) scenario.h2("Link Token") link_token = Link_token(FA2.FA2_config(single_asset = True), admin.address) scenario += link_token scenario += link_token.mint(address = admin.address, amount = 200, symbol = 'tzLINK', token_id = 0).run(sender = admin) scenario.h2("Token Faucet") faucet = TokenFaucet(admin.address, link_token, link_token.address, 10) scenario += faucet scenario.h2("Oracle") oracle = Oracle(oracle1.address, link_token) scenario += oracle scenario.h2("Client1") client1 = Client(link_token.address, oracle.address, sp.bytes("0x0001"), client1_admin.address) scenario += client1 scenario.h2("Client2") client2 = Client(link_token.address, oracle.address, sp.bytes("0x0001"), client2_admin.address) scenario += client2 scenario.h2("Tokens") scenario += link_token.transfer([sp.record(from_ = admin.address, txs = [sp.record(to_ = faucet.address, token_id = 0, amount = 100)])]).run(sender = admin) scenario += link_token.transfer([sp.record(from_ = admin.address, txs = [sp.record(to_ = oracle1.address, token_id = 0, amount = 1)])]).run(sender = admin) scenario += faucet.request_tokens(sp.set([client1.address, client2.address])) #scenario += link_token.transfer([sp.record(from_ = client1_admin.address, txs = [sp.record(to_ = client1.address, token_id = 0, amount = 10)])]).run(sender = client1_admin) scenario.h2("Client1 sends a request that gets fulfilled") scenario.h3("A request") scenario += client1.request_fortune(payment = 1, timeout = 10).run(sender = bob)
def __init__(self, initial_auction_house_address): self.init( initial_auction_house_address = initial_auction_house_address, ledger = sp.big_map(tkey=LedgerKey.get_type(), tvalue=sp.TNat), token_metadata = sp.bytes('0x697066733a2f2f516d5477794e383547667a6942354247684632454c6f67654a527436437765765a5937323962616851714b486944'), total_supply=sp.big_map(tkey=sp.TNat, tvalue = sp.TNat), allowances = sp.big_map(tkey=AllowanceKey.get_type(), tvalue=sp.TBool) )
def test(): hashSecret = sp.blake2b(sp.bytes("0x12345678aabb")) alice = sp.test_account("Alice") bob = sp.test_account("Robert") c1 = AtomicSwap(sp.mutez(12), sp.timestamp(50), hashSecret, alice.address, bob.address) scenario = sp.test_scenario() scenario.h1("Atomic Swap") scenario += c1
def test(): scenario = sp.test_scenario() scenario.h1("Escrow") hashSecret = sp.blake2b(sp.bytes("0x01223344")) alice = sp.test_account("Alice") bob = sp.test_account("Bob") c1 = Escrow(alice.address, sp.tez(50), bob.address, sp.tez(4), sp.timestamp(123), hashSecret) scenario += c1 # Alice (owner) is adding some tez to the contract. scenario += c1.addBalanceOwner().run(sender=alice, amount=sp.tez(50)) # Bob (counterparty) is adding some tez to the contract. scenario += c1.addBalanceCounterparty().run(sender=bob, amount=sp.tez(4)) scenario.h3("Erronous secret") # Bob tries to claim the funds with an incorrect secret and fails. scenario += c1.claimCounterparty(secret=sp.bytes("0x01223343")).run( sender=bob, valid=False) scenario.h3("Correct secret") # This time Bob claims the funds with a correct secret and claims the total funds. scenario += c1.claimCounterparty(secret=sp.bytes("0x01223344")).run( sender=bob)
def test(): key = '' initialNonce = 1 initialHashedProof = sp.blake2b( sp.bytes("0x62436d677a706263382f2b4f4a4d67367478745233513d3d") ) # 12345678aabc - UTF-8 c1 = SecretStore(initialNonce, initialHashedProof) # show its representation scenario = sp.test_scenario() scenario.h2("Contract") scenario += c1 # now store secret data encrypted with key using aes scenario.h1('Test Store Secret 1') encryptedData = 'abcdefgh' proof = sp.bytes("0x62436d677a706263382f2b4f4a4d67367478745233513d3d" ) # 12345678aabc - UTF-8 # create next token nexthashedProof = sp.blake2b( sp.bytes("0x313233343536373861626263")) # 12345678aabc - UTF-8 c2 = c1.add_secret(proof=proof, nexthashedProof=nexthashedProof, encryptedData=encryptedData).run() scenario += c2 # now store proof data encrypted with key using aes scenario.h1('Test Store Secret 2') encryptedData = 'qwertyuiop' proof = sp.bytes("0x313233343536373861626263") # 0x12345678aabc - UTF-8 # create next token nexthashedProof = sp.blake2b( sp.bytes("0x313233343536373861616264")) # 0x12345678aabd - UTF-8 c2 = c1.add_secret(proof=proof, nexthashedProof=nexthashedProof, encryptedData=encryptedData).run() scenario += c2
def custClose(self, params): sp.verify(self.data.custAddr == sp.sender) sp.verify((self.data.status == OPEN) | (self.data.status == MERCH_CLOSE)) # custClose inputs custBal = params.custBal merchBal = params.merchBal revLock = params.revLock s1 = params.s1 s2 = params.s2 # Prepare pairing check inputs g2 = self.data.g2 merchPk0 = self.data.merchPk0 merchPk1 = self.data.merchPk1 merchPk2 = self.data.merchPk2 merchPk3 = self.data.merchPk3 merchPk4 = self.data.merchPk4 chanID = self.data.chanID cust_b = sp.local('cust_b', sp.fst(sp.ediv(custBal, sp.mutez(1)).open_some())) one = sp.local('one', sp.bls12_381_fr("0x01")) cust_bal_b = sp.local("cust_bal_b", sp.mul(cust_b.value, one.value)) merch_b = sp.local('merch_b', sp.fst(sp.ediv(merchBal, sp.mutez(1)).open_some())) merch_bal_b = sp.local("merch_bal_b", sp.mul(merch_b.value, one.value)) revLockConcat = sp.local('revLockConcat', sp.concat([sp.bytes("0x050a00000020"), revLock])) rev_lock_b = sp.local('rev_lock_b', sp.unpack(revLockConcat.value, t = sp.TBls12_381_fr).open_some()) # Verify signature val1 = sp.local("val1", sp.mul(merchPk0, chanID)) val2 = sp.local("val2", sp.mul(merchPk1, rev_lock_b.value)) val3 = sp.local("val3", sp.mul(merchPk2, cust_bal_b.value)) val4 = sp.local("val4", sp.mul(merchPk3, merch_bal_b.value)) prod1 = sp.local("prod1", val1.value + val2.value + val3.value + val4.value + merchPk4) g2_negated = - g2 pair_list = sp.local("pair_list", [sp.pair(s1, prod1.value), sp.pair(s2, g2_negated)]) out = sp.local('out', False) sp.verify(sp.pairing_check(pair_list.value)) # Update on-chain state and transfer merchant's balance self.data.custBal = custBal self.data.revLock = revLock self.data.delayExpiry = sp.now.add_seconds(self.data.selfDelay) sp.send(self.data.merchAddr, merchBal) self.data.merchBal = sp.tez(0) self.data.status = CUST_CLOSE
def test(): scenario = sp.test_scenario() link_admin_address = sp.address('tz1UBSgsJTTBQWGieRPZmwG3gpAy3kHd7N4y') link_token = Link_token(FA2.FA2_config(single_asset = True), link_admin_address) scenario += link_token link_token_address = sp.address('KT1TQR3eyYCytqBK9EB28J1taa2cX41F9R8x') token_faucet = TokenFaucet(link_admin_address, link_token, link_token_address, 10 * wLINK_decimals) scenario += token_faucet token_faucet_address = sp.address('KT1JWENqDEoGasUty7m22QBPk6gfau8H4VQS') oracle1_admin_address = sp.address('tz1fextP23D6Ph2zeGTP8EwkP5Y8TufeFCHA') oracle1 = Oracle(oracle1_admin_address, link_token, token_address = link_token_address) scenario += oracle1 oracle1_address = sp.address('KT1VnsKJu8KKnut6qCqig4LVFv3n8wqq6fpy') client1_admin_address = sp.address('tz1axGtTkg1hJvGenTrhqpFbW1S8GcQpPdve') client1 = Client(link_token_address, oracle1_address, sp.bytes("0x0001"), client1_admin_address) scenario += client1 client1_address = sp.address('KT1K7LyozUeLVv5yu6XeVFwQm2feEGNQXKUN')
def test(): chain_id=sp.chain_id_cst("0x9caecab9") scenario = sp.test_scenario() scenario.h1("Generic Multi Signature Executor") scenario.table_of_contents() admin = sp.test_account("Administrator") alice = sp.test_account("Alice") bob = sp.test_account("Robert") dan = sp.test_account("Dan") # Let's display the accounts: scenario.h2("Accounts") scenario.show([admin, alice, bob, dan]) admin = sp.test_account("Administrator") # We have not found a way to pack for signature lambdas in smartpy directly # Hence we are using the tezos-client for helping out # The following payload assumes chain id 0x7a06a770, nonce 1, and a lambda hitting the honey pot at address tz1d393dnaDkbGk8GiKhhy1PX5qgF8XDKpEz # tezos-client -S -P 443 -A delphinet-tezos.giganode.io hash data 'Pair 0x7a06a770 (Pair 1 { DROP; NIL operation; PUSH address "tz1d393dnaDkbGk8GiKhhy1PX5qgF8XDKpEz"; CONTRACT unit; IF_SOME {} { UNIT; FAILWITH }; PUSH mutez 0; UNIT; TRANSFER_TOKENS; CONS })' of type 'pair (chain_id) (pair (nat) (lambda unit (list operation)))' # Raw packed data: 0x0507070a000000047a06a77007070001020000004a0320053d036d0743036e0a000000160000bed9025f33e3e29f09c45528114877c7628099e60555036c0200000010072f0200000004034f032702000000000743036a0000034f034d031b honey_pot = HoneyPot() honey_pot.address = sp.address("tz1d393dnaDkbGk8GiKhhy1PX5qgF8XDKpEz") executor = Executor(sp.nat(2), [sp.key("edpkumVGEtDQgDAcMyB5FRn7UBLuhzg6D7aEnCDamXgjqGxavnsgvP"), sp.key("edpkv6MFhAVnpAhrvMjdW1tGfLDDiU6i6P9kt3ZmyiiMpxCsB5Cg4v"), sp.key("edpktwdcwT8iMDWcg2ePJ6J1sbwXYdn7GYJWyRybi8vpkrv4aduFHt")]) scenario += executor scenario += honey_pot def execution_payload(x): sp.set_type(x, sp.TUnit) honey_pot_contract = sp.contract(sp.TUnit, honey_pot.address).open_some() sp.result([sp.transfer_operation(sp.unit, sp.mutez(0), honey_pot_contract)]) #lambda_packer = LambdaPacker(honey_pot.address) #scenario += lambda_packer #scenario += lambda_packer.pack_signing_payload() #execution_payload_pack = sp.pack(execution_payload) #scenario.show(execution_payload_pack) #signing_payload = ExecutionRequest.get_signing_payload(chain_id, sp.nat(1), execution_payload) alice_signature = sp.make_signature(alice.secret_key, sp.bytes("0x0507070a000000047a06a77007070001020000004a0320053d036d0743036e0a000000160000bed9025f33e3e29f09c45528114877c7628099e60555036c0200000010072f0200000004034f032702000000000743036a0000034f034d031b")) bob_signature = sp.make_signature(bob.secret_key, sp.bytes("0x0507070a000000047a06a77007070001020000004a0320053d036d0743036e0a000000160000bed9025f33e3e29f09c45528114877c7628099e60555036c0200000010072f0200000004034f032702000000000743036a0000034f034d031b")) dan_signature = sp.make_signature(dan.secret_key, sp.bytes("0x0507070a000000047a06a77007070001020000004a0320053d036d0743036e0a000000160000bed9025f33e3e29f09c45528114877c7628099e60555036c0200000010072f0200000004034f032702000000000743036a0000034f034d031b")) signatures= sp.map({sp.hash_key(bob.public_key):bob_signature, sp.hash_key(alice.public_key): alice_signature, sp.hash_key(dan.public_key):dan_signature}) execution_request = sp.set_type_expr(sp.record(execution_payload=execution_payload, signatures=signatures),ExecutionRequest.get_type()) scenario.show(signatures) executor = TimeLockedExecutor(sp.nat(2), [alice.public_key, bob.public_key, dan.public_key], sp.nat(60)) scenario += executor signatures= sp.map({sp.hash_key(bob.public_key):bob_signature, sp.hash_key(alice.public_key): alice_signature, sp.hash_key(dan.public_key):dan_signature}) execution_request = sp.set_type_expr(sp.record(execution_payload=execution_payload, signatures=signatures),ExecutionRequest.get_type()) #scenario += executor.update_signatories() #scenario += executor.execute(execution_request).run(sender=admin, chain_id=sp.chain_id_cst("0x7a06a770")) #scenario += executor.default(test2).run(sender=admin) #scenario += honey_pot
def request_xtzusd(self): self.request_helper(2, sp.bytes("0x"), specify("XTZUSD"), self.data.oracle, self.data.waiting_xtzusd_id, sp.self_entry_point("set_xtzusd"))
def test(): alice = sp.test_account("Alice") bob = sp.test_account("Bob") admin = sp.test_account("Admin") swapHash = sp.bytes("0x68656c6c6f666473667364666c64736a666c73646a6664736a6673646a6b666a") swapContract = Swap(_admin=admin.address, _swapFee=15, _commission=1000000) token_metadata = { "decimals" : "18", # Mandatory by the spec "name" : "My Great Token", # Recommended "symbol" : "MGT", # Recommended # Extra fields "icon" : 'https://smartpy.io/static/img/logo-only.svg' } contract_metadata = { "" : "ipfs://QmaiAUj1FFNGYTu8rLBjc3eeN9cSKwaF8EGMBNDmhzPNFd", } tokenContract = FA12.FA12(admin.address, config = FA12.FA12_config(support_upgradable_metadata = True), token_metadata = token_metadata, contract_metadata = contract_metadata) scenario = sp.test_scenario() scenario.table_of_contents() scenario.h2("Accounts") scenario.show([admin, alice, bob]) scenario.h2("Swap") scenario += swapContract scenario.h2("FA1.2") scenario += tokenContract scenario.h3("Admin mints token balances to participants") scenario += tokenContract.mint(address=alice.address, value=2000000).run(sender=admin) scenario += tokenContract.mint(address=bob.address, value=3000000).run(sender=admin) scenario.h3("Alice & Bob approve token balances for the Swap contract") scenario += tokenContract.approve(spender=swapContract.address, value=1900000).run(sender=alice) scenario += tokenContract.approve(spender=swapContract.address, value=3000000).run(sender=bob) scenario.h2("Swap Scenarios") scenario.h3("Alice fails to add a swap pair") scenario += swapContract.addSwapPair({"xtz/usdtz":{"xtz": sp.record(counterAsset="usdtz", decimals=6, contract=swapContract.address), "usdtz": sp.record(counterAsset="xtz", decimals=6, contract=tokenContract.address)}}).run(sender=alice, valid=False) scenario.h3("Admin adds a swap pair") scenario += swapContract.addSwapPair({"xtz/usdtz":{"xtz":sp.record(counterAsset="usdtz",decimals=6,contract=swapContract.address), "usdtz":sp.record(counterAsset="xtz",decimals=6,contract=tokenContract.address)}}).run(sender=admin) scenario.h3("Bob fails to offer a swap, swap contract is inactive") # no operations work without contract being active scenario += swapContract.offer(_swapHash=swapHash, _value=2000000, _expected=1900000, _refundTimestamp=sp.timestamp(159682500), _pair="xtz/usdtz", _asset="xtz").run(sender=bob, amount=sp.tez(3), now=sp.timestamp(159682400), valid=False) # activate only by admin scenario.h3("Alice fails activate swap contract") scenario += swapContract.toggleContractState(True).run(sender=alice, valid=False) scenario.h3("Admin activates swap contract") scenario += swapContract.toggleContractState(True).run(sender=admin) # swap must include service fee scenario.h3("Bob fails to offer a swap without service fee included in amount") scenario += swapContract.offer(_swapHash=swapHash, _value=2000000, _expected=1900000, _refundTimestamp=sp.timestamp(159682500), _pair="xtz/usdtz", _asset="xtz").run(sender=bob, amount=sp.tez(2), now=sp.timestamp(159682400), valid=False) # initiate new swap scenario.h3("Bob offers a swap") scenario += swapContract.offer(_swapHash=swapHash, _value=2000000, _expected=1900000, _refundTimestamp=sp.timestamp(159682500), _pair="xtz/usdtz", _asset="xtz").run(sender=bob, amount=sp.tez(3), now=sp.timestamp(159682400)) # cannot initiate new swap with same hash scenario.h3("Alice fails to offer a swap with duplicate hash") scenario += swapContract.offer(_swapHash=swapHash, _value=2000000, _expected=1900000, _refundTimestamp=sp.timestamp(159682500), _pair="xtz/usdtz", _asset="xtz").run(sender=alice, amount=sp.tez(3), now=sp.timestamp(159682400), valid=False) # balance check scenario.verify(swapContract.balance == sp.tez(3)) scenario.h3("Alice fails to take swap below expected return") # cannot redeem without value matching the required expected value scenario += swapContract.take(_swapHash=swapHash, _value=1800000, _pair="xtz/usdtz", _asset="xtz").run(sender=alice, now=sp.timestamp(159682450), valid=False) scenario.h3("Alice takes swap at expected value") scenario += swapContract.take(_swapHash=swapHash, _value=1900000, _pair="xtz/usdtz", _asset="usdtz").run(sender=alice, now=sp.timestamp(159682450)) scenario.verify(swapContract.balance == sp.tez(1)) scenario.verify(tokenContract.data.balances[bob.address].balance == sp.nat(4900000)) scenario.h3("Alice fails to update swap fees") # update fees only by admin scenario += swapContract.updateSwapFees(_swapFee=20, _commission=1500000).run(sender=alice, valid=False) scenario.h3("Admin updates swap fees") scenario += swapContract.updateSwapFees(_swapFee=20, _commission=1500000).run(sender=admin) scenario.h3("Bob fails to offer a swap with outdated fees") # need to use updated service fees scenario += swapContract.offer(_swapHash=swapHash, _value=2000000, _expected=1900000, _refundTimestamp=sp.timestamp(159682500), _pair="xtz/usdtz", _asset="usdtz").run(sender=bob, amount=sp.tez(1), now=sp.timestamp(159682400), valid=False) scenario.h3("Bob offers a new swap with updated commission, reusing a hash") scenario += swapContract.offer(_swapHash=swapHash, _value=2000000, _expected=1900000, _refundTimestamp=sp.timestamp(159682500), _pair="xtz/usdtz", _asset="usdtz").run(sender=bob, amount=sp.utils.nat_to_mutez(1500000), now=sp.timestamp(159682400)) # balance check scenario.verify(swapContract.balance == sp.utils.nat_to_mutez(2500000)) # # cannot be refunded before the refund time scenario += swapContract.refund(swapHash).run(sender=bob, now=sp.timestamp(159682450), valid=False) scenario.h3("Alice refunds Bob's expired swap") # can be refunded by anyone but funds transferred only to initiator after expiry scenario += swapContract.refund(swapHash).run(sender=alice, now=sp.timestamp(159682550)) scenario.h3("Alice fails to refund a removed swap") # cannot be refunded again once it has been refunded scenario += swapContract.refund(swapHash).run(sender=alice, now=sp.timestamp(159682550), valid=False) # balance check [refunded swap service fees are also returned] scenario.verify(swapContract.balance == sp.tez(1)) scenario.h2("Other admin functions") scenario.h3("Alice fails to remove a pair") # only admin can remove swap pair scenario += swapContract.removeSwapPair(["xtz/usdtz"]).run(sender=alice, valid=False) scenario.h3("Admin removes a pair") scenario += swapContract.removeSwapPair(["xtz/usdtz"]).run(sender=admin) # only swap pairs available can be used scenario += swapContract.offer(_swapHash=swapHash, _value=2000000, _expected=1900000, _refundTimestamp=sp.timestamp(159682500), _pair="xtz/usdtz", _asset="xtz").run(sender=bob, amount=sp.utils.nat_to_mutez(3500000), now=sp.timestamp(159682400), valid=False) scenario.h3("Alice fails to set a delegate") voting_powers = { sp.key_hash("tz1YB12JHVHw9GbN66wyfakGYgdTBvokmXQk"): 0, } # only admin can set baker scenario += swapContract.setBaker(sp.some(sp.key_hash("tz1YB12JHVHw9GbN66wyfakGYgdTBvokmXQk"))).run(sender=alice,voting_powers = voting_powers, valid=False) scenario.h3("Admin sets a delegate") scenario += swapContract.setBaker(sp.some(sp.key_hash("tz1YB12JHVHw9GbN66wyfakGYgdTBvokmXQk"))).run(sender=admin,voting_powers = voting_powers) scenario.verify(swapContract.baker==sp.some(sp.key_hash("tz1YB12JHVHw9GbN66wyfakGYgdTBvokmXQk"))) scenario.h3("Alice fails to withdraw commission fees") # only admin can transfer service fees scenario += swapContract.transferFees(bob.address).run(sender=alice, valid=False) scenario.h3("Update Baker") scenario += swapContract.transferFees(bob.address).run(sender=admin) scenario.verify(swapContract.balance == sp.tez(0)) scenario.h3("Transfer rights from Admin to Bob") scenario += swapContract.updateAdmin(bob.address).run(sender=admin)
def test(): scenario = sp.test_scenario() scenario.h1("FA1.2 template - Fungible assets") scenario.table_of_contents() # sp.test_account generates ED25519 key-pairs deterministically: admin = sp.test_account("Administrator") alice = sp.test_account("Alice") bob = sp.test_account("Robert") # Let's display the accounts: scenario.h1("Accounts") scenario.show([admin, alice, bob]) scenario.h1("Contract") token_metadata = { "decimals" : "18", # Mandatory by the spec "name" : "My Great Token", # Recommended "symbol" : "MGT", # Recommended # Extra fields "icon" : 'https://smartpy.io/static/img/logo-only.svg' } contract_metadata = { "" : "ipfs://QmaiAUj1FFNGYTu8rLBjc3eeN9cSKwaF8EGMBNDmhzPNFd", } c1 = FA12( admin.address, config = FA12_config(support_upgradable_metadata = True), token_metadata = token_metadata, contract_metadata = contract_metadata ) scenario += c1 scenario.h1("Offchain view - token_metadata") # Test token_metadata view offchainViewTester = TestOffchainView(c1.token_metadata) scenario.register(offchainViewTester) offchainViewTester.compute(data = c1.data, params = 0) scenario.verify_equal( offchainViewTester.data.result, sp.some( sp.pair( 0, sp.map({ "decimals" : sp.utils.bytes_of_string("18"), "name" : sp.utils.bytes_of_string("My Great Token"), "symbol" : sp.utils.bytes_of_string("MGT"), "icon" : sp.utils.bytes_of_string('https://smartpy.io/static/img/logo-only.svg') }) ) ) ) scenario.h1("Attempt to update metadata") scenario.verify( c1.data.metadata[""] == sp.utils.bytes_of_string("ipfs://QmaiAUj1FFNGYTu8rLBjc3eeN9cSKwaF8EGMBNDmhzPNFd") ) c1.update_metadata(key = "", value = sp.bytes("0x00")).run(sender = admin) scenario.verify(c1.data.metadata[""] == sp.bytes("0x00")) scenario.h1("Entry points") scenario.h2("Admin mints a few coins") c1.mint(address = alice.address, value = 12).run(sender = admin) c1.mint(address = alice.address, value = 3).run(sender = admin) c1.mint(address = alice.address, value = 3).run(sender = admin) scenario.h2("Alice transfers to Bob") c1.transfer(from_ = alice.address, to_ = bob.address, value = 4).run(sender = alice) scenario.verify(c1.data.balances[alice.address].balance == 14) scenario.h2("Bob tries to transfer from Alice but he doesn't have her approval") c1.transfer(from_ = alice.address, to_ = bob.address, value = 4).run(sender = bob, valid = False) scenario.h2("Alice approves Bob and Bob transfers") c1.approve(spender = bob.address, value = 5).run(sender = alice) c1.transfer(from_ = alice.address, to_ = bob.address, value = 4).run(sender = bob) scenario.h2("Bob tries to over-transfer from Alice") c1.transfer(from_ = alice.address, to_ = bob.address, value = 4).run(sender = bob, valid = False) scenario.h2("Admin burns Bob token") c1.burn(address = bob.address, value = 1).run(sender = admin) scenario.verify(c1.data.balances[alice.address].balance == 10) scenario.h2("Alice tries to burn Bob token") c1.burn(address = bob.address, value = 1).run(sender = alice, valid = False) scenario.h2("Admin pauses the contract and Alice cannot transfer anymore") c1.setPause(True).run(sender = admin) c1.transfer(from_ = alice.address, to_ = bob.address, value = 4).run(sender = alice, valid = False) scenario.verify(c1.data.balances[alice.address].balance == 10) scenario.h2("Admin transfers while on pause") c1.transfer(from_ = alice.address, to_ = bob.address, value = 1).run(sender = admin) scenario.h2("Admin unpauses the contract and transferts are allowed") c1.setPause(False).run(sender = admin) scenario.verify(c1.data.balances[alice.address].balance == 9) c1.transfer(from_ = alice.address, to_ = bob.address, value = 1).run(sender = alice) scenario.verify(c1.data.totalSupply == 17) scenario.verify(c1.data.balances[alice.address].balance == 8) scenario.verify(c1.data.balances[bob.address].balance == 9) scenario.h1("Views") scenario.h2("Balance") view_balance = Viewer(sp.TNat) scenario += view_balance c1.getBalance((alice.address, view_balance.typed.target)) scenario.verify_equal(view_balance.data.last, sp.some(8)) scenario.h2("Administrator") view_administrator = Viewer(sp.TAddress) scenario += view_administrator c1.getAdministrator((sp.unit, view_administrator.typed.target)) scenario.verify_equal(view_administrator.data.last, sp.some(admin.address)) scenario.h2("Total Supply") view_totalSupply = Viewer(sp.TNat) scenario += view_totalSupply c1.getTotalSupply((sp.unit, view_totalSupply.typed.target)) scenario.verify_equal(view_totalSupply.data.last, sp.some(17)) scenario.h2("Allowance") view_allowance = Viewer(sp.TNat) scenario += view_allowance c1.getAllowance((sp.record(owner = alice.address, spender = bob.address), view_allowance.typed.target)) scenario.verify_equal(view_allowance.data.last, sp.some(1))
def test(): scenario = sp.test_scenario() scenario.table_of_contents() scenario.h1("zkChannels") aliceCust = sp.test_account("Alice") bobMerch = sp.test_account("Bob") scenario.h2("Parties") scenario.p("We start with two accounts Alice (customer) and Bob (merchant):") scenario.show([aliceCust, bobMerch]) # Set zkChannel parameters chanID = sp.bls12_381_fr(CHAN_ID_FR) custAddr = aliceCust.address merchAddr = bobMerch.address revLock = sp.sha256(sp.bytes("0x12345678aabb")) # selfDelay = 60*60*24 # seconds in one day - 86,400 selfDelay = 3 # seconds in one day - 86,400 scenario.h2("On-chain installment") custFunding = sp.tez(20) merchFunding = sp.tez(10) g2 = sp.bls12_381_g2(PUB_GEN_G2) merchPk0 = sp.bls12_381_g2(MERCH_PK0_G2) merchPk1 = sp.bls12_381_g2(MERCH_PK1_G2) merchPk2 = sp.bls12_381_g2(MERCH_PK2_G2) merchPk3 = sp.bls12_381_g2(MERCH_PK3_G2) merchPk4 = sp.bls12_381_g2(MERCH_PK4_G2) scenario.h2("Scenario 1: escrow -> merchClose -> merchClaim") scenario.h3("escrow") c1 = ZkChannel(chanID, aliceCust.address, bobMerch.address, aliceCust.public_key, bobMerch.public_key, custFunding, merchFunding, selfDelay, revLock, g2, merchPk0, merchPk1, merchPk2, merchPk3, merchPk4) scenario += c1 scenario.h3("Funding the channel") scenario += c1.addFunding().run(sender = aliceCust, amount = custFunding) scenario += c1.addFunding().run(sender = bobMerch, amount = merchFunding) scenario.h3("merchClose") scenario += c1.merchClose().run(sender = bobMerch) scenario.h3("unsuccessful merchClaim before delay period") scenario += c1.merchClaim().run(sender = bobMerch, now = sp.timestamp(1), valid = False) scenario.h3("successful merchClaim after delay period") scenario += c1.merchClaim().run(sender = bobMerch, now = sp.timestamp(100000)) scenario.h2("Scenario 2: escrow -> custClose -> custClaim") scenario.h3("escrow") c2 = ZkChannel(chanID, aliceCust.address, bobMerch.address, aliceCust.public_key, bobMerch.public_key, custFunding, merchFunding, selfDelay, revLock, g2, merchPk0, merchPk1, merchPk2, merchPk3, merchPk4) scenario += c2 scenario.h3("Funding the channel") scenario += c2.addFunding().run(sender = aliceCust, amount = custFunding) scenario += c2.addFunding().run(sender = bobMerch, amount = merchFunding) scenario.p("Now the customer and merchant make a payment off chain.") scenario.p("For the payment to be considered complete, the customer should have received a signature from the merchant reflecting the final balances, and the merchant should have received the secret corresponding to the previous state's revLock.") scenario.h3("custClose") custBal = sp.tez(18) merchBal = sp.tez(12) revLock2 = sp.bytes(REV_LOCK_FR) scenario += c2.custClose( revLock = revLock2, custBal = custBal, merchBal = merchBal, s1 = sp.bls12_381_g1(SIG_S1_G1), s2 = sp.bls12_381_g1(SIG_S2_G1), g2 = sp.bls12_381_g2(PUB_GEN_G2) ).run(sender = aliceCust) scenario.h3("unsuccessful custClaim attempt before delay period") scenario += c2.custClaim().run(sender = aliceCust, now = sp.timestamp(1), valid = False) scenario.h3("successful custClaim after delay period") scenario += c2.custClaim().run(sender = aliceCust, now = sp.timestamp(100000)) scenario.h2("Scenario 3: escrow -> custClose -> merchDispute") scenario.h3("escrow") c3 = ZkChannel(chanID, aliceCust.address, bobMerch.address, aliceCust.public_key, bobMerch.public_key, custFunding, merchFunding, selfDelay, revLock, g2, merchPk0, merchPk1, merchPk2, merchPk3, merchPk4) scenario += c3 scenario.h3("Funding the channel") scenario += c3.addFunding().run(sender = aliceCust, amount = custFunding) scenario += c3.addFunding().run(sender = bobMerch, amount = merchFunding) scenario.h3("custClose") revLock2 = sp.bytes(REV_LOCK_FR) # sp.sha256(sp.bytes("0x12345678aacc")) scenario += c3.custClose( revLock = revLock2, custBal = custBal, merchBal = merchBal, s1 = sp.bls12_381_g1(SIG_S1_G1), s2 = sp.bls12_381_g1(SIG_S2_G1), g2 = sp.bls12_381_g2(PUB_GEN_G2), merchPk0 = sp.bls12_381_g2(MERCH_PK0_G2), merchPk1 = sp.bls12_381_g2(MERCH_PK1_G2), merchPk2 = sp.bls12_381_g2(MERCH_PK2_G2), merchPk3 = sp.bls12_381_g2(MERCH_PK3_G2), merchPk4 = sp.bls12_381_g2(MERCH_PK4_G2) ).run(sender = aliceCust) scenario.h3("merchDispute called with correct secret") # scenario += c3.merchDispute(secret = sp.bytes("0x12345678aacc")).run(sender = bobMerch, now = sp.timestamp(10)) scenario.h2("Scenario 4: escrow -> merchClose -> custClose") scenario.h3("escrow") c4 = ZkChannel(chanID, aliceCust.address, bobMerch.address, aliceCust.public_key, bobMerch.public_key, custFunding, merchFunding, selfDelay, revLock, g2, merchPk0, merchPk1, merchPk2, merchPk3, merchPk4) scenario += c4 scenario.h3("Funding the channel") scenario += c4.addFunding().run(sender = aliceCust, amount = custFunding) scenario += c4.addFunding().run(sender = bobMerch, amount = merchFunding) scenario.h3("merchClose") scenario += c4.merchClose().run(sender = bobMerch) scenario.h3("custClose") revLock3 = sp.sha256(sp.bytes("0x12345678aacc")) scenario += c4.custClose( revLock = revLock2, custBal = custBal, merchBal = merchBal, s1 = sp.bls12_381_g1(SIG_S1_G1), s2 = sp.bls12_381_g1(SIG_S2_G1), g2 = sp.bls12_381_g2(PUB_GEN_G2), merchPk0 = sp.bls12_381_g2(MERCH_PK0_G2), merchPk1 = sp.bls12_381_g2(MERCH_PK1_G2), merchPk2 = sp.bls12_381_g2(MERCH_PK2_G2), merchPk3 = sp.bls12_381_g2(MERCH_PK3_G2), merchPk4 = sp.bls12_381_g2(MERCH_PK4_G2) ).run(sender = aliceCust) scenario.h2("Scenario 5: escrow -> mutualClose") scenario.h3("escrow") c5 = ZkChannel(chanID, aliceCust.address, bobMerch.address, aliceCust.public_key, bobMerch.public_key, custFunding, merchFunding, selfDelay, revLock, g2, merchPk0, merchPk1, merchPk2, merchPk3, merchPk4) scenario += c5 scenario.h3("Funding the channel") scenario += c5.addFunding().run(sender = aliceCust, amount = custFunding) scenario += c5.addFunding().run(sender = bobMerch, amount = merchFunding) # Customer's signature on the latest state custSig = sp.make_signature(aliceCust.secret_key, sp.pack(sp.record(chanID = chanID, custAddr = custAddr, merchAddr = merchAddr, custBal = custBal, merchBal = merchBal))) # Merchant's signature on the latest state merchSig = sp.make_signature(bobMerch.secret_key, sp.pack(sp.record(chanID = chanID, custAddr = custAddr, merchAddr = merchAddr, custBal = custBal, merchBal = merchBal))) scenario.h3("mutualClose") scenario += c5.mutualClose(custBal = custBal, merchBal = merchBal, custSig = custSig, merchSig = merchSig).run(sender = aliceCust) scenario.h2("Scenario 6: escrow -> addCustFunding -> reclaimCustFunding") scenario.h3("escrow") c6 = ZkChannel(chanID, aliceCust.address, bobMerch.address, aliceCust.public_key, bobMerch.public_key, custFunding, merchFunding, selfDelay, revLock, g2, merchPk0, merchPk1, merchPk2, merchPk3, merchPk4) scenario += c6 scenario.h3("Customer Funding their side of the channel") scenario += c6.addFunding().run(sender = aliceCust, amount = custFunding) scenario.h3("Customer pulling out their side of the channel (before merchant funds their side)") scenario += c6.reclaimFunding().run(sender = aliceCust) scenario.table_of_contents()
| Approve | update_operators | | Mint | mint | | Burn | burn | | addOperator | see multisig | | removeOperator | see multisig | | setRedeemAddress | set_redeem_address | | Pause | pause | | Unpause | unpause | | transferOwnership| propose_administrator/remove_administrator | | acceptOwnership | set_administrator | """ import smartpy as sp NULL_ADDRESS = sp.address("tz1YtuZ4vhzzn7ssCt93Put8U9UJDdvCXci4") NULL_BYTES = sp.bytes('0x') class FA2ErrorMessage: """Static enum used for the FA2 related errors, using the `FA2_` prefix""" PREFIX = "FA2_" TOKEN_UNDEFINED = "{}TOKEN_UNDEFINED".format(PREFIX) """This error is thrown if the token id used in to defined""" INSUFFICIENT_BALANCE = "{}INSUFFICIENT_BALANCE".format(PREFIX) """This error is thrown if the source address transfers an amount that exceeds its balance""" NOT_OWNER = "{}_NOT_OWNER".format(PREFIX) """This error is thrown if not the owner is performing an action that he/she shouldn't""" NOT_OPERATOR = "{}_NOT_OPERATOR".format(PREFIX) """This error is thrown if neither token owner nor permitted operators are trying to transfer an amount""" class TokenMetadata: """Token metadata object as per FA2 standard"""
def test(): scenario = sp.test_scenario() scenario.h1("FA1.2 template - Fungible assets") scenario.table_of_contents() # sp.test_account generates ED25519 key-pairs deterministically: admin = sp.test_account("Administrator") alice = sp.test_account("Alice") bob = sp.test_account("Robert") # Let's display the accounts: scenario.h1("Test Accounts") scenario.show([admin, alice, bob]) scenario.h1("Contract") token_metadata = { "decimals" : "6", # Mandatory by the spec "name" : "Oropocket Gold", # Recommended "symbol" : "XTZGold", # Recommended # Extra fields "thumbnailUri" : 'https://oropocket.com/unifarm/assets/icons/oro-gold.png' } contract_metadata = { "" : "ipfs://QmYA6osSz1pzy3YviBV4WQvs8W4L7zCNedVXL3UsiapSsP", } c1 = FA12( admin.address, config = FA12_config(support_upgradable_metadata = True), token_metadata = token_metadata, contract_metadata = contract_metadata ) scenario += c1 scenario.h1("Offchain view - token_metadata") # Test token_metadata view offchainViewTester = TestOffchainView(c1.token_metadata) scenario.register(offchainViewTester) offchainViewTester.compute(data = c1.data, params = 0) scenario.verify_equal( offchainViewTester.data.result, sp.some( sp.record( token_id = 0, token_info = sp.map({ "decimals" : sp.utils.bytes_of_string("6"), "name" : sp.utils.bytes_of_string("Oropocket Gold"), "symbol" : sp.utils.bytes_of_string("XTZGold"), "thumbnailUri" : sp.utils.bytes_of_string('https://oropocket.com/unifarm/assets/icons/oro-gold.png') }) ) ) ) scenario.h1("Attempt to update metadata") scenario.verify( c1.data.metadata[""] == sp.utils.bytes_of_string("ipfs://QmYA6osSz1pzy3YviBV4WQvs8W4L7zCNedVXL3UsiapSsP") ) c1.update_metadata(key = "", value = sp.bytes("0x00")).run(sender = admin) scenario.verify(c1.data.metadata[""] == sp.bytes("0x00")) scenario.h1("Entry points") scenario.h2("Admin mints a few coins") c1.mint(address = alice.address, value = 12000000).run(sender = admin) c1.mint(address = alice.address, value = 3000000).run(sender = admin) c1.mint(address = alice.address, value = 3000000).run(sender = admin) scenario.h2("Alice transfers to Bob") c1.transfer(from_ = alice.address, to_ = bob.address, value = 4000000).run(sender = alice) scenario.verify(c1.data.balances[alice.address].balance == 14000000) scenario.h2("Bob tries to transfer from Alice but he doesn't have her approval") c1.transfer(from_ = alice.address, to_ = bob.address, value = 4000000).run(sender = bob, valid = False) scenario.h2("Alice approves Bob and Bob transfers") c1.approve(spender = bob.address, value = 5000000).run(sender = alice) c1.transfer(from_ = alice.address, to_ = bob.address, value = 4000000).run(sender = bob) scenario.h2("Bob tries to over-transfer from Alice") c1.transfer(from_ = alice.address, to_ = bob.address, value = 4000000).run(sender = bob, valid = False) scenario.h2("Admin burns Bob token") c1.burn(address = bob.address, value = 1000000).run(sender = admin) scenario.verify(c1.data.balances[alice.address].balance == 10000000) scenario.h2("Alice tries to burn Bob token") c1.burn(address = bob.address, value = 1000000).run(sender = alice, valid = False) scenario.h2("Admin pauses the contract and Alice cannot transfer anymore") c1.setPause(True).run(sender = admin) c1.transfer(from_ = alice.address, to_ = bob.address, value = 4000000).run(sender = alice, valid = False) scenario.verify(c1.data.balances[alice.address].balance == 10000000) scenario.h2("Admin transfers while on pause") c1.transfer(from_ = alice.address, to_ = bob.address, value = 1000000).run(sender = admin) scenario.h2("Admin unpauses the contract and transferts are allowed") c1.setPause(False).run(sender = admin) scenario.verify(c1.data.balances[alice.address].balance == 9000000) c1.transfer(from_ = alice.address, to_ = bob.address, value = 1000000).run(sender = alice) scenario.verify(c1.data.totalSupply == 17000000) scenario.verify(c1.data.balances[alice.address].balance == 8000000) scenario.verify(c1.data.balances[bob.address].balance == 9000000) scenario.h1("Views") scenario.h2("Balance") view_balance = Viewer(sp.TNat) scenario += view_balance c1.getBalance((alice.address, view_balance.typed.target)) scenario.verify_equal(view_balance.data.last, sp.some(8000000)) scenario.h2("Administrator") view_administrator = Viewer(sp.TAddress) scenario += view_administrator c1.getAdministrator((sp.unit, view_administrator.typed.target)) scenario.verify_equal(view_administrator.data.last, sp.some(admin.address)) scenario.h2("Total Supply") view_totalSupply = Viewer(sp.TNat) scenario += view_totalSupply c1.getTotalSupply((sp.unit, view_totalSupply.typed.target)) scenario.verify_equal(view_totalSupply.data.last, sp.some(17000000)) scenario.h2("Allowance") view_allowance = Viewer(sp.TNat) scenario += view_allowance c1.getAllowance((sp.record(owner = alice.address, spender = bob.address), view_allowance.typed.target)) scenario.verify_equal(view_allowance.data.last, sp.some(1000000))
def test(): admin = sp.test_account("Administrator") alice = sp.test_account("Alice") bob = sp.test_account("Bob") init_eth = "0x91f79893E7B923410Ef1aEba6a67c6fab0sfsdgffd" hashSecret = sp.sha256(sp.sha256(sp.bytes( "0x68656c6c6f666473667364666c64736a666c73646a6664736a6673646a6b666a"))) token_metadata = { "decimals" : "18", # Mandatory by the spec "name" : "My Great Token", # Recommended "symbol" : "MGT", # Recommended # Extra fields "icon" : 'https://smartpy.io/static/img/logo-only.svg' } contract_metadata = { "" : "ipfs://QmaiAUj1FFNGYTu8rLBjc3eeN9cSKwaF8EGMBNDmhzPNFd", } c2 = FA12.FA12(admin.address, config = FA12.FA12_config(support_upgradable_metadata = True), token_metadata = token_metadata, contract_metadata = contract_metadata) c1 = TokenSwap(_admin=admin.address, _fa12=c2.address) scenario = sp.test_scenario() scenario.table_of_contents() scenario.h1("Atomic Swap") scenario += c1 scenario.h2("Accounts") scenario.show([admin, alice, bob]) scenario.h2("FA1.2") scenario.h3("Entry points") scenario += c2 scenario.h3("Admin mints a few coins") scenario += c2.mint(address=alice.address, value=12).run(sender=admin) scenario += c2.mint(address=alice.address, value=3).run(sender=admin) scenario += c2.mint(address=alice.address, value=3).run(sender=admin) scenario.h2("Alice approves Contract") scenario += c2.approve(spender=c1.address, value=10).run(sender=alice) scenario.h2("Swap[Wait] Testing") # no operations work without contract being active scenario += c1.initiateWait(_hashedSecret=hashSecret, initiator_eth_addr=init_eth, _refundTimestamp=sp.timestamp( 159682500), _amount=5).run(sender=alice, now=sp.timestamp(159682400), valid=False) # activate only by admin scenario += c1.toggleContractState(True).run(sender=alice, valid=False) scenario += c1.toggleContractState(True).run(sender=admin) # update reward only by admin scenario += c1.updateReward(50).run(sender=alice, valid=False) scenario += c1.updateReward(50).run(sender=admin) # initiate new swap scenario += c1.initiateWait(_hashedSecret=hashSecret, initiator_eth_addr=init_eth, _refundTimestamp=sp.timestamp( 159682500), _amount=5).run(sender=alice, now=sp.timestamp(159682400)) # balance check scenario.verify(c2.data.balances[c1.address].balance == sp.nat(5)) scenario.verify(c2.data.balances[alice.address].balance == sp.nat(13)) # cannot redeem before it is activated & initiated scenario += c1.redeem(_hashedSecret=hashSecret, _secret=sp.bytes( "0x68656c6c6f666473667364666c64736a666c73646a6664736a6673646a6b666a")).run(sender=bob, now=sp.timestamp(159682450), valid=False) # successful add participant only by initiator scenario += c1.addCounterParty(_hashedSecret=hashSecret, _participant=bob.address).run(sender=bob, valid=False) # successful add participant only by initiator scenario += c1.addCounterParty(_hashedSecret=hashSecret, _participant=bob.address).run(sender=alice) # cannot be redeemed with wrong secret scenario += c1.redeem(_hashedSecret=hashSecret, _secret=sp.bytes( "0x12345678aa")).run(sender=bob, now=sp.timestamp(159682450), valid=False) # cannot be redeemed after refundtime has come scenario += c1.redeem(_hashedSecret=hashSecret, _secret=sp.bytes( "0x68656c6c6f666473667364666c64736a666c73646a6664736a6673646a6b666a")).run(sender=bob, now=sp.timestamp(159682550), valid=False) # new swap with the same hash cannot be added unless the previous one is redeemed/refunded scenario += c1.initiateWait(_hashedSecret=hashSecret, initiator_eth_addr=init_eth, _refundTimestamp=sp.timestamp( 159682500), _amount=5).run(sender=alice, amount=sp.tez(2), now=sp.timestamp(159682400), valid=False) # successful redeem can be initiated by anyone but funds transfered to participant scenario += c1.redeem(_hashedSecret=hashSecret, _secret=sp.bytes("0x68656c6c6f666473667364666c64736a666c73646a6664736a6673646a6b666a")).run(sender=bob, now=sp.timestamp(159682450)) # balance check scenario.verify(c2.data.balances[c1.address].balance == sp.nat(0)) scenario.verify(c2.data.balances[bob.address].balance == sp.nat(5)) # successful swap creation with same hash after redeem scenario += c1.initiateWait(_hashedSecret=hashSecret, initiator_eth_addr=init_eth, _refundTimestamp=sp.timestamp( 159682500), _amount=5).run(sender=alice, now=sp.timestamp(159682400)) # balance check scenario.verify(c2.data.balances[c1.address].balance == sp.nat(5)) scenario.verify(c2.data.balances[alice.address].balance == sp.nat(8)) # cannot be refunded before the refundtime scenario += c1.refund(hashSecret).run(sender=bob, now=sp.timestamp(159682450), valid=False) scenario += c1.refund(hashSecret).run(sender=alice, now=sp.timestamp(159682450), valid=False) # can be refunded in any initated or waiting state if refund time has come, can be done by anyone but funds transfered only to initiator scenario += c1.refund(hashSecret).run(sender=bob, now=sp.timestamp(159682550)) # cannot be refunded again once it has been refunded scenario += c1.refund(hashSecret).run(sender=alice, now=sp.timestamp(159682550), valid=False) # balance check scenario.verify(c2.data.balances[c1.address].balance == sp.nat(0)) scenario.verify(c2.data.balances[alice.address].balance == sp.nat(13))
def test(): alice = sp.test_account("Alice") bob = sp.test_account("Bob") init_eth = "0x91f79893E7B923410Ef1aEba6a67c6fab0sfsdgffd" hashSecret = sp.sha256( sp.sha256( sp.bytes( "0x68656c6c6f666473667364666c64736a666c73646a6664736a6673646a6b666a" ))) c1 = AtomicSwap(_admin=bob.address) scenario = sp.test_scenario() scenario.table_of_contents() scenario.h1("Atomic Swap") scenario += c1 scenario.h2("Swap[Wait] Testing") # no operations work without contract being active scenario += c1.initiateWait(_hashedSecret=hashSecret, initiator_eth_addr=init_eth, _refundTimestamp=sp.timestamp(159682500)).run( sender=alice, amount=sp.tez(2), now=sp.timestamp(159682400), valid=False) # activate only by admin scenario += c1.toggleContractState(True).run(sender=alice, valid=False) scenario += c1.toggleContractState(True).run(sender=bob) # initiate new swap scenario += c1.initiateWait(_hashedSecret=hashSecret, initiator_eth_addr=init_eth, _refundTimestamp=sp.timestamp(159682500)).run( sender=alice, amount=sp.tez(2), now=sp.timestamp(159682400)) # balance check scenario.verify(c1.balance == sp.tez(2)) # cannot redeem before it is activated & initiated scenario += c1.redeem( _hashedSecret=hashSecret, _secret=sp.bytes( "0x68656c6c6f666473667364666c64736a666c73646a6664736a6673646a6b666a" )).run(sender=bob, now=sp.timestamp(159682450), valid=False) # successful add participant only by initiator scenario += c1.addCounterParty(_hashedSecret=hashSecret, _participant=bob.address).run(sender=bob, valid=False) # successful add participant only by initiator scenario += c1.addCounterParty(_hashedSecret=hashSecret, _participant=bob.address).run(sender=alice) # cannot be redeemed with wrong secret scenario += c1.redeem(_hashedSecret=hashSecret, _secret=sp.bytes("0x12345678aa")).run( sender=bob, now=sp.timestamp(159682450), valid=False) # cannot be redeemed after refundtime has come scenario += c1.redeem( _hashedSecret=hashSecret, _secret=sp.bytes( "0x68656c6c6f666473667364666c64736a666c73646a6664736a6673646a6b666a" )).run(sender=bob, now=sp.timestamp(159682550), valid=False) # new swap with the same hash cannot be added unless the previous one is redeemed/refunded scenario += c1.initiateWait(_hashedSecret=hashSecret, initiator_eth_addr=init_eth, _refundTimestamp=sp.timestamp(159682500)).run( sender=alice, amount=sp.tez(2), now=sp.timestamp(159682400), valid=False) # successful redeem can be initiated by anyone but funds transfered to participant scenario += c1.redeem( _hashedSecret=hashSecret, _secret=sp.bytes( "0x68656c6c6f666473667364666c64736a666c73646a6664736a6673646a6b666a" )).run(sender=bob, now=sp.timestamp(159682450)) # balance check scenario.verify(c1.balance == sp.tez(0)) # successful swap creation with same hash after redeem scenario += c1.initiateWait(_hashedSecret=hashSecret, initiator_eth_addr=init_eth, _refundTimestamp=sp.timestamp(159682500)).run( sender=alice, amount=sp.tez(2), now=sp.timestamp(159682400)) # balance check scenario.verify(c1.balance == sp.tez(2)) # cannot be refunded before the refundtime scenario += c1.refund(hashSecret).run(sender=bob, now=sp.timestamp(159682450), valid=False) scenario += c1.refund(hashSecret).run(sender=alice, now=sp.timestamp(159682450), valid=False) # can be refunded in any initated or waiting state if refund time has come, can be done by anyone but funds transfered only to initiator scenario += c1.refund(hashSecret).run(sender=bob, now=sp.timestamp(159682550)) # cannot be refunded again once it has been refunded scenario += c1.refund(hashSecret).run(sender=alice, now=sp.timestamp(159682550), valid=False) # balance check scenario.verify(c1.balance == sp.tez(0))
def test(): admin = sp.test_account("Administrator") alice = sp.test_account("Alice") bob = sp.test_account("Bob") init_eth = "0x91f79893E7B923410Ef1aEba6a67c6fab0sfsdgffd" hashSecret = sp.sha256( sp.sha256( sp.bytes( "0x68656c6c6f666473667364666c64736a666c73646a6664736a6673646a6b666a" ))) c2 = FA12.FA12(admin.address) c1 = TokenSwap(_admin=admin.address, _fa12=c2.address) scenario = sp.test_scenario() scenario.table_of_contents() scenario.h1("Atomic Swap") scenario += c1 scenario.h2("Accounts") scenario.show([admin, alice, bob]) scenario.h2("FA1.2") scenario.h3("Entry points") scenario += c2 scenario.h3("Admin mints a few coins") scenario += c2.mint(address=alice.address, value=12).run(sender=admin) scenario += c2.mint(address=alice.address, value=3).run(sender=admin) scenario += c2.mint(address=alice.address, value=3).run(sender=admin) scenario.h2("Alice approves Contract") scenario += c2.approve(spender=c1.address, value=10).run(sender=alice) scenario.h2("Swap[Wait] Testing") # no operations work without contract being active scenario += c1.initiateWait(_hashedSecret=hashSecret, initiator_eth_addr=init_eth, _refundTimestamp=sp.timestamp(159682500), _amount=5).run(sender=alice, now=sp.timestamp(159682400), valid=False) # activate only by admin scenario += c1.toggleContractState(True).run(sender=alice, valid=False) scenario += c1.toggleContractState(True).run(sender=admin) # update reward only by admin scenario += c1.updateReward(50).run(sender=alice, valid=False) scenario += c1.updateReward(50).run(sender=admin) # initiate new swap scenario += c1.initiateWait(_hashedSecret=hashSecret, initiator_eth_addr=init_eth, _refundTimestamp=sp.timestamp(159682500), _amount=5).run(sender=alice, now=sp.timestamp(159682400)) # balance check scenario.verify(c2.data.balances[c1.address].balance == sp.nat(5)) scenario.verify(c2.data.balances[alice.address].balance == sp.nat(13)) # cannot redeem before it is activated & initiated scenario += c1.redeem( _hashedSecret=hashSecret, _secret=sp.bytes( "0x68656c6c6f666473667364666c64736a666c73646a6664736a6673646a6b666a" )).run(sender=bob, now=sp.timestamp(159682450), valid=False) # successful add participant only by initiator scenario += c1.addCounterParty(_hashedSecret=hashSecret, _participant=bob.address).run(sender=bob, valid=False) # successful add participant only by initiator scenario += c1.addCounterParty(_hashedSecret=hashSecret, _participant=bob.address).run(sender=alice) # cannot be redeemed with wrong secret scenario += c1.redeem(_hashedSecret=hashSecret, _secret=sp.bytes("0x12345678aa")).run( sender=bob, now=sp.timestamp(159682450), valid=False) # cannot be redeemed after refundtime has come scenario += c1.redeem( _hashedSecret=hashSecret, _secret=sp.bytes( "0x68656c6c6f666473667364666c64736a666c73646a6664736a6673646a6b666a" )).run(sender=bob, now=sp.timestamp(159682550), valid=False) # new swap with the same hash cannot be added unless the previous one is redeemed/refunded scenario += c1.initiateWait(_hashedSecret=hashSecret, initiator_eth_addr=init_eth, _refundTimestamp=sp.timestamp(159682500), _amount=5).run(sender=alice, amount=sp.tez(2), now=sp.timestamp(159682400), valid=False) # successful redeem can be initiated by anyone but funds transfered to participant scenario += c1.redeem( _hashedSecret=hashSecret, _secret=sp.bytes( "0x68656c6c6f666473667364666c64736a666c73646a6664736a6673646a6b666a" )).run(sender=bob, now=sp.timestamp(159682450)) # balance check scenario.verify(c2.data.balances[c1.address].balance == sp.nat(0)) scenario.verify(c2.data.balances[bob.address].balance == sp.nat(5)) # successful swap creation with same hash after redeem scenario += c1.initiateWait(_hashedSecret=hashSecret, initiator_eth_addr=init_eth, _refundTimestamp=sp.timestamp(159682500), _amount=5).run(sender=alice, now=sp.timestamp(159682400)) # balance check scenario.verify(c2.data.balances[c1.address].balance == sp.nat(5)) scenario.verify(c2.data.balances[alice.address].balance == sp.nat(8)) # cannot be refunded before the refundtime scenario += c1.refund(hashSecret).run(sender=bob, now=sp.timestamp(159682450), valid=False) scenario += c1.refund(hashSecret).run(sender=alice, now=sp.timestamp(159682450), valid=False) # can be refunded in any initated or waiting state if refund time has come, can be done by anyone but funds transfered only to initiator scenario += c1.refund(hashSecret).run(sender=bob, now=sp.timestamp(159682550)) # cannot be refunded again once it has been refunded scenario += c1.refund(hashSecret).run(sender=alice, now=sp.timestamp(159682550), valid=False) # balance check scenario.verify(c2.data.balances[c1.address].balance == sp.nat(0)) scenario.verify(c2.data.balances[alice.address].balance == sp.nat(13))