@@ -11,6 +11,7 @@ import {
11
11
ACCOUNT_IMPLEMENTATION ,
12
12
DEPLOYER ,
13
13
asUAddress ,
14
+ PLACEHOLDER_ACCOUNT_ADDRESS ,
14
15
} from 'lib' ;
15
16
import { ShapeFunc } from '~/core/database' ;
16
17
import {
@@ -30,6 +31,7 @@ import { AccountsCacheService } from '../auth/accounts.cache.service';
30
31
import { v4 as uuid } from 'uuid' ;
31
32
import { selectAccount2 } from './accounts.util' ;
32
33
import { AccountEvent } from './accounts.model' ;
34
+ import { PolicyInput } from '../policies/policies.input' ;
33
35
34
36
const accountTrigger = ( account : UAddress ) => `account.updated:${ account } ` ;
35
37
const accountApproverTrigger = ( approver : Address ) => `account.updated:approver:${ approver } ` ;
@@ -105,7 +107,7 @@ export class AccountsService {
105
107
throw new UserInputError ( 'Duplicate policy keys' ) ;
106
108
107
109
const implementation = ACCOUNT_IMPLEMENTATION . address [ chain ] ;
108
- const account = asUAddress (
110
+ const address = asUAddress (
109
111
getProxyAddress ( {
110
112
deployer : DEPLOYER . address [ chain ] ,
111
113
implementation,
@@ -119,35 +121,51 @@ export class AccountsService {
119
121
const id = uuid ( ) ;
120
122
await this . accountsCache . addCachedAccount ( {
121
123
approver : getApprover ( ) ,
122
- account : { id, address : account } ,
124
+ account : { id, address } ,
123
125
} ) ;
124
126
127
+ // Replace self address with account address
128
+ const selfRefPolicies = policies . map (
129
+ ( p ) =>
130
+ ( {
131
+ ...p ,
132
+ actions : p . actions ?. map ( ( a ) => ( {
133
+ ...a ,
134
+ functions : a . functions . map ( ( f ) => ( {
135
+ ...f ,
136
+ contract :
137
+ f . contract === PLACEHOLDER_ACCOUNT_ADDRESS ? asAddress ( address ) : f . contract ,
138
+ } ) ) ,
139
+ } ) ) ,
140
+ } ) satisfies PolicyInput ,
141
+ ) ;
142
+
125
143
await this . db . transaction ( async ( ) => {
126
144
await this . db . query (
127
145
e . insert ( e . Account , {
128
146
id,
129
- address : account ,
147
+ address,
130
148
name,
131
149
implementation,
132
150
salt,
133
151
} ) ,
134
152
) ;
135
153
136
- for ( const policy of policyInputs ) {
137
- await this . policies . create ( {
138
- account,
139
- initState : true ,
140
- ... policy ,
141
- } ) ;
142
- }
154
+ await this . policies . propose (
155
+ {
156
+ account : address ,
157
+ isInitialization : true ,
158
+ } ,
159
+ ... selfRefPolicies ,
160
+ ) ;
143
161
} ) ;
144
162
145
- this . contracts . addAccountAsVerified ( asAddress ( account ) ) ;
146
- this . faucet . requestTokens ( account ) ;
147
- this . event ( { account, event : AccountEvent . created } ) ;
163
+ this . contracts . addAccountAsVerified ( asAddress ( address ) ) ;
164
+ this . faucet . requestTokens ( address ) ;
165
+ this . event ( { account : address , event : AccountEvent . created } ) ;
148
166
this . setAsPrimaryAccountIfNotConfigured ( id ) ;
149
167
150
- return { id, address : account } ;
168
+ return { id, address : address } ;
151
169
}
152
170
153
171
async updateAccount ( { account, name, photo } : UpdateAccountInput ) {
0 commit comments