-
Notifications
You must be signed in to change notification settings - Fork 2
/
forwarder.oscript
68 lines (67 loc) · 1.76 KB
/
forwarder.oscript
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
/*
Double forwarder:
- forward data1 to address1
- receive some coins in response (single-asset responses only)
- forward the coins, plus data2, to the final recipient address2 (which can be another AA)
*/
{
init: `{
$non_bytes_asset = trigger.output[[asset!=base]].asset;
if ($non_bytes_asset == 'ambiguous')
bounce('ambiguous asset');
$asset = $non_bytes_asset == 'none' ? 'base' : $non_bytes_asset;
$claim_num = trigger.data.claim_num;
$step1 = !!$claim_num; // from the bridge
if ($step1){
$data = trigger.data.data;
$address = $data.address1;
$data_to_forward = $data.data1;
}
else if (var['initial_unit'] AND var['initial_unit'] == trigger.initial_unit AND var['address2']){ // response from the AA called in step 1
$step2 = true;
$address = var['address2'];
$data_to_forward = var['data2'];
}
}`,
messages: [
{
if: `{$step1 OR $step2}`,
app: 'payment',
payload: {
asset: '{$asset}',
outputs: [{address: `{$address}`, amount: `{trigger.output[[asset=$asset]]}`}]
}
},
{ // send additional 1000 bytes
if: `{($step1 OR $step2) AND $asset != 'base'}`,
app: 'payment',
payload: {
asset: 'base',
outputs: [{address: `{$address}`, amount: 1000}]
}
},
{
if: `{$data_to_forward}`,
app: 'data',
payload: `{
$data_to_forward
}`
},
{
app: 'state',
state: `{
if ($step1 AND $data.address2){ // save step2 data for future forwarding
var['initial_unit'] = trigger.initial_unit;
var['address2'] = $data.address2;
var['data2'] = $data.data2;
}
else if ($step2){
var['initial_unit'] = false;
var['address2'] = false;
var['data2'] = false;
}
response['message'] = $step1 ? 'forwarded 1' : ($step2 ? 'forwarded 2' : 'deposited');
}`
}
]
}