@@ -12,99 +12,94 @@ function checkOrder(a0, a1, b0, b1) {
12
12
throw new TypeError ( 'Canot resove asset ordering' ) ;
13
13
}
14
14
15
- export async function makeExchange ( E , log , host , zoe , registrar ) {
16
- // === API
17
-
18
- async function getPrice ( instanceId , extent0 , assayId0 , assayId1 ) {
19
- const instanceHandle = await E ( registrar ) . get ( instanceId ) ;
20
-
21
- // Find the assays in the registrar
22
- const registrarAssays = await Promise . all ( [
23
- E ( registrar ) . get ( assayId0 ) ,
24
- E ( registrar ) . get ( assayId1 ) ,
25
- ] ) ;
26
-
27
- // Get the assays in the contract.
28
- // Get the contract instance.
29
- const {
30
- terms : { assays : contractAssays } ,
31
- instance,
32
- } = await E ( zoe ) . getInstance ( instanceHandle ) ;
33
-
34
- // Check whether we sell on contract assay 0 or 1.
35
- const normal = checkOrder (
36
- registrarAssays [ 0 ] ,
37
- registrarAssays [ 1 ] ,
38
- contractAssays [ 0 ] ,
39
- contractAssays [ 1 ] ,
15
+ export function makeExchange ( E , log , host , zoe , registrar ) {
16
+
17
+ function getPrice ( instanceId , extent0 , assayId0 , assayId1 ) {
18
+ const instanceHandleP = E ( registrar ) . get ( instanceId ) ;
19
+ const regAssay0P = E ( registrar ) . get ( assayId0 ) ;
20
+ const regAssay1P = E ( registrar ) . get ( assayId1 ) ;
21
+ const unit0P = E ( regAssay0P ) . makeUnits ( extent0 ) ;
22
+
23
+ return Promise . all ( [ instanceHandleP , regAssay0P , regAssay1P , unit0P ] ) . then (
24
+ ( [ instanceHandle , regAssay0 , regAssay1 , unit0 ] ) =>
25
+ E ( zoe )
26
+ . getInstance ( instanceHandle )
27
+ . then (
28
+ ( {
29
+ terms : {
30
+ assays : [ contractAssay0 , contractAssay1 ] ,
31
+ } ,
32
+ instance,
33
+ } ) => {
34
+ // Check whether we sell on contract assay 0 or 1.
35
+ const normal = checkOrder (
36
+ regAssay0 ,
37
+ regAssay1 ,
38
+ contractAssay0 ,
39
+ contractAssay1 ,
40
+ ) ;
41
+ // Order the units accordingly.
42
+ const units = [
43
+ normal ? unit0 : undefined ,
44
+ normal ? undefined : unit0 ,
45
+ undefined ,
46
+ ] ;
47
+ return E ( instance )
48
+ . getPrice ( units )
49
+ . then ( unit1 => unit1 . extent ) ;
50
+ } ,
51
+ ) ,
40
52
) ;
41
-
42
- // Units of the input amount.
43
- const unit0 = await E ( registrarAssays [ 0 ] ) . makeUnits ( extent0 ) ;
44
-
45
- // Order the units accordingly.
46
- const units = [
47
- normal ? unit0 : undefined ,
48
- normal ? undefined : unit0 ,
49
- undefined ,
50
- ] ;
51
-
52
- // Extract the price (multi steps for debugging).
53
- const unit1 = await E ( instance ) . getPrice ( units ) ;
54
- const { extent } = unit1 ;
55
- return extent ;
56
53
}
57
54
58
- async function getOfferRules ( instanceId , extent , assayId0 , assayId1 ) {
59
- const instanceHandle = await E ( registrar ) . get ( instanceId ) ;
60
-
61
- // Find the assays by id in the registrar.
62
- const registrarAssays = await Promise . all ( [
63
- E ( registrar ) . get ( assayId0 ) ,
64
- E ( registrar ) . get ( assayId1 ) ,
65
- ] ) ;
66
-
67
- // Get the assays in the contract.
68
- const {
69
- terms : { assays : contractAssays } ,
70
- } = await E ( zoe ) . getInstance ( instanceHandle ) ;
71
-
72
- // Check whether we sell on contract assay 0 or 1.
73
- const normal = checkOrder (
74
- registrarAssays [ 0 ] ,
75
- registrarAssays [ 1 ] ,
76
- contractAssays [ 0 ] ,
77
- contractAssays [ 1 ] ,
55
+ function getOfferRules ( instanceId , extent0 , assayId0 , assayId1 ) {
56
+ const instanceHandleP = E ( registrar ) . get ( instanceId ) ;
57
+ const regAssay0P = E ( registrar ) . get ( assayId0 ) ;
58
+ const regAssay1P = E ( registrar ) . get ( assayId1 ) ;
59
+
60
+ return Promise . all ( [ instanceHandleP , regAssay0P , regAssay1P ] ) . then (
61
+ ( [ instanceHandle , regAssay0 , regAssay1 ] ) =>
62
+ E ( zoe )
63
+ . getInstance ( instanceHandle )
64
+ . then ( ( { terms : { assays : [ contractAssay0 , contractAssay1 ] } } ) => {
65
+ // Check whether we sell on contract assay 0 or 1.
66
+ const normal = checkOrder (
67
+ regAssay0 ,
68
+ regAssay1 ,
69
+ contractAssay0 ,
70
+ contractAssay1 ,
71
+ ) ;
72
+
73
+ // Contrust the rules for serialization (no instance).
74
+ // This rule is the payment
75
+ const rule0 = {
76
+ kind : 'offerExactly' ,
77
+ units : { assayId : assayId0 , extent : extent0 } ,
78
+ } ;
79
+ // This rule is the payout
80
+ const rule1 = {
81
+ kind : 'wantAtLeast' ,
82
+ units : { assayId : assayId1 } ,
83
+ } ;
84
+
85
+ // Order the rules accordingly.
86
+ const offerRules = harden ( {
87
+ payoutRules : [
88
+ normal ? rule0 : rule1 ,
89
+ normal ? rule1 : rule0 ,
90
+ {
91
+ kind : 'wantAtLeast' ,
92
+ units : { } ,
93
+ } ,
94
+ ] ,
95
+ exitRule : {
96
+ kind : 'onDemand' ,
97
+ } ,
98
+ } ) ;
99
+
100
+ return offerRules ;
101
+ } ) ,
78
102
) ;
79
-
80
- // Contrust the rules for serialization (no instance).
81
- // This rule is the payment
82
- const rule0 = {
83
- kind : 'offerExactly' ,
84
- units : { assayId : assayId0 , extent } ,
85
- } ;
86
- // This rule is the payout
87
- const rule1 = {
88
- kind : 'wantAtLeast' ,
89
- units : { assayId : assayId1 } ,
90
- } ;
91
-
92
- // Order the rules accordingly.
93
- const offerRules = harden ( {
94
- payoutRules : [
95
- normal ? rule0 : rule1 ,
96
- normal ? rule1 : rule0 ,
97
- {
98
- kind : 'wantAtLeast' ,
99
- units : { } ,
100
- } ,
101
- ] ,
102
- exitRule : {
103
- kind : 'onDemand' ,
104
- } ,
105
- } ) ;
106
-
107
- return offerRules ;
108
103
}
109
104
110
105
const autoswap = harden ( {
0 commit comments