wan-ton-sdk 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/code/userLock.ts CHANGED
@@ -211,7 +211,7 @@ async function buildLockWrappedTokenMessages(opts:any,jwAddrBridgeSc:Address,jwA
211
211
  return ret;
212
212
  }
213
213
 
214
- async function getFee(client:TonClient|Blockchain,bridgeScAddr:Address,tokenPairID:number,srcChainId,dstChainId){
214
+ export async function getFee(client:TonClient|Blockchain,bridgeScAddr:Address,tokenPairID:number,srcChainId,dstChainId){
215
215
  let fee = 0;
216
216
  if(client instanceof TonClient){
217
217
  let ba = BridgeAccess.create(client,bridgeScAddr.toString())
@@ -234,7 +234,7 @@ async function getFee(client:TonClient|Blockchain,bridgeScAddr:Address,tokenPair
234
234
  return fee;
235
235
  }
236
236
 
237
- async function getJettonWalletAddr(client:TonClient|Blockchain,jettonMasterAddr:Address,ownerAddr:Address){
237
+ export async function getJettonWalletAddr(client:TonClient|Blockchain,jettonMasterAddr:Address,ownerAddr:Address){
238
238
  console.log("in getJettonWalletAddr", "jettonMasterAddr", jettonMasterAddr.toString(),"ownerAddr",ownerAddr.toString());
239
239
  if(client instanceof TonClient){
240
240
  let jettonMasterSc = JettonMaster.create(jettonMasterAddr)
@@ -248,7 +248,7 @@ async function getJettonWalletAddr(client:TonClient|Blockchain,jettonMasterAddr:
248
248
 
249
249
  }
250
250
 
251
- async function getJettonAdminAddr(client:TonClient|Blockchain,jettonMasterAddr:Address){
251
+ export async function getJettonAdminAddr(client:TonClient|Blockchain,jettonMasterAddr:Address){
252
252
  console.log("in getJettonAdminAddr", "jettonMasterAddr", jettonMasterAddr.toString());
253
253
  if(client instanceof TonClient){
254
254
  let jettonMasterSc = JettonMaster.create(jettonMasterAddr)
package/db/DbAccess.ts CHANGED
@@ -160,6 +160,18 @@ export class DBAccess {
160
160
  }
161
161
  }
162
162
 
163
+ async getTxByOnlyMsgHash(dbName:string,msgCellHash:string){
164
+ if(!this.has(dbName)){
165
+ throw new Error(`db ${dbName} not exists`);
166
+ }
167
+ let ret = await this.dbs.get(this.getDbNameFinal(dbName)).getTxByOnlyMsgHash(msgCellHash);
168
+ if(!ret || ret.length == 0) {
169
+ return null;
170
+ }else{
171
+ return convertTonTransToTrans(ret);
172
+ }
173
+ }
174
+
163
175
  getDbNameFinal(dbName:string){
164
176
  let dbNameFinal = dbName;
165
177
  if(Address.parse(dbName).toString() != dbName){
@@ -156,7 +156,7 @@ export async function getAllTransactions(client:TonClient,scAddress:string,limit
156
156
  return trans;
157
157
  }
158
158
 
159
- async function getEventFromTran(client:TonClient,tran:Transaction, scAddress:string){
159
+ export async function getEventFromTran(client:TonClient,tran:Transaction, scAddress:string){
160
160
  let bodyCell = tran.inMessage?.body;
161
161
  if(!bodyCell){
162
162
  console.error("body is empty","tran",tran.hash().toString("base64"));
@@ -325,7 +325,7 @@ export async function getEventByTranHash(client:TonClient, scAddress:string, lt:
325
325
  return await getEventFromTran(client,tran,scAddress);
326
326
  }
327
327
 
328
- async function getOpCodeFromCell(cell:Cell){
328
+ export async function getOpCodeFromCell(cell:Cell){
329
329
  if(cell.equals(Cell.EMPTY)){
330
330
  throw new Error("empty cell");
331
331
  }
package/index.ts CHANGED
@@ -1,19 +1,22 @@
1
1
  export {
2
- TonClientConfig, getClient,
2
+ TonClientConfig, TonUrlConfig,TonConfig,getGlobalTonConfig,getClient,wanTonSdkInit
3
3
  } from "./client/client";
4
4
 
5
5
  export {codeTable,} from "./code/encode-decode"
6
- export {buildUserLockMessages} from "./code/userLock"
7
- export {getTokenPairInfo} from "./code/userLock"
6
+ export {LOCK_TYPE,buildUserLockMessages,getFee,getJettonWalletAddr,getJettonAdminAddr,getTokenPairInfo,} from "./code/userLock"
7
+
8
+ export {configTestnet,configMainnet} from "./config/config-ex"
8
9
 
9
10
  export {
10
11
  BIP44_CHAINID, TON_COIN_ACCOUT, TON_COIN_ACCOUNT_STR, WK_CHIANID,
11
12
  } from "./const/const-value"
12
13
 
13
14
  export {
14
- getEvents,
15
+ getEvents,getTransactions,getAllTransactions,getEventFromTran,getTransaction,getTransactionFromDb,getEventByTranHash,getOpCodeFromCell
15
16
  } from "./event/getEvents"
16
17
 
18
+ export {TON_FEE} from "./fee/fee"
19
+
17
20
  export {
18
21
  computeHash,
19
22
  } from "./sign/buildHash"
@@ -30,6 +33,7 @@ export {
30
33
 
31
34
  export {
32
35
  TranStepInfo, TranResult, TranPathInfo, getTranResultByMsgHash, getTranResultByTxHash, getTranByMsgHash,
36
+ getTranResultByTran,getTranByOnlyMsgHash
33
37
  } from "./transResult/transResult"
34
38
 
35
39
  export {
@@ -37,9 +41,13 @@ export {
37
41
  } from "./utils/compileContract"
38
42
 
39
43
  export {
40
- getRandomTon, BufferrToHexString, HexStringToBuffer, getQueryID, bigIntReviver, bigIntReplacer
44
+ getRandomTon, BufferrToHexString, HexStringToBuffer, getQueryID, bigIntReviver,
45
+ bigIntReplacer,isAddressEqual,bigIntToBytes32,int64ToByte32,remove0x,add0x,
46
+ formatError,isValidHexString,isNotBase64,toBase64
41
47
  } from "./utils/utils"
42
48
 
49
+ export {CoinBalance,TokenBalance} from "./wallet/balance"
50
+
43
51
  export {
44
52
  getJettonBalance,
45
53
  getJettonAddress,
@@ -65,3 +73,4 @@ export {
65
73
  getWalletAddrBySecPrvKey,
66
74
  isAddrDepolyed
67
75
  } from "./wallet/walletContract"
76
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wan-ton-sdk",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "scripts": {
5
5
  "build": "rm -rf ../wan-ton-sdk/dist && tsc",
6
6
  "test": "jest --verbose",
@@ -90,6 +90,7 @@ export async function getUpperStepsFromDb(client:TonClient,scAddr:Address,tran:T
90
90
  let dbAccess = await DBAccess.getDBAccess();
91
91
  if(!dbAccess){
92
92
  console.error("not using db cache");
93
+ throw new Error("not using db cache");
93
94
  }
94
95
  let transFromDb = null;
95
96
  let foundInDb = false;
@@ -140,6 +141,7 @@ export async function getUpperSteps(client:TonClient,scAddr:Address,tran:Transac
140
141
  console.log("getUpperStepsFromDb error",formatError(err));
141
142
  }
142
143
 
144
+ console.log("getUpperSteps from rpc","scAddr",scAddr,"tran hash",tran.hash().toString('hex'));
143
145
  const inMessageCell = beginCell().store(storeMessage( tran.inMessage)).endCell();
144
146
  console.log("inMessageCell==>",inMessageCell.toBoc().toString('hex'));
145
147
  let tranInMsgHash = inMessageCell.hash().toString('hex');
@@ -424,6 +426,33 @@ export async function findMsgCellHashInTran(tran:Transaction,msgCellHash:string,
424
426
  return found;
425
427
  }
426
428
 
429
+ export async function findOnlyMsgCellHashInTran(tran:Transaction,msgCellHash:string):Promise<Boolean> {
430
+ let found = false;
431
+ const inMessageCell = beginCell().store(storeMessage(tran.inMessage)).endCell();
432
+ let inMessageHash = inMessageCell.hash().toString('hex');
433
+ let inMessageBodyCellHash = tran.inMessage.body.hash().toString('hex');
434
+ let internalMsg = true;
435
+ if(tran.inMessage.info.type == 'external-in'){
436
+ internalMsg = false;
437
+ }
438
+ let inMessageLt = "";
439
+ if(internalMsg){
440
+ inMessageLt = (tran.inMessage.info as unknown as CommonMessageInfoInternal).createdLt.toString(10);
441
+ }
442
+
443
+ if(!internalMsg){
444
+ if ((inMessageHash == msgCellHash)) {
445
+ found = true;
446
+ }
447
+ }else{
448
+ if ((inMessageHash == msgCellHash)) {
449
+ found = true;
450
+ }
451
+ }
452
+
453
+ return found;
454
+ }
455
+
427
456
  export async function getTranByMsgHash(client:TonClient, scAddr:Address, msgCellHash:string,msgBodyHash:string,lt:string=''):Promise<Transaction> {
428
457
  let limit = MAX_LIMIT;
429
458
  let retry = 5
@@ -501,121 +530,79 @@ export async function getTranByMsgHash(client:TonClient, scAddr:Address, msgCell
501
530
 
502
531
  }
503
532
 
504
- /*
505
-
506
- async function monitorTransactionbyHash(client, addr, txhash, txlt) {
507
- let allTransactions = []
508
- const myAddress = Address.parse(addr);
509
- let tx = await client.getTransaction(myAddress,txlt, txhash)
510
- for (const outMessage of tx.outMessages.values()) {
511
- const outMessageCell = beginCell().store(storeMessage(outMessage)).endCell();
512
- const outMessageHash = outMessageCell.hash().toString('hex');
513
- await findTransactionbyMsgHash(client, outMessage.info.dest.toString({testOnly:true}), outMessageHash, allTransactions)
514
- }
515
- let success = true
516
- for(let i=0; i<allTransactions.length; i++) {
517
- success = success && allTransactions[i].description.aborted==false && allTransactions[i].description.computePhase.exitCode==0
518
- && allTransactions[i].description.computePhase.seccess && allTransactions[i].description.actionPhase.seccess
519
- }
520
- return {success,allTransactions}
521
- }
522
-
523
- async function monitorTransactionbyExternalIn(client, from, msgHash){
524
- let allTransactions = []
525
- await waitTransactionbyExternalIn(client, from, msgHash, allTransactions);
526
- let success = true
527
- for(let i=0; i<allTransactions.length; i++) {
528
- success = success && allTransactions[i].description.aborted==false && allTransactions[i].description.computePhase.exitCode==0
529
- && allTransactions[i].description.computePhase.seccess && allTransactions[i].description.actionPhase.seccess
530
- }
531
- return {success,allTransactions}
532
- }
533
-
534
- // wait the new sub transactions to be confirmed
535
- async function waitTransactionbyExternalIn(client, from, msgHash, allTransactions) {
536
- const myAddress = Address.parse(from); // address that you want to fetch transactions from
537
- const maxRetry = 30;
538
- let retry=0;
539
- let to_lt = "0"
540
- while(retry++ < maxRetry) {
541
- console.log("call getTransactions, para:", myAddress.toString({testOnly:true}), to_lt);
542
- const transactions = await client.getTransactions(myAddress, {
543
- to_lt ,
544
- limit: 10,
545
- });
546
- console.log("transactions length:", transactions.length)
547
- for (let i=0; i<transactions.length; i++) {
548
- let tx = transactions[i]
549
- if(i == 0) {
550
- to_lt = tx.lt.toString()
533
+ export async function getTranByOnlyMsgHash(client:TonClient, scAddr:Address, msgCellHash:string):Promise<Transaction> {
534
+ let limit = MAX_LIMIT;
535
+ let retry = 5
536
+ let maxRetry = retry;
537
+ //get from scanned db
538
+ let dbAccess = await DBAccess.getDBAccess();
539
+ let transFromDb = null;
540
+ while(maxRetry-- >0 && !transFromDb){
541
+ try{
542
+ let inDb = await dbAccess?.has(scAddr.toString());
543
+ if(!inDb){
544
+ await dbAccess.addDbByName(scAddr.toString());
545
+ await sleep(2000);
551
546
  }
552
- const transactionHash = tx.hash().toString('hex');
553
- console.log("tx hash is:",i, tx.lt, transactionHash)
554
- const inMessage = tx.inMessage;
555
- let inMessageHash
556
- // if (inMessage?.info.type === 'external-in') {
557
- const inMessageCell = beginCell().store(storeMessage(inMessage)).endCell();
558
- inMessageHash = inMessageCell.hash().toString('hex');
559
- // console.log("inMessageHash", inMessageHash, msgHash);
560
- if(inMessageHash == msgHash) {
561
- console.log("found:", tx.lt, tx.hash().toString('hex'))
562
- allTransactions.push(tx)
563
- if(tx.outMessagesCount!=0){
564
- for (const outMessage of tx.outMessages.values()) {
565
- console.log("outMessage:", outMessage)
566
- const outMessageCell = beginCell().store(storeMessage(outMessage)).endCell();
567
- const outMessageHash = outMessageCell.hash().toString('hex');
568
- console.log("outMessageHash:", outMessageHash)
569
- await waitTransactionbyExternalIn(client, outMessage.info.dest.toString(), outMessageHash, allTransactions)
570
- }
571
- }
572
- return
547
+ transFromDb = await dbAccess?.getTxByOnlyMsgHash(scAddr.toString(),msgCellHash);
548
+ if(transFromDb){ // found from db
549
+ return transFromDb;
573
550
  }
551
+ }catch(err){
552
+ console.error("getTranByMsgHash from db err",formatError(err),"retry ",maxRetry);
574
553
  }
575
- await sleep(1000)
554
+ await sleep(2000);
576
555
  }
577
- }
578
556
 
579
- // find old transactions.
580
- async function findTransactionbyMsgHash(client:TonClient, from:Address, msgHash:string, allTransactions) {
581
- const myAddress = from;
582
- let maxRetry = 30;
583
- let txOpts = {
584
- limit: 10
557
+ //get from rpc
558
+ maxRetry = retry;
559
+
560
+ let trans = [];
561
+ let transCount = limit;
562
+ let opts: {
563
+ limit: number;
564
+ lt?: string;
565
+ hash?: string;
566
+ to_lt?: string;
567
+ inclusive?: boolean;
568
+ archival?: boolean;
569
+ } = {
570
+ limit,
571
+ archival:true,
585
572
  }
586
- while(maxRetry-- > 0){
587
- console.log("call findTransactionbyMsgHash, para:", from, txOpts);
588
- const transactions = await client.getTransactions(from, txOpts);
589
- console.log("transactions length:", transactions.length)
590
- for (let i=0; i<transactions.length; i++) {
591
- let tx = transactions[i]
592
- // console.log("tx:", tx)
593
- const transactionHash = tx.hash().toString('hex');
594
- console.log("tx hash is:",i, tx.lt, transactionHash)
595
- const inMessage = tx.inMessage;
596
- let inMessageHash
597
- // if (inMessage?.info.type === 'external-in') {
598
- const inMessageCell = beginCell().store(storeMessage(inMessage)).endCell();
599
- inMessageHash = inMessageCell.hash().toString('hex');
600
- // console.log("inMessageHash", inMessageHash, msgHash);
601
- if(inMessageHash == msgHash) {
602
- console.log("found:", tx.lt, tx.hash().toString('hex'))
603
- allTransactions.push(tx)
604
- if(tx.outMessagesCount!=0){
605
- for (const outMessage of tx.outMessages.values()) {
606
- console.log("outMessage:", outMessage)
607
- const outMessageCell = beginCell().store(storeMessage(outMessage)).endCell();
608
- const outMessageHash = outMessageCell.hash().toString('hex');
609
- console.log("outMessageHash:", outMessageHash)
610
- await findTransactionbyMsgHash(client, outMessage.info.dest.toString(), outMessageHash, allTransactions)
573
+
574
+ while(transCount){
575
+ let getSuccess = false
576
+ while(maxRetry-- >0 && (!getSuccess)){
577
+ try{
578
+ console.log("maxRetry = %s, getSuccess = %s, transCount = %s, scAddress = %s opts = %s",maxRetry,getSuccess,transCount,scAddr,JSON.stringify(opts,bigIntReplacer));
579
+ let ret = await client.getTransactions(scAddr,opts)
580
+ transCount = ret.length;
581
+ console.log("getTransactions success","opts",JSON.stringify(opts,bigIntReplacer),"len of getTransactions",transCount);
582
+ for(let tran of ret){
583
+ console.log("=====> tranHash = %s lt = %s",tran.hash().toString('base64'),tran.lt.toString(10));
584
+ let found = await findOnlyMsgCellHashInTran(tran,msgCellHash);
585
+ if(found){
586
+ return tran;
611
587
  }
612
588
  }
613
- return
589
+ if(ret.length){
590
+ opts.lt = ret[ret.length-1].lt.toString(10);
591
+ opts.hash = ret[ret.length-1].hash().toString('base64');
592
+ }
593
+ getSuccess = true;
594
+ maxRetry = retry;
595
+
596
+ }catch(e){
597
+ console.error("err ",formatError(e));
598
+ await sleep(2000);
614
599
  }
615
600
  }
616
- txOpts.hash = transactions[transactions.length - 1].hash().toString('base64')
617
- txOpts.lt = transactions[transactions.length - 1].lt.toString()
601
+ if(maxRetry == 0){
602
+ throw(new Error(formatUtil("getTransactions failed after %d retry. opts is %s",retry,JSON.stringify(opts))));
603
+ }
604
+
605
+ await sleep(2000);
618
606
  }
619
- }
620
607
 
621
- */
608
+ }
package/tsconfig.json CHANGED
@@ -15,5 +15,5 @@
15
15
  },
16
16
  "include": ["./**/*"],
17
17
  "exclude": [
18
- "node_modules", "./*.spec.ts"]
18
+ "node_modules", "./*.spec.ts","./*/*log*","./log/*"]
19
19
  }