22import axios from 'axios' ;
33
44//Lucis imports
5- import { Blockfrost , CML , Lucid , LucidEvolution , TxSigned , walletFromSeed } from "@lucid-evolution/lucid" ;
5+ import { Blockfrost , CML , Lucid , LucidEvolution , TxSigned , walletFromSeed , Credential , valueToAssets , Assets , UTxO , Address , paymentCredentialOf , credentialToAddress , toUnit , Unit } from "@lucid-evolution/lucid" ;
66
77async function loadKey ( ) {
88 const response = await axios . get ( "/blockfrost-key" ,
@@ -85,4 +85,135 @@ export type WalletType = "Lace" | "Eternl" | "Nami" | "Yoroi";
8585export async function selectLucidWallet ( lucid : LucidEvolution , wallet : WalletType ) {
8686 const api = ( await window . cardano [ wallet . toLowerCase ( ) ] . enable ( ) ) ;
8787 lucid . selectWallet . fromAPI ( api ) ;
88+ }
89+
90+ const progLogicBase : Credential = {
91+ type : "Script" ,
92+ hash : "fca77bcce1e5e73c97a0bfa8c90f7cd2faff6fd6ed5b6fec1c04eefa"
93+ }
94+
95+ const stableCoin : Unit = toUnit ( "b34a184f1f2871aa4d33544caecefef5242025f45c3fa5213d7662a9" , "575354" )
96+
97+ export function adjustMintOutput ( tx : CML . Transaction , receiverAddress : Address , mintedAmount : bigint ) {
98+ const txB : CML . TransactionBody = tx . body ( )
99+ const new_outputs = CML . TransactionOutputList . new ( )
100+
101+ const outputs : CML . TransactionOutputList = txB . outputs ( )
102+ const outputsLen = outputs . len ( )
103+ for ( let i = 0 ; i < outputsLen ; i ++ ) {
104+ const output : CML . TransactionOutput = outputs . get ( i )
105+ const address = output . address ( )
106+ const assets : Assets = valueToAssets ( output . amount ( ) )
107+ if ( stableCoin in assets ) {
108+ console . log ( "Found stablecoin in output" )
109+ const stablecoinAmount = assets [ stableCoin ]
110+ if ( stablecoinAmount == mintedAmount ) {
111+ console . log ( "Found minted amount in output" )
112+ const newOutput = CML . TransactionOutput . new ( CML . Address . from_bech32 ( receiverAddress ) , output . amount ( ) , output . datum ( ) , output . script_ref ( ) )
113+ new_outputs . add ( newOutput )
114+ //new(address: Address, amount: Value, datum_option?: DatumOption, script_reference?: Script): TransactionOutput;
115+ } else {
116+ new_outputs . add ( output )
117+ }
118+ } else {
119+ new_outputs . add ( output )
120+ }
121+ }
122+ const newTxB : CML . TransactionBody = CML . TransactionBody . new ( txB . inputs ( ) , new_outputs , txB . fee ( ) ) ;
123+ const oldTxAuxHash = txB . auxiliary_data_hash ( )
124+ if ( oldTxAuxHash ) {
125+ newTxB . set_auxiliary_data_hash ( oldTxAuxHash )
126+ }
127+ const oldWithdrawals = txB . withdrawals ( )
128+ if ( oldWithdrawals ) {
129+ newTxB . set_withdrawals ( oldWithdrawals )
130+ }
131+ const oldTTL = txB . ttl ( )
132+ if ( oldTTL ) {
133+ newTxB . set_ttl ( oldTTL )
134+ }
135+ const oldCerts = txB . certs ( )
136+ if ( oldCerts ) {
137+ newTxB . set_certs ( oldCerts )
138+ }
139+
140+ const oldValidityStart = txB . validity_interval_start ( )
141+ if ( oldValidityStart ) {
142+ newTxB . set_validity_interval_start ( oldValidityStart )
143+ }
144+
145+ const oldMint = txB . mint ( )
146+ if ( oldMint ) {
147+ newTxB . set_mint ( oldMint )
148+ }
149+
150+ const oldCollateral = txB . collateral_inputs ( )
151+ if ( oldCollateral ) {
152+ newTxB . set_collateral_inputs ( oldCollateral )
153+ }
154+
155+ const oldRequiredSigners = txB . required_signers ( )
156+ if ( oldRequiredSigners ) {
157+ newTxB . set_required_signers ( oldRequiredSigners )
158+ }
159+
160+ const oldNetworkId = txB . network_id ( )
161+ if ( oldNetworkId ) {
162+ newTxB . set_network_id ( oldNetworkId )
163+ }
164+
165+ const oldCollateralReturn = txB . collateral_return ( )
166+ if ( oldCollateralReturn ) {
167+ newTxB . set_collateral_return ( oldCollateralReturn )
168+ }
169+
170+ const oldTotalCollateral = txB . total_collateral ( )
171+ if ( oldTotalCollateral ) {
172+ newTxB . set_total_collateral ( oldTotalCollateral )
173+ }
174+
175+ const oldReferenceInputs = txB . reference_inputs ( ) ;
176+ if ( oldReferenceInputs ) {
177+ newTxB . set_reference_inputs ( oldReferenceInputs )
178+ }
179+
180+ const oldTreasuryValue = txB . current_treasury_value ( )
181+ if ( oldTreasuryValue ) {
182+ newTxB . set_current_treasury_value ( oldTreasuryValue )
183+ }
184+
185+ console . log ( "New outputs length: " , new_outputs . len ( ) )
186+ // const oldAuxiliaryData = tx.auxiliary_data()
187+ // if(oldAuxiliaryData){
188+
189+ // }
190+ return CML . Transaction . new ( newTxB , tx . witness_set ( ) , true , tx . auxiliary_data ( ) )
191+
192+ }
193+
194+ export async function getStablecoinAccounts ( lucid : LucidEvolution ) {
195+ const progUTxOs : UTxO [ ] = await lucid . utxosAtWithUnit ( progLogicBase , stableCoin ) ;
196+ const addresses = new Set < string > ( ) ;
197+ const valueMap = new Map < Address , number > ( ) ;
198+ progUTxOs . forEach ( utxo => {
199+ addresses . add ( utxo . address )
200+ valueMap . set ( utxo . address , Number ( utxo . assets [ stableCoin ] ) )
201+ } ) ;
202+ return { addresses : Array . from ( addresses ) , valueMap } ;
203+ }
204+
205+ export async function deriveProgrammableAddress ( lucid : LucidEvolution , userAddress : Address ) {
206+ const network = lucid . config ( ) . network ! ;
207+ // user's payment credential
208+ const ownerCred : Credential = paymentCredentialOf ( userAddress ) ;
209+
210+ // construct the user's programmable token address
211+ // payment credential is always the programmable token base script hash
212+ // staking credential is the user's payment credential
213+ const userProgrammableTokenAddress = credentialToAddress (
214+ network ,
215+ progLogicBase ,
216+ ownerCred ,
217+ ) ;
218+ return userProgrammableTokenAddress ;
88219}
0 commit comments