/
SWAP.masm
81 lines (64 loc) · 2.14 KB
/
SWAP.masm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use.miden::note
use.miden::contracts::wallets::basic->wallet
# CONSTANTS
# =================================================================================================
const.OFFCHAIN_NOTE=2
# ERRORS
# =================================================================================================
# SWAP script expects exactly 9 note inputs
const.ERR_SWAP_WRONG_NUMBER_OF_INPUTS=0x00020007
# SWAP script requires exactly 1 note asset
const.ERR_SWAP_WRONG_NUMBER_OF_ASSETS=0x00020008
# Swap script: adds an asset from the note into consumers account and
# creates a note consumable by note issuer containing requested ASSET.
#
# Requires that the account exposes:
#
# Inputs: [SCRIPT_ROOT]
# Outputs: []
#
# Note inputs are assumed to be as follows:
# - RECIPIENT
# - ASSET
# - TAG = [tag, 0, 0, 0]
#
# FAILS if:
# - Account does not expose miden::contracts::wallets::basic::receive_asset procedure
# - Account does not expose miden::contracts::wallets::basic::send_asset procedure
# - Account vault does not contain the requested asset
# - Adding a fungible asset would result in amount overflow, i.e., the total amount would be
# greater than 2^63
begin
# drop the transaction script root
dropw
# => []
# store asset into memory at address 3
push.3 exec.note::get_assets assert.err=ERR_SWAP_WRONG_NUMBER_OF_ASSETS
# => [ptr]
# load the asset and add it to the account
mem_loadw call.wallet::receive_asset dropw
# => []
# store note inputs into memory starting at address 0
push.0 exec.note::get_inputs
# => [num_inputs, inputs_ptr]
# make sure the number of inputs is 9
eq.9 assert.err=ERR_SWAP_WRONG_NUMBER_OF_INPUTS
# => [inputs_ptr]
# load recipient
drop padw mem_loadw
# => [RECIPIENT]
padw mem_loadw.1
# => [ASSET, RECIPIENT]
padw mem_loadw.2
# => [0, 0, 0, tag, ASSET, RECIPIENT]
drop drop drop movdn.4
# => [ASSET, tag, RECIPIENT]
push.OFFCHAIN_NOTE movdn.5
# => [ASSET, tag, note_type, RECIPIENT]
# create a note using inputs
call.wallet::send_asset
# => [ptr, ZERO, ZERO, 0]
# clean stack
dropw dropw drop drop
# => []
end