Skip to content

Commit

Permalink
Implement getMessageDetails endpoint in Filfox to include fee data
Browse files Browse the repository at this point in the history
  • Loading branch information
samholmes committed Sep 8, 2023
1 parent ec219ca commit 0fab2a2
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/filecoin/FilecoinEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
FilecoinWalletOtherData,
SafeFilecoinWalletInfo
} from './filecoinTypes'
import { Filfox, FilfoxMessage } from './Filfox'
import { Filfox, FilfoxMessageDetailed } from './Filfox'
import { Filscan, FilscanMessage } from './Filscan'
import { RpcExtra } from './RpcExtra'

Expand Down Expand Up @@ -392,7 +392,10 @@ export class FilecoinEngine extends CurrencyEngine<
if (message.height < this.walletLocalData.lastAddressQueryHeight) return

// Process message into a transaction
const tx = this.filfoxMessageToEdgeTransaction(message)
const messageDetails = await this.filfoxApi.getMessageDetails(
message.cid
)
const tx = this.filfoxMessageToEdgeTransaction(messageDetails)

// Calculate the progress
const progress =
Expand Down Expand Up @@ -441,25 +444,32 @@ export class FilecoinEngine extends CurrencyEngine<
}

filfoxMessageToEdgeTransaction = (
message: FilfoxMessage
messageDetails: FilfoxMessageDetailed
): EdgeTransaction => {
const addressString = this.address.toString()
let netNativeAmount = message.value
let netNativeAmount = messageDetails.value
const ourReceiveAddresses = []

const networkFee = '0' // TODO: calculate transaction fee from onchain gas fields
if (message.to !== addressString) {
// Get the fees paid
const networkFee = messageDetails.transfers
.filter(
transfer =>
transfer.type === 'miner-fee' || transfer.type === 'burner-fee'
)
.reduce((sum, transfer) => add(sum, transfer.value), '0')

if (messageDetails.to !== addressString) {
// check if tx is a spend
netNativeAmount = `-${add(netNativeAmount, networkFee)}`
} else {
ourReceiveAddresses.push(addressString)
}

const edgeTransaction: EdgeTransaction = {
txid: message.cid,
date: message.timestamp,
txid: messageDetails.cid,
date: messageDetails.timestamp,
currencyCode: this.currencyInfo.currencyCode,
blockHeight: message.height,
blockHeight: messageDetails.height,
nativeAmount: netNativeAmount,
isSend: netNativeAmount.startsWith('-'),
networkFee,
Expand Down
67 changes: 67 additions & 0 deletions src/filecoin/Filfox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@ export const asFilfoxMessage = asObject({
value: asString
})

export type FilfoxMessageDetailed = ReturnType<typeof asFilfoxMessageDetailed>
export const asFilfoxMessageDetailed = asObject({
cid: asString,
height: asNumber,
timestamp: asNumber,
from: asString,
to: asString,
value: asString,
gasLimit: asNumber,
gasFeeCap: asString,
gasPremium: asString,
receipt: asObject({
exitCode: asNumber,
return: asString,
gasUsed: asNumber
}),
baseFee: asString,
fee: asObject({
baseFeeBurn: asString,
overEstimationBurn: asString,
minerPenalty: asString,
minerTip: asString,
refund: asString
}),
transfers: asArray(
asObject({
from: asString,
fromId: asString,
to: asString,
toId: asString,
value: asString,
type: asString
})
)
})

//
// Messages
//
Expand All @@ -58,6 +94,15 @@ export const asFilfoxMessagesResult = asObject({
totalCount: asNumber
})

//
// Message Details
//

export type FilfoxMessageDetailsResult = ReturnType<
typeof asFilfoxMessageDetailsResult
>
export const asFilfoxMessageDetailsResult = asFilfoxMessageDetailed

// -----------------------------------------------------------------------------
// Implementation
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -98,4 +143,26 @@ export class Filfox {

return responseBody
}

async getMessageDetails(
messageCid: string
): Promise<FilfoxMessageDetailsResult> {
const response = await this.fetch(`${this.baseUrl}/message/${messageCid}`, {
method: 'GET',
headers: {
'content-type': 'application/json'
}
})
const responseText = await response.text()
const responseBody = asFilfoxEnvelope(asFilfoxMessageDetailsResult)(
responseText
)

if ('error' in responseBody)
throw new Error(
`Error response code ${responseBody.statusCode}: ${responseBody.message} ${responseBody.error}`
)

return responseBody
}
}

0 comments on commit 0fab2a2

Please sign in to comment.