-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Obtain transaction payload, mirror back to client * Respond with request, or error if none present * Load node friendly versions of cosmos-api for dev * Send response as JSON * Successfully retrieve a gas estimate - Use node-fetch for cosmosAPIs - * Add two sub routes: /estimate and /broadcast * Add account number and sequence to overview query * Disable cors options * Add support to broadcast messages * Ignore fetch undefined * Delete package-lock.json
- Loading branch information
Showing
11 changed files
with
214 additions
and
5 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,102 @@ | ||
const { getMessage } = require('./messageConstructor') | ||
const { networks } = require('../../networks') | ||
|
||
global.fetch = require('node-fetch') | ||
|
||
async function estimate(tx) { | ||
const context = { | ||
userAddress: tx.address, | ||
networkId: tx.networkId, | ||
url: networks[tx.networkId].api_url, | ||
chainId: networks[tx.networkId].chain_id | ||
} | ||
|
||
const message = getMessage(tx.messageType, tx.txProperties, context) | ||
|
||
try { | ||
const gasEstimate = await message.simulate({ memo: tx.memo }) | ||
return { | ||
gasEstimate, | ||
success: true | ||
} | ||
} catch (e) { | ||
return { | ||
error: e, | ||
success: false | ||
} | ||
} | ||
} | ||
|
||
async function broadcast(tx) { | ||
console.log(`Received broadcast: ${JSON.stringify(tx)}`) | ||
try { | ||
const hash = await broadcastTransaction( | ||
networks[tx.networkId].api_url, | ||
tx.signedMessage | ||
) | ||
return { | ||
hash: hash, | ||
success: true | ||
} | ||
} catch (e) { | ||
return { | ||
error: e, | ||
success: false | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
estimate, | ||
broadcast | ||
} | ||
|
||
async function broadcastTransaction(url, signedTx) { | ||
// broadcast transaction with signatures included | ||
const body = createBroadcastBody(signedTx, `sync`) | ||
console.log('broadcast to:', url) | ||
// eslint-disable-next-line no-undef | ||
const res = await fetch(`${url}/txs`, { | ||
method: `POST`, | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body | ||
}) | ||
.then(res => res.json()) | ||
.then(assertOk) | ||
|
||
return res.txhash | ||
} | ||
|
||
function createBroadcastBody(signedTx, returnType = `sync`) { | ||
return JSON.stringify({ | ||
tx: signedTx, | ||
mode: returnType | ||
}) | ||
} | ||
|
||
function assertOk(res) { | ||
if (Array.isArray(res)) { | ||
if (res.length === 0) throw new Error(`Error sending transaction`) | ||
|
||
res.forEach(assertOk) | ||
} | ||
|
||
if (res.error) { | ||
throw new Error(res.error) | ||
} | ||
|
||
// Sometimes we get back failed transactions, which shows only by them having a `code` property | ||
if (res.code) { | ||
const message = JSON.parse(res.raw_log).message | ||
throw new Error(message) | ||
} | ||
|
||
if (!res.txhash) { | ||
const message = res.message | ||
throw new Error(message) | ||
} | ||
|
||
return res | ||
} |
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,48 @@ | ||
const cosmosapiV0 = require('../../../cosmos-api-v0-0.1.2/').default | ||
const cosmosapiV2 = require('../../../cosmos-api-v0-0.2.2/').default | ||
|
||
function getMessage(messageType, transactionProperties, context) { | ||
const messageConstructor = getMessageConstructor(context) | ||
const message = messageConstructor( | ||
messageType, | ||
context.userAddress, | ||
transactionProperties | ||
) | ||
return message | ||
} | ||
|
||
function getMessageConstructor({ networkId, url, chainId }) { | ||
switch (networkId) { | ||
case `local-cosmos-hub-testnet`: | ||
case `cosmos-hub-mainnet`: { | ||
const cosmos = new cosmosapiV0(url || '', chainId || '') | ||
return (messageType, userAddress, transactionProperties) => | ||
cosmos[messageType](userAddress, transactionProperties) | ||
} | ||
case `cosmos-hub-testnet`: { | ||
const cosmos = new cosmosapiV2(url || '', chainId || '') | ||
return (messageType, userAddress, transactionProperties) => | ||
cosmos[messageType](userAddress, transactionProperties) | ||
} | ||
} | ||
throw Error('Network is not supported for signing transactions.') | ||
} | ||
|
||
function getMultiMessage({ userAddress, networkId, url, chainId }, messages) { | ||
switch (networkId) { | ||
case `local-cosmos-hub-testnet`: | ||
case `cosmos-hub-mainnet`: { | ||
const cosmos = new cosmosapiV0(url || '', chainId || '') | ||
return cosmos.MultiMessage(userAddress, messages) | ||
} | ||
case `cosmos-hub-testnet`: { | ||
const cosmos = new cosmosapiV2(url || '', chainId || '') | ||
return cosmos.MultiMessage(userAddress, messages) | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
getMessage, | ||
getMultiMessage | ||
} |
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
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,12 @@ | ||
const config = require('../config') | ||
const networksMain = require('../data/networks.json') | ||
const networksLocal = require('../data/networks-local.js') | ||
|
||
let networks = networksMain | ||
if (config.enableTestnet) { | ||
networks = { ...networks, ...networksLocal } | ||
} | ||
|
||
module.exports = { | ||
networks | ||
} |
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
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
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,13 +1,25 @@ | ||
var express = require('express') | ||
var router = express.Router() | ||
var { estimate, broadcast } = require('./../controller/transaction') | ||
|
||
router.use(function timeLog(req, res, next) { | ||
console.log('Transaction Received', Date.now()) | ||
req.txRequest = req.body && req.body.payload | ||
if (req.txRequest) { | ||
console.log(`Transaction ${Date.now()} ${req.txRequest.messageType}`) | ||
} else { | ||
res.json({ error: 'No Request Found' }) | ||
} | ||
next() | ||
}) | ||
|
||
router.use('/', function(req, res) { | ||
res.send('Transaction API Request received') | ||
router.use('/estimate', async function(req, res) { | ||
const response = await estimate(req.txRequest) | ||
res.json(response) | ||
}) | ||
|
||
router.use('/broadcast', async function(req, res) { | ||
const response = await broadcast(req.txRequest) | ||
res.json(response) | ||
}) | ||
|
||
module.exports = router |
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
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