Skip to content

Commit

Permalink
Fabo/broadcast polkadot txs (#461)
Browse files Browse the repository at this point in the history
* first steps to broadcast polkadot txs

* remove package-lock

* fixed api init

* remove debugger

* working broadcasting

* working

* ignore polkadot in tests

* Update lib/controller/transaction/index.js

Co-Authored-By: Jordan Bibla <jbibla@gmail.com>

Co-authored-by: Jordan Bibla <jbibla@gmail.com>
  • Loading branch information
faboweb and jbibla authored Mar 20, 2020
1 parent 1ff18d5 commit cf30587
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 24 deletions.
106 changes: 83 additions & 23 deletions lib/controller/transaction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,27 @@ const {
defineActionDenom,
defineActionValue
} = require('../../statistics')
const { ApiPromise, WsProvider } = require('@polkadot/api')

global.fetch = require('node-fetch')

let api
async function initPolkadotAPI() {
// ignore polkadot in tests for now
if (process.env.TEST) return

api = new ApiPromise({
provider: new WsProvider(networkMap['polkadot-testnet'].rpc_url)
})
await api.isReady
}
initPolkadotAPI()

async function getPolkadotAPI() {
await api.isReady
return api
}

async function estimate() {
// const context = {
// userAddress: tx.address,
Expand Down Expand Up @@ -40,30 +58,67 @@ async function estimate() {
}
}

async function broadcastWithCosmos(tx, fingerprint) {
const hash = await broadcastCosmosTransaction(
tx.networkId,
tx.senderAddress,
networkMap[tx.networkId].api_url,
tx.signedMessage
)
// presaving to the database
prestore(
{
network: tx.networkId,
address: tx.senderAddress,
action: defineActionType(tx.signedMessage.msg[0].type),
value: defineActionValue(tx.signedMessage.msg),
denom: defineActionDenom(tx.signedMessage.msg),
fingerprint
},
hash
)
return {
hash: hash,
success: true
}
}

async function broadcastWithPolkadot(
tx
// fingerprint
) {
const api = await getPolkadotAPI()

const result = await api.rpc.author.submitExtrinsic(tx.signedMessage)

const hash = result.toJSON()
// TODO presaving to the database
// prestore(
// {
// network: tx.networkId,
// address: tx.senderAddress,
// action: tx.messageType,
// // value: defineActionValue(tx.signedMessage.msg),
// // denom: defineActionDenom(tx.signedMessage.msg),
// fingerprint
// },
// hash
// )

return {
hash,
success: true
}
}

async function broadcast(tx, fingerprint) {
console.log(`Received broadcast: ${JSON.stringify(tx)}`)
try {
const hash = await broadcastTransaction(
tx.networkId,
tx.senderAddress,
networkMap[tx.networkId].api_url,
tx.signedMessage
)
// presaving to the database
prestore(
{
network: tx.networkId,
address: tx.senderAddress,
action: defineActionType(tx.signedMessage.msg[0].type),
value: defineActionValue(tx.signedMessage.msg),
denom: defineActionDenom(tx.signedMessage.msg),
fingerprint
},
hash
)
return {
hash: hash,
success: true
switch (networkMap[tx.networkId].network_type) {
case 'cosmos':
return await broadcastWithCosmos(tx, fingerprint)
case 'polkadot':
return await broadcastWithPolkadot(tx, fingerprint)
}
} catch (e) {
Sentry.withScope(function(scope) {
Expand All @@ -83,8 +138,13 @@ module.exports = {
broadcast
}

// TODO implment broadcasting across network types
async function broadcastTransaction(networkId, senderAddress, url, signedTx) {
// TODO implement broadcasting across network types
async function broadcastCosmosTransaction(
networkId,
senderAddress,
url,
signedTx
) {
// broadcast transaction with signatures included
// `block` means we wait for the tx to be included into a block before returning. this helps with figuring out "out of gas" issues which only appear when the block is created
const body = createBroadcastBody(signedTx, `sync`)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"lint-fix": "yarn lint --fix",
"release": "git fetch --all && git checkout origin/develop -B develop && git pull && git checkout origin/release -B release && git pull && git merge origin/develop && git push",
"start": "node index.js ",
"test": "jest"
"test": "cross-env TEST=true jest"
},
"husky": {
"hooks": {
Expand Down

0 comments on commit cf30587

Please sign in to comment.