Skip to content

Commit

Permalink
fix: use snap config to format fil amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Jul 24, 2023
1 parent 7996524 commit eba19be
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 114 deletions.
4 changes: 2 additions & 2 deletions examples/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"clsx": "^2.0.0",
"filsnap-adapter": "workspace:^",
"iso-base": "^1.1.1",
"iso-filecoin": "^1.3.0",
"iso-filecoin": "^2.0.1",
"metamask-testing-tools": "^1.1.4",
"preact": "^10.16.0",
"react-hook-form": "^7.45.2",
Expand All @@ -65,7 +65,7 @@
"@playwright/test": "^1.36.1",
"@preact/preset-vite": "^2.5.0",
"@types/node": "^20.4.4",
"vite": "^4.4.6"
"vite": "^4.4.7"
},
"eslintConfig": {
"extends": [
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/src/components/connect.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default function Connect() {
<div title={account.balance + ' attoFIL'}>
<b>
{account
? Token.fromAttoFIL(account.balance).toFIL().toFormat(18)
? Token.fromAttoFIL(account.balance).toFIL().toFormat()
: 'unknown'}{' '}
FIL
</b>
Expand Down
5 changes: 3 additions & 2 deletions examples/demo/src/components/send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ function Send() {
)

const total = Token.fromFIL(amount).add(gas)
setEstimate(gas.toFIL().toFormat(10))
setTotal(total.toFIL().toFormat(10))
setEstimate(gas.toFIL().toFormat())
setTotal(total.toFIL().toFormat())
setIsEstimating(false)
}
})
Expand Down Expand Up @@ -122,6 +122,7 @@ function Send() {
Amount
</label>
<input
placeholder="FIL"
disabled={isLoading || isSending}
{...register('amount', { required: true })}
/>
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"path": "../../packages/adapter"
}
],
"include": ["src", "test"],
// "include": ["**/*.{jsx,js,ts,tsx}"],
"exclude": ["node_modules", "dist", "out"]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"hd-scripts": "^7.0.0",
"lint-staged": "^13.2.3",
"prettier": "3.0.0",
"simple-git-hooks": "^2.8.1",
"simple-git-hooks": "^2.9.0",
"typedoc": "^0.24.8",
"typedoc-plugin-expand-object-like-types": "^0.1.2",
"typedoc-plugin-missing-exports": "^2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@
}
},
"dependencies": {
"@metamask/key-tree": "^7.1.1",
"@metamask/key-tree": "^9.0.0",
"@metamask/snaps-ui": "^0.32.2",
"iso-base": "^1.1.1",
"iso-filecoin": "^1.3.0",
"iso-filecoin": "^2.0.1",
"merge-options": "^3.0.4",
"zod": "^3.21.4"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/filecoin-project/filsnap.git"
},
"source": {
"shasum": "ItKS9vWUjt8rOCzAHM5k2qI7rGzJBZQ8YuDJKqtC2D4=",
"shasum": "tyJztEVhbticwDWbe6rN1ESFDMtz/jyrfAD0ovISqgo=",
"location": {
"npm": {
"filePath": "dist/snap.js",
Expand Down
11 changes: 5 additions & 6 deletions packages/snap/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export const mainnetConfig: SnapConfig = {
url: 'https://api.node.glif.io',
},
unit: {
decimals: 6,
image: `https://cryptologos.cc/logos/filecoin-fil-logo.svg?v=007`,
decimals: 18,
image: `https://filecoin.io/images/filecoin-logo.svg`,
symbol: 'FIL',
},
}
Expand All @@ -22,10 +22,9 @@ export const testnetConfig: SnapConfig = {
url: `https://api.calibration.node.glif.io`,
},
unit: {
decimals: 6,
image: `https://cryptologos.cc/logos/filecoin-fil-logo.svg?v=007`,
symbol: 'FIL',
// custom view url ?
decimals: 18,
image: `https://filecoin.io/images/filecoin-logo.svg`,
symbol: 'tFIL',
},
}

Expand Down
20 changes: 14 additions & 6 deletions packages/snap/src/rpc/sign-message.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/restrict-template-expressions */
import { copyable, divider, heading, panel, text } from '@metamask/snaps-ui'
import { base64pad } from 'iso-base/rfc4648'
import * as Address from 'iso-filecoin/address'
import { Message, Schemas } from 'iso-filecoin/message'
import { Token } from 'iso-filecoin/token'
import * as Address from 'iso-filecoin/address'
import { signMessage as filSignMessage, sign } from 'iso-filecoin/wallet'
import { z } from 'zod'
import type { SignedMessage, SnapContext, SnapResponse } from '../types'
Expand Down Expand Up @@ -68,14 +68,22 @@ export async function signMessage(
const conf = await snapDialog(ctx.snap, {
type: 'confirmation',
content: panel([
heading(
`Send ${Token.fromAttoFIL(message.value).toFIL().toFormat(10)} FIL to`
),
heading(`Send ${Token.fromAttoFIL(message.value).toFIL().toString()} to`),
copyable(message.to),
divider(),
heading('Details'),
text(`Gas _(estimated)_: **${gas.toFIL().toFormat(10)} FIL**`),
text(`Total _(amount + gas)_: **${total.toFIL().toFormat(10)} FIL**`),
text(
`Gas _(estimated)_: **${gas.toFIL().toFormat({
decimalPlaces: ctx.config.unit?.decimals,
suffix: ` ${ctx.config.unit?.symbol}`,
})}**`
),
text(
`Total _(amount + gas)_: **${total.toFIL().toFormat({
decimalPlaces: ctx.config.unit?.decimals,
suffix: ` ${ctx.config.unit?.symbol}`,
})}**`
),
]),
})

Expand Down

0 comments on commit eba19be

Please sign in to comment.