@@ -10,6 +10,11 @@ import { newProgressBar } from "./util.js";
1010const argitRemoteURIRegex = '^gitopia:\/\/([a-zA-Z0-9-_]{43})\/([A-Za-z0-9_.-]*)'
1111const contractId = "1ljLAR55OhtenU0iDWkLGT6jF4ApxeQd5P0gXNyNJXg" ;
1212
13+ const sleep = async ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
14+
15+ const getStatus = async ( arweave , txid ) =>
16+ ( await arweave . transactions . getStatus ( txid ) ) . status ;
17+
1318export function parseArgitRemoteURI ( remoteURI ) {
1419 const matchGroups = remoteURI . match ( argitRemoteURIRegex ) ;
1520 const repoOwnerAddress = matchGroups [ 1 ] ;
@@ -24,7 +29,7 @@ export async function makeUpdateRefTx(
2429 remoteURI ,
2530 ref ,
2631 oid ,
27- bundledDataTxs
32+ bundledDataTxInfo
2833) {
2934 const { repoName } = parseArgitRemoteURI ( remoteURI ) ;
3035 const numCommits = shell
@@ -54,11 +59,9 @@ export async function makeUpdateRefTx(
5459 tx . addTag ( "Origin" , "git-remote-gitopia" ) ;
5560 }
5661
57- const bundledDataTxIds = [ ] ;
58- for ( let i = 0 ; i < bundledDataTxs . length ; i ++ ) {
59- bundledDataTxIds . push ( bundledDataTxs [ i ] . id ) ;
60- }
61-
62+ const bundledDataTxIds = bundledDataTxInfo . map (
63+ ( bundledDataTx ) => bundledDataTx . id
64+ ) ;
6265 tx . addTag ( "Reference-Txs" , JSON . stringify ( bundledDataTxIds ) ) ;
6366
6467 await arweave . transactions . sign ( tx , wallet ) ;
@@ -90,15 +93,8 @@ export const makeDataItem = async (
9093 return await arData . sign ( item , wallet ) ;
9194} ;
9295
93- export const makeBundledDataTx = async (
94- arweave ,
95- arData ,
96- wallet ,
97- remoteURI ,
98- dataItems
99- ) => {
96+ export const makeBundledDataTx = async ( arweave , wallet , remoteURI , bundle ) => {
10097 const { repoName } = parseArgitRemoteURI ( remoteURI ) ;
101- const bundle = await arData . bundleData ( dataItems ) ;
10298 const data = JSON . stringify ( bundle ) ;
10399 const tx = await arweave . createTransaction ( { data } , wallet ) ;
104100 tx . addTag ( "Repo" , repoName ) ;
@@ -128,11 +124,53 @@ export const postTransaction = async (arweave, tx) => {
128124 bar . stop ( ) ;
129125} ;
130126
127+ export const waitTxPropogation = async ( arweave , tx ) => {
128+ let status = await getStatus ( arweave , tx . id ) ;
129+
130+ let wait = 6 ;
131+ while ( status === 404 && wait -- ) {
132+ await sleep ( 5000 ) ;
133+ try {
134+ status = await getStatus ( arweave , tx . id ) ;
135+ } catch ( err ) {
136+ wait ++ ;
137+ status = 404 ;
138+ }
139+ }
140+
141+ if ( status === 400 || status === 404 || status === 410 ) {
142+ return status ;
143+ }
144+
145+ if ( status === 202 ) {
146+ return 202 ;
147+ }
148+
149+ // we'll give it 2 minutes for propogation
150+ if ( status === 404 ) {
151+ let tries = 3 ;
152+ do {
153+ await sleep ( 40000 ) ; //40 secs
154+ try {
155+ status = await getStatus ( arweave , tx . id ) ;
156+ } catch ( err ) {
157+ tries ++ ;
158+ status = 404 ;
159+ }
160+ if ( status === 200 ) {
161+ return 200 ;
162+ }
163+ } while ( -- tries ) ;
164+ }
165+
166+ return 404 ;
167+ } ;
168+
131169export const sendPSTFee = async (
132170 arweave ,
133171 wallet ,
134172 remoteURI ,
135- transactions ,
173+ transactionsInfo ,
136174 referenceId
137175) => {
138176 const { repoName } = parseArgitRemoteURI ( remoteURI ) ;
@@ -146,8 +184,8 @@ export const sendPSTFee = async (
146184
147185 // PST Fee
148186 let totalTxFee = new BigNumber ( 0 ) ;
149- for ( let i = 0 ; i < transactions . length ; i ++ ) {
150- const txFee = new BigNumber ( transactions [ i ] . reward ) ;
187+ for ( let i = 0 ; i < transactionsInfo . length ; i ++ ) {
188+ const txFee = new BigNumber ( transactionsInfo [ i ] . reward ) ;
151189 totalTxFee = totalTxFee . plus ( txFee ) ;
152190 }
153191
@@ -159,16 +197,22 @@ export const sendPSTFee = async (
159197 ? pstFee . toFixed ( 0 )
160198 : arweave . ar . arToWinston ( "0.01" ) ;
161199
162- const pstTx = await arweave . createTransaction (
163- { target : holder , quantity } ,
164- wallet
165- ) ;
166- pstTx . addTag ( "Reference-Id" , referenceId ) ;
167- pstTx . addTag ( "Repo" , repoName ) ;
168- pstTx . addTag ( "Version" , "0.0.2" ) ;
169- pstTx . addTag ( "App-Name" , "Gitopia" ) ;
170- pstTx . addTag ( "Unix-Time" , Math . round ( new Date ( ) . getTime ( ) / 1000 ) . toString ( ) ) ;
171-
172- await arweave . transactions . sign ( pstTx , wallet ) ;
173- await arweave . transactions . post ( pstTx ) ;
200+ let pstTx = null ;
201+ do {
202+ pstTx = await arweave . createTransaction (
203+ { target : holder , quantity } ,
204+ wallet
205+ ) ;
206+ pstTx . addTag ( "Reference-Id" , referenceId ) ;
207+ pstTx . addTag ( "Repo" , repoName ) ;
208+ pstTx . addTag ( "Version" , "0.0.2" ) ;
209+ pstTx . addTag ( "App-Name" , "Gitopia" ) ;
210+ pstTx . addTag (
211+ "Unix-Time" ,
212+ Math . round ( new Date ( ) . getTime ( ) / 1000 ) . toString ( )
213+ ) ;
214+
215+ await arweave . transactions . sign ( pstTx , wallet ) ;
216+ await arweave . transactions . post ( pstTx ) ;
217+ } while ( ( await waitTxPropogation ( arweave , pstTx ) ) !== 202 ) ;
174218} ;
0 commit comments