This repository has been archived by the owner on Oct 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Spacemesh api mock [WIP] #42
Open
ImmanuelSegol
wants to merge
6
commits into
spacemeshos:master
Choose a base branch
from
ImmanuelSegol:spacemesh-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fedb429
feat(SpacemeshApiMock) adds SpacemeshApi HOC component
ImmanuelSegol 1f72f83
chore(SpacemeshApiClientMock) added props
ImmanuelSegol 3163c65
chore(review) add axios like responses
ImmanuelSegol a1d1dd8
chore() review fix
ImmanuelSegol 1feda42
chore(review) fix
ImmanuelSegol cd82171
chore(review) changes based on review
ImmanuelSegol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
import * as React from 'react'; | ||
import { ISpacemeshApi } from '../../model'; | ||
import { ITransaction, TransactionStatus, MockData} from 'src/types'; | ||
|
||
interface IProps extends ISpacemeshApi{ | ||
apiReady: boolean; | ||
} | ||
|
||
interface IState { | ||
connectedToApi: boolean; | ||
} | ||
|
||
const SpacemeshApiClientMock = <P extends IProps>(Component: React.ComponentType<P>) => { | ||
return class SpacemeshApiClient extends React.Component<P & IProps, IState> implements ISpacemeshApi { | ||
constructor(props: P & IProps) { | ||
super(props); | ||
this.state = { | ||
connectedToApi: false, | ||
} | ||
} | ||
|
||
public componentDidMount() { | ||
this.connectSpacemeshApiClient(); | ||
} | ||
|
||
public render() { | ||
|
||
return <Component | ||
{...this.props} | ||
apiReady = {this.state.connectedToApi} | ||
boradCastTransaction = {this.boradCastTransaction} | ||
getAddressBalance = {this.getAddressBalance} | ||
getTransactionFromAddress = {this.getTransactionFromAddress} | ||
buySMCFromExchange = {this.buySMCFromExchange} | ||
getCurrentSMCPrice = {this.getCurrentSMCPrice} | ||
getSMCfromTestNetTap = {this.getSMCfromTestNetTap} | ||
getTransactionStatus = {this.getTransactionStatus} | ||
/>; | ||
} | ||
|
||
/** | ||
* Broadcast a transaction to the Spacemesh network | ||
* @param tx raw transaction to broadcast | ||
* @returns transactions hash | ||
*/ | ||
public boradCastTransaction = (tx: ITransaction): Promise<MockData.IBroadCastTransactionResponse> => { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve({ | ||
id:1, | ||
jsonrpc: '1.0', | ||
params: [tx], | ||
result: '0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331' | ||
ImmanuelSegol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
}, 2000); | ||
}); | ||
} | ||
|
||
/** | ||
* Get SMC balance of an address | ||
* @param address | ||
* @returns amount of SMC in address | ||
*/ | ||
public getAddressBalance = (address: string): Promise<MockData.IGetAddressBalanceResponse> => { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve({ | ||
id: 1, | ||
jsonrpc: '1.0', | ||
params:[address], | ||
result: '0x0234c8a3397aab58', | ||
}); | ||
}, 2000); | ||
}); | ||
} | ||
|
||
/** | ||
* Get all transactions sent by an address | ||
* @param address account address | ||
* @returns all transactions sent by address | ||
*/ | ||
public getTransactionFromAddress = (address: string): Promise<MockData.IGetTransactionFromAddressResponse> => { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve({ | ||
id: 1, | ||
jsonrpc: '1.0', | ||
params:[address], | ||
result: ['0x130012017c9d75e66a5aff3895aebceaaa6a43887b3f466aa73cc0e5edeea745', | ||
'0xaa12d099e33eccacb71d4c7b27bffa1ca2504897fe59aa5784bfdfabd363f5b9', | ||
'0xd9a2d6ac3355a03482da887b6f43083423fb8b9b2d9def1d9cfa330aad6dc886'], | ||
} | ||
); | ||
}, 2000); | ||
}); | ||
ImmanuelSegol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Get a transaction by its hash | ||
* @param txhash transactions hash | ||
* @returns full transaction | ||
*/ | ||
public getTransactionFromHash = (txhash: string): Promise<MockData.IGetTransactionFromHashResponse> => { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve({ | ||
id: 1, | ||
jsonrpc: '1.0', | ||
params:[txhash], | ||
result: { | ||
number: "0x1b4", | ||
hash: "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331", | ||
parentHash: "0x9646252be9520f6e71339a8df9c55e4d7619deeb018d2a3f2d21fc165dde5eb5", | ||
nonce: "0xe04d296d2460cfb8472af2c5fd05b5a214109c25688d3704aed5484f9a7792f2", | ||
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", | ||
logsBloom: "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331", | ||
transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", | ||
stateRoot: "0xd5855eb08b3387c0af375e9cdb6acfc05eb8f519e419b874b6ff2ffda7ed1dff", | ||
miner: "0x4e65fda2159562a496f9f3522f89122a3088497a", | ||
difficulty: "0x027f07", | ||
totalDifficulty: "0x027f07", | ||
extraData: "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
size: "0x027f07", | ||
gasLimit: "0x9f759", | ||
gasUsed: "0x9f759", | ||
timestamp: "0x54e34e8e" | ||
} | ||
} | ||
); | ||
}, 2000); | ||
}); | ||
} | ||
|
||
/** | ||
* Buy SMC from exchange | ||
* @param exchangeId the exchange we want to query for deals | ||
* @param pair the currency we want to buy SMC for | ||
* @param quantity the amount of SMC we want to buy | ||
* @param maxprice the maximum we are willing to pay | ||
* @return the price of SMC in the currency we queried for | ||
*/ | ||
public buySMCFromExchange = (exchangeId: string, pair: string, quantity: number, maxprice: number): Promise<MockData.IBuySMCFromExchange> => { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve({ | ||
id: 1, | ||
jsonrpc: '1.0', | ||
params: [exchangeId, pair, quantity, maxprice], | ||
result: [ | ||
{ | ||
quantity, | ||
offerId:'ff2825ec-d939-11e8-8801-f2801f1b9fd1', | ||
offerDate: `${new Date().getMilliseconds()}`, | ||
price: 12, | ||
} | ||
] | ||
}); | ||
}, 2000); | ||
}); | ||
} | ||
|
||
/** | ||
* Get Current SMC Price | ||
* @param exchangeId the exchange we want to query for the current price | ||
* @param pair the currency we want to buy SMC for | ||
* @returns the best price found on the exchange | ||
*/ | ||
public getCurrentSMCPrice = (exchangeId: string, pair: string): Promise<MockData.IGetCurrentSMCPriceResponse> => { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve({ | ||
id: 1, | ||
jsonrpc: '1.0', | ||
params: [exchangeId, pair], | ||
result: 24 | ||
}); | ||
}, 2000); | ||
}); | ||
} | ||
|
||
/** | ||
* Get SMC from testnet tap | ||
* @param amout the desired amount of SMC | ||
* @returns a boolean indicating if successfully requested funds | ||
*/ | ||
public getSMCfromTestNetTap = (amount: number): Promise<MockData.IGetSMCfromTestNetTap> => { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve({ | ||
id: 1, | ||
jsonrpc: '1.0', | ||
params: [amount], | ||
result: true, | ||
}); | ||
}, 2000); | ||
}); | ||
} | ||
|
||
/** | ||
* Gets the current status of a transaction by its transaction hash | ||
* @param txhash transaction hash | ||
* @returns the status of the transaction | ||
*/ | ||
public getTransactionStatus = (txhash: string): Promise<MockData.IGetTransactionStatus> => { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve({ | ||
id: 1, | ||
jsonrpc: '1.0', | ||
params: [txhash], | ||
result: TransactionStatus.Success, | ||
}) | ||
}, 2000); | ||
}) | ||
} | ||
|
||
/** | ||
* Connect to Spacemesh api | ||
*/ | ||
private connectSpacemeshApiClient = async () => { | ||
await new Promise(resolve => { | ||
setTimeout(() => { | ||
this.setState(prev => ({connectedToApi: !prev.connectedToApi})) | ||
resolve() | ||
}, 2000); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default SpacemeshApiClientMock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ITransaction, MockData} from '../types'; | ||
ImmanuelSegol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* SpaceMesh api | ||
* @description Spacemesh api | ||
*/ | ||
export interface ISpacemeshApi { | ||
boradCastTransaction(tx: ITransaction): Promise<MockData.IBroadCastTransactionResponse>; | ||
getAddressBalance(address: string): Promise<MockData.IGetAddressBalanceResponse>; | ||
getTransactionFromAddress(address: string): Promise<MockData.IGetTransactionFromAddressResponse>; | ||
getTransactionFromHash(txhash: string): Promise<MockData.IGetTransactionFromHash>; | ||
buySMCFromExchange(exchangeId: string, pair: string, quantity: number, maxprice: number): Promise<MockData.IBuySMCFromExchange>; | ||
getCurrentSMCPrice(exchangeId: string, pair: string): Promise<MockData.IGetCurrentSMCPriceResponse>; | ||
getSMCfromTestNetTap(amount: number): Promise<MockData.IGetSMCfromTestNetTap>; | ||
getTransactionStatus(txhash: string): Promise<MockData.IGetTransactionStatus>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./InputModel"; | ||
export * from "./InputModel"; | ||
export * from './SpacemeshApi'; | ||
ImmanuelSegol marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import TransactionStatus from './TransactionStatus'; | ||
import ITransaction from './MockeTransaction'; | ||
|
||
interface IResponse { | ||
jsonrpc: string; | ||
params:any[], | ||
id: number; | ||
} | ||
|
||
export interface IBroadCastTransactionResponse extends IResponse { | ||
result: string; | ||
} | ||
|
||
export interface IGetAddressBalanceResponse extends IResponse { | ||
result: string; | ||
} | ||
|
||
export interface IGetTransactionFromAddressResponse extends IResponse { | ||
result: string[]; | ||
} | ||
|
||
export interface IGetTransactionFromHashResponse extends IResponse { | ||
result: ITransaction; | ||
} | ||
|
||
export interface IGetAddressFromTransactionResponse extends IResponse { | ||
result: string; | ||
} | ||
|
||
export interface IBuySMCFromExchange extends IResponse { | ||
result: Array<{ | ||
offerId: string, | ||
offerDate: string, | ||
price: number, | ||
quantity: number, | ||
}>; | ||
} | ||
|
||
export interface IGetCurrentSMCPriceResponse extends IResponse { | ||
result: number; | ||
} | ||
|
||
export interface IGetSMCfromTestNetTap extends IResponse { | ||
result: boolean; | ||
} | ||
|
||
export interface IGetTransactionStatus extends IResponse { | ||
result: TransactionStatus; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
interface ITransaction { | ||
number: string, | ||
hash: string, | ||
parentHash: string, | ||
nonce: string, | ||
sha3Uncles: string, | ||
logsBloom: string, | ||
transactionsRoot: string, | ||
stateRoot: string, | ||
miner: string, | ||
difficulty: string, | ||
totalDifficulty: string, | ||
extraData: string, | ||
size: string, | ||
gasLimit: string, | ||
gasUsed: string, | ||
timestamp: string, | ||
} | ||
|
||
export default ITransaction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
enum TransactionStatus { | ||
Cancelled, | ||
Faild, | ||
Success, | ||
} | ||
|
||
export default TransactionStatus; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import ITransaction from './MockeTransaction'; | ||
import TransactionStatus from './TransactionStatus'; | ||
import * as MockData from './MockData'; | ||
|
||
export { | ||
ITransaction, | ||
TransactionStatus, | ||
MockData, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clean code and good documentation, thanks.
I came back here for a second pass and am missing the class-level comment :-(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@daonb What do you mean by the class-level comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like I missed the line, sorry. I meant you should add in line 12 something like: