forked from cerc-io/registry-sdk
		
	Compare commits
	
		
			2 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 04d176eb55 | |||
| 9152d09c66 | 
| @ -1,6 +1,6 @@ | |||||||
| { | { | ||||||
|   "name": "@cerc-io/registry-sdk", |   "name": "@cerc-io/registry-sdk", | ||||||
|   "version": "0.2.10", |   "version": "0.2.11", | ||||||
|   "main": "dist/index.js", |   "main": "dist/index.js", | ||||||
|   "types": "dist/index.d.ts", |   "types": "dist/index.d.ts", | ||||||
|   "repository": "git@github.com:cerc-io/registry-sdk.git", |   "repository": "git@github.com:cerc-io/registry-sdk.git", | ||||||
|  | |||||||
| @ -67,10 +67,13 @@ const auctionTests = () => { | |||||||
|   test('Commit bids.', async () => { |   test('Commit bids.', async () => { | ||||||
|     for (let i = 0; i < numBidders; i++) { |     for (let i = 0; i < numBidders; i++) { | ||||||
|       bidAmounts.push((LOWEST_BID_AMOUNT + (i * 500)).toString()); |       bidAmounts.push((LOWEST_BID_AMOUNT + (i * 500)).toString()); | ||||||
|       bidderAccounts[i].bid = await createBid(chainId, auctionId, bidderAccounts[i].address, `${bidAmounts[i]}${DENOM}`); |  | ||||||
|       await registry.commitBid({ auctionId, commitHash: bidderAccounts[i].bid.commitHash }, bidderAccounts[i].privateKey, fee); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     await Promise.all(bidderAccounts.map(async (bidderAccount, i) => { | ||||||
|  |       bidderAccounts[i].bid = await createBid(chainId, auctionId, bidderAccount.address, `${bidAmounts[i]}${DENOM}`); | ||||||
|  |       return registry.commitBid({ auctionId, commitHash: bidderAccount.bid.commitHash }, bidderAccount.privateKey, fee); | ||||||
|  |     })); | ||||||
|  | 
 | ||||||
|     const [auction] = await registry.getAuctionsByIds([auctionId]); |     const [auction] = await registry.getAuctionsByIds([auctionId]); | ||||||
|     expect(auction.status).toEqual('commit'); |     expect(auction.status).toEqual('commit'); | ||||||
|     expect(auction.bids.length).toEqual(3); |     expect(auction.bids.length).toEqual(3); | ||||||
| @ -90,9 +93,9 @@ const auctionTests = () => { | |||||||
|     const [auction] = await registry.getAuctionsByIds([auctionId]); |     const [auction] = await registry.getAuctionsByIds([auctionId]); | ||||||
|     expect(auction.status).toEqual('reveal'); |     expect(auction.status).toEqual('reveal'); | ||||||
| 
 | 
 | ||||||
|     for (let i = 0; i < numBidders; i++) { |     await Promise.all(bidderAccounts.map(async bidderAccount => { | ||||||
|       await registry.revealBid({ auctionId, reveal: bidderAccounts[i].bid.revealString }, bidderAccounts[i].privateKey, fee); |       return registry.revealBid({ auctionId, reveal: bidderAccount.bid.revealString }, bidderAccount.privateKey, fee); | ||||||
|     } |     })); | ||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
|   test('Check bids are revealed', async () => { |   test('Check bids are revealed', async () => { | ||||||
| @ -175,7 +178,7 @@ const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number | |||||||
|     const otherAcc = await Account.generateFromMnemonic(mnenonic3); |     const otherAcc = await Account.generateFromMnemonic(mnenonic3); | ||||||
|     await otherAcc.init(); |     await otherAcc.init(); | ||||||
| 
 | 
 | ||||||
|     await registry.sendCoins({ denom: DENOM, amount: CREATOR_INITIAL_BALANCE.toString(), destinationAddress: otherAcc.address }, privateKey, fee); |     await registry.sendCoins({ denom: DENOM, amount: BIDDER_INITIAL_BALANCE.toString(), destinationAddress: otherAcc.address }, privateKey, fee); | ||||||
|     otherAccount = { address: otherAcc.address, privateKey: otherAcc.privateKey.toString('hex') }; |     otherAccount = { address: otherAcc.address, privateKey: otherAcc.privateKey.toString('hex') }; | ||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
| @ -205,10 +208,10 @@ const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number | |||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
|   test('Commit bids.', async () => { |   test('Commit bids.', async () => { | ||||||
|     for (let i = 0; i < numBidders; i++) { |     await Promise.all(bidderAccounts.map(async (bidderAccount, i) => { | ||||||
|       bidderAccounts[i].bid = await createBid(chainId, auctionId, bidderAccounts[i].address, `${bidAmounts[i]}${DENOM}`); |       bidderAccounts[i].bid = await createBid(chainId, auctionId, bidderAccount.address, `${bidAmounts[i]}${DENOM}`); | ||||||
|       await registry.commitBid({ auctionId, commitHash: bidderAccounts[i].bid.commitHash }, bidderAccounts[i].privateKey, fee); |       return registry.commitBid({ auctionId, commitHash: bidderAccount.bid.commitHash }, bidderAccount.privateKey, fee); | ||||||
|     } |     })); | ||||||
| 
 | 
 | ||||||
|     sortedBidders = bidderAccounts.slice().sort((a, b) => { |     sortedBidders = bidderAccounts.slice().sort((a, b) => { | ||||||
|       return parseInt(a.bid.reveal.bidAmount) - parseInt(b.bid.reveal.bidAmount); |       return parseInt(a.bid.reveal.bidAmount) - parseInt(b.bid.reveal.bidAmount); | ||||||
| @ -239,21 +242,21 @@ const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number | |||||||
|     const [auction] = await registry.getAuctionsByIds([auctionId]); |     const [auction] = await registry.getAuctionsByIds([auctionId]); | ||||||
|     expect(auction.status).toEqual('reveal'); |     expect(auction.status).toEqual('reveal'); | ||||||
| 
 | 
 | ||||||
|     for (let i = 0; i < numBidders; i++) { |     await Promise.all(bidderAccounts.map(async bidderAccount => { | ||||||
|       try { |       try { | ||||||
|         await registry.revealBid( |         await registry.revealBid( | ||||||
|           { auctionId, reveal: bidderAccounts[i].bid.revealString }, |           { auctionId, reveal: bidderAccount.bid.revealString }, | ||||||
|           bidderAccounts[i].privateKey, |           bidderAccount.privateKey, | ||||||
|           fee |           fee | ||||||
|         ); |         ); | ||||||
|       } catch (error: any) { |       } catch (error: any) { | ||||||
|         if (invalidBidderAddress === bidderAccounts[i].address) { |         if (bidderAccount.address === invalidBidderAddress) { | ||||||
|           expect(error.toString()).toContain(INVALID_BID_ERROR); |           expect(error.toString()).toContain(INVALID_BID_ERROR); | ||||||
|         } else { |         } else { | ||||||
|           throw error; |           throw error; | ||||||
|         } |         } | ||||||
|       } |       } | ||||||
|     } |     })); | ||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
|   test('Check bids are revealed', async () => { |   test('Check bids are revealed', async () => { | ||||||
|  | |||||||
| @ -588,3 +588,4 @@ export { LaconicClient }; | |||||||
| export * from './constants'; | export * from './constants'; | ||||||
| export * from './types/cerc/bond/message'; | export * from './types/cerc/bond/message'; | ||||||
| export * from './types/cerc/onboarding/message'; | export * from './types/cerc/onboarding/message'; | ||||||
|  | export { getGasPrice, parseGasAndFees } from './util'; | ||||||
|  | |||||||
							
								
								
									
										36
									
								
								src/util.ts
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								src/util.ts
									
									
									
									
									
								
							| @ -1,6 +1,8 @@ | |||||||
| import * as Block from 'multiformats/block'; | import * as Block from 'multiformats/block'; | ||||||
| import { sha256 as hasher } from 'multiformats/hashes/sha2'; | import { sha256 as hasher } from 'multiformats/hashes/sha2'; | ||||||
|  | import assert from 'assert'; | ||||||
| 
 | 
 | ||||||
|  | import { GasPrice, StdFee, parseCoins } from '@cosmjs/stargate'; | ||||||
| import * as dagCBOR from '@ipld/dag-cbor'; | import * as dagCBOR from '@ipld/dag-cbor'; | ||||||
| import * as dagJSON from '@ipld/dag-json'; | import * as dagJSON from '@ipld/dag-json'; | ||||||
| 
 | 
 | ||||||
| @ -22,7 +24,7 @@ export class Util { | |||||||
| 
 | 
 | ||||||
|     let keys = Object.keys(obj); |     let keys = Object.keys(obj); | ||||||
|     keys = keys.sort(); |     keys = keys.sort(); | ||||||
|     const newObject: {[key: string]: any} = {}; |     const newObject: { [key: string]: any } = {}; | ||||||
| 
 | 
 | ||||||
|     for (let i = 0; i < keys.length; i++) { |     for (let i = 0; i < keys.length; i++) { | ||||||
|       newObject[keys[i]] = Util.sortJSON(obj[keys[i]]); |       newObject[keys[i]] = Util.sortJSON(obj[keys[i]]); | ||||||
| @ -77,7 +79,7 @@ export class Util { | |||||||
|    * Unmarshal attributes array to object. |    * Unmarshal attributes array to object. | ||||||
|    */ |    */ | ||||||
|   static fromGQLAttributes (attributes: any[] = []) { |   static fromGQLAttributes (attributes: any[] = []) { | ||||||
|     const res: {[key: string]: any} = {}; |     const res: { [key: string]: any } = {}; | ||||||
| 
 | 
 | ||||||
|     attributes.forEach(attr => { |     attributes.forEach(attr => { | ||||||
|       res[attr.key] = (attr.value === null) ? null : this.fromGQLValue(attr.value); |       res[attr.key] = (attr.value === null) ? null : this.fromGQLValue(attr.value); | ||||||
| @ -119,3 +121,33 @@ export class Util { | |||||||
|     return block.cid.toString(); |     return block.cid.toString(); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Get gas in proper format | ||||||
|  |  */ | ||||||
|  | export const parseGasAndFees = (gas?: string, fees?: string): StdFee | undefined | number => { | ||||||
|  |   // If fees is not given or a number, treat it as a gas estimation multiplier
 | ||||||
|  |   if (fees === null || fees === undefined) { | ||||||
|  |     return undefined; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   const isFeesANumber = !isNaN(Number(fees)); | ||||||
|  |   if (isFeesANumber) { | ||||||
|  |     return Number(fees); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   // If fees is not a gas estimation multiplier, gas is required
 | ||||||
|  |   assert(gas, 'Invalid gas.'); | ||||||
|  | 
 | ||||||
|  |   return { | ||||||
|  |     amount: parseCoins(String(fees)), | ||||||
|  |     gas: String(gas) | ||||||
|  |   }; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Get gas price in proper format | ||||||
|  |  */ | ||||||
|  | export const getGasPrice = (gasPrice: string | null): GasPrice | undefined => { | ||||||
|  |   return gasPrice != null ? GasPrice.fromString(String(gasPrice)) : undefined; | ||||||
|  | }; | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user