@@ -78,7 +78,6 @@ export const create_campaign = ({ args }: CreateCampaignParams) => {
7878
7979 return contractApi . callMultiple ( transactions ) ;
8080 } else {
81- console . log ( "create campaign" ) ;
8281 return contractApi . call < CreateCampaignParams [ "args" ] , Campaign > ( "create_campaign" , {
8382 args,
8483 deposit : floatToYoctoNear ( 0.021 ) ,
@@ -117,13 +116,62 @@ export const delete_campaign = ({ args }: DeleteCampaignParams) =>
117116 gas : FULL_TGAS ,
118117 } ) ;
119118
120- export const donate = ( args : CampaignDonationArgs , depositAmountYocto : IndivisibleUnits ) =>
121- contractApi . call < CampaignDonationArgs , CampaignDonation > ( "donate" , {
119+ export type DonateResult = {
120+ donation : CampaignDonation ;
121+ txHash : string | null ;
122+ } ;
123+
124+ export const donate = async (
125+ args : CampaignDonationArgs ,
126+ depositAmountYocto : IndivisibleUnits ,
127+ ) : Promise < DonateResult > => {
128+ const { walletApi } = await import ( "@/common/blockchains/near-protocol/client" ) ;
129+ const wallet = await walletApi . ensureWallet ( ) ;
130+ const signerId = walletApi . accountId ;
131+
132+ if ( ! signerId ) {
133+ throw new Error ( "Wallet is not signed in." ) ;
134+ }
135+
136+ const { actionCreators } = await import ( "@near-js/transactions" ) ;
137+ const { providers } = await import ( "near-api-js" ) ;
138+
139+ const action = actionCreators . functionCall (
140+ "donate" ,
122141 args ,
123- deposit : depositAmountYocto ,
124- gas : FULL_TGAS ,
125- callbackUrl : window . location . href ,
126- } ) ;
142+ BigInt ( FULL_TGAS ) ,
143+ BigInt ( depositAmountYocto ) ,
144+ ) ;
145+
146+ let outcome : any ;
147+ const walletAny = wallet as any ;
148+
149+ if ( "signAndSendTransaction" in walletAny ) {
150+ outcome = await walletAny . signAndSendTransaction ( {
151+ signerId,
152+ receiverId : CAMPAIGNS_CONTRACT_ACCOUNT_ID ,
153+ actions : [ action ] ,
154+ } ) ;
155+ } else if ( "signAndSendTransactions" in walletAny ) {
156+ const results = await walletAny . signAndSendTransactions ( {
157+ transactions : [
158+ {
159+ receiverId : CAMPAIGNS_CONTRACT_ACCOUNT_ID ,
160+ actions : [ action ] ,
161+ } ,
162+ ] ,
163+ } ) ;
164+
165+ outcome = Array . isArray ( results ) ? results [ 0 ] : results ;
166+ } else {
167+ throw new Error ( "Wallet does not support transaction signing" ) ;
168+ }
169+
170+ const txHash = outcome ?. transaction ?. hash || outcome ?. transaction_outcome ?. id || null ;
171+ const donation = providers . getTransactionLastResult ( outcome ) as CampaignDonation ;
172+
173+ return { donation, txHash } ;
174+ } ;
127175
128176export const get_campaigns = ( ) => contractApi . view < { } , Campaign [ ] > ( "get_campaigns" ) ;
129177
0 commit comments