@@ -14,11 +14,12 @@ import {
1414import { signWithLitActionCode , executeLitAction } from './litActions' ;
1515import { State , StateParams } from './states' ;
1616import { Check , Transition } from './transitions' ;
17- import { getChain } from './utils/chain' ;
17+ import { getEvmChain } from './utils/chain' ;
1818import { getBalanceTransitionCheck , getERC20Balance } from './utils/erc20' ;
1919
2020import {
2121 BaseStateMachineParams ,
22+ ContextOrLiteral ,
2223 PKPInfo ,
2324 StateMachineDefinition ,
2425 TransitionDefinition ,
@@ -135,15 +136,15 @@ export class StateMachine {
135136 onEnterFunctions . push ( async ( ) => {
136137 console . log (
137138 `MachineContext at state ${ state . key } enter: ` ,
138- stateMachine . context . get ( contextAction . log ?. path ) ,
139+ stateMachine . context . get ( contextAction . log ?. path )
139140 ) ;
140141 } ) ;
141142 }
142143 if ( contextAction . log ?. atExit ) {
143144 onExitFunctions . push ( async ( ) => {
144145 console . log (
145146 `MachineContext at state ${ state . key } exit: ` ,
146- stateMachine . context . get ( contextAction . log ?. path ) ,
147+ stateMachine . context . get ( contextAction . log ?. path )
147148 ) ;
148149 } ) ;
149150 }
@@ -162,8 +163,8 @@ export class StateMachine {
162163 litNodeClient : litNodeClientInstance ,
163164 pkpPublicKey : stateMachine . pkp ! . publicKey ,
164165 authSigner : signer ,
165- ipfsId : litAction . ipfsId ,
166- code : litAction . code ,
166+ ipfsId : stateMachine . resolveValue ( litAction . ipfsId ) ,
167+ code : stateMachine . resolveValue ( litAction . code ) ,
167168 jsParams : litAction . jsParams ,
168169 } ) ;
169170
@@ -175,36 +176,38 @@ export class StateMachine {
175176 if ( transaction ) {
176177 onEnterFunctions . push ( async ( ) => {
177178 const yellowstoneMachineSigner = new ethers . Wallet (
178- transaction . pkpOwnerKey ,
179+ stateMachine . resolveValue ( transaction . pkpOwnerKey ) ,
179180 new ethers . providers . JsonRpcProvider ( LIT_RPC . CHRONICLE_YELLOWSTONE )
180181 ) ;
181182
182- const chain = getChain ( transaction ) ;
183+ const chainId = stateMachine . resolveValue ( transaction . evmChainId ) ;
184+ const chain = getEvmChain ( chainId ) ;
183185 const chainProvider = new ethers . providers . JsonRpcProvider (
184186 chain . rpcUrls [ 0 ] ,
185187 chain . chainId
186188 ) ;
187189
188190 const contract = new ethers . Contract (
189- transaction . contractAddress ,
191+ stateMachine . resolveValue ( transaction . contractAddress ) ,
190192 transaction . contractABI ,
191193 chainProvider
192194 ) ;
193195
194- const txParams = ( transaction . params || [ ] ) . map ( param =>
195- 'contextPath' in param
196- ? stateMachine . context . get ( param . contextPath )
197- : param
196+ const txParams = ( transaction . params || [ ] ) . map (
197+ stateMachine . resolveValue . bind ( stateMachine )
198+ ) ;
199+ const txMethod = stateMachine . resolveValue ( transaction . method ) ;
200+ const txData = await contract . populateTransaction [ txMethod ] (
201+ ...txParams
198202 ) ;
199- const txData = await contract . populateTransaction [ transaction . method ] ( ...txParams ) ;
200203 const gasLimit = await chainProvider . estimateGas ( {
201- to : transaction . contractAddress ,
204+ to : stateMachine . resolveValue ( transaction . contractAddress ) ,
202205 data : txData . data ,
203- from : transaction . pkpEthAddress ,
206+ from : stateMachine . resolveValue ( transaction . pkpEthAddress ) ,
204207 } ) ;
205208 const gasPrice = await chainProvider . getGasPrice ( ) ;
206209 const nonce = await chainProvider . getTransactionCount (
207- transaction . pkpEthAddress
210+ stateMachine . resolveValue ( transaction . pkpEthAddress )
208211 ) ;
209212
210213 const rawTx = {
@@ -213,7 +216,7 @@ export class StateMachine {
213216 gasLimit : gasLimit . toHexString ( ) ,
214217 gasPrice : gasPrice . toHexString ( ) ,
215218 nonce,
216- to : transaction . contractAddress ,
219+ to : stateMachine . resolveValue ( transaction . contractAddress ) ,
217220 } ;
218221 const rawTxHash = ethers . utils . keccak256 (
219222 ethers . utils . serializeTransaction ( rawTx )
@@ -222,7 +225,7 @@ export class StateMachine {
222225 // Sign with the PKP in a LitAction
223226 const litActionResponse = await executeLitAction ( {
224227 litNodeClient : litNodeClientInstance ,
225- pkpPublicKey : transaction . pkpPublicKey ,
228+ pkpPublicKey : stateMachine . resolveValue ( transaction . pkpPublicKey ) ,
226229 authSigner : yellowstoneMachineSigner ,
227230 code : signWithLitActionCode ,
228231 jsParams : {
@@ -282,7 +285,8 @@ export class StateMachine {
282285
283286 if ( evmContractEvent ) {
284287 const transitionIndex = checks . length ;
285- const chain = getChain ( evmContractEvent ) ;
288+ const chainId = stateMachine . resolveValue ( evmContractEvent . evmChainId ) ;
289+ const chain = getEvmChain ( chainId ) ;
286290
287291 listeners . push (
288292 new EVMContractEventListener (
@@ -302,12 +306,12 @@ export class StateMachine {
302306 | ContractEventData
303307 | undefined ;
304308
305- evmContractEvent . contextUpdates ?. forEach ( contextUpdate =>
309+ evmContractEvent . contextUpdates ?. forEach ( ( contextUpdate ) =>
306310 stateMachine . context . setFromData (
307311 contextUpdate . contextPath ,
308312 eventData ,
309- contextUpdate . dataPath ,
310- ) ,
313+ contextUpdate . dataPath
314+ )
311315 ) ;
312316
313317 return eventData ?. event . event === evmContractEvent . eventName ;
@@ -317,7 +321,8 @@ export class StateMachine {
317321 if ( balances ) {
318322 balances . forEach ( ( balance ) => {
319323 const transitionIndex = checks . length ;
320- const chain = getChain ( balance ) ;
324+ const chainId = stateMachine . resolveValue ( balance . evmChainId ) ;
325+ const chain = getEvmChain ( chainId ) ;
321326
322327 const chainProvider = new ethers . providers . JsonRpcProvider (
323328 chain . rpcUrls [ 0 ] ,
@@ -471,6 +476,13 @@ export class StateMachine {
471476 this . debug && console . log ( 'State machine stopped' ) ;
472477 }
473478
479+ public resolveValue < T = unknown > ( value : ContextOrLiteral < T > | T ) : T {
480+ if ( value && typeof value === 'object' && 'contextPath' in value ) {
481+ return this . context . get ( value . contextPath ) as T ;
482+ }
483+ return value ;
484+ }
485+
474486 /**
475487 * Validates whether a PKP (Private Key Pair) is configured in the state machine.
476488 * If a PKP is not present, it initiates the minting of a new PKP through the
0 commit comments