@forevermoney/sdk 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/README.md +27 -5
- package/dist/index.cjs +350 -223
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +49 -27
- package/dist/index.d.ts +49 -27
- package/dist/index.js +356 -206
- package/dist/index.js.map +1 -1
- package/examples/viem.ts +64 -0
- package/package.json +5 -6
package/examples/viem.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Chain, PublicClient, Transport, WalletClient } from 'viem'
|
|
2
|
+
import {
|
|
3
|
+
createForeverMoneyClient,
|
|
4
|
+
toViemTransaction,
|
|
5
|
+
type BaseToSubtensorRequest,
|
|
6
|
+
} from '@forevermoney/sdk'
|
|
7
|
+
|
|
8
|
+
/** Reuse a wallet application's existing viem transports and signer. */
|
|
9
|
+
export async function bridgeWithViem(options: {
|
|
10
|
+
base: PublicClient
|
|
11
|
+
subtensor: PublicClient
|
|
12
|
+
wallet: WalletClient<Transport, Chain>
|
|
13
|
+
request: BaseToSubtensorRequest
|
|
14
|
+
}) {
|
|
15
|
+
const sdk = createForeverMoneyClient({
|
|
16
|
+
transports: {
|
|
17
|
+
base: options.base.transport,
|
|
18
|
+
subtensor: options.subtensor.transport,
|
|
19
|
+
},
|
|
20
|
+
})
|
|
21
|
+
await sdk.verifyConnections()
|
|
22
|
+
const prepared = await sdk.bridge.prepareBaseToSubtensor(options.request)
|
|
23
|
+
let checkpoint
|
|
24
|
+
let bridgeHash
|
|
25
|
+
for (const step of prepared.plan.steps) {
|
|
26
|
+
const transaction = toViemTransaction(step.transaction)
|
|
27
|
+
const [chainId, accounts] = await Promise.all([
|
|
28
|
+
options.wallet.getChainId(),
|
|
29
|
+
options.wallet.getAddresses(),
|
|
30
|
+
])
|
|
31
|
+
if (
|
|
32
|
+
chainId !== transaction.chainId ||
|
|
33
|
+
accounts[0]?.toLowerCase() !== transaction.account.toLowerCase()
|
|
34
|
+
) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
'The signing account or chain changed. Re-prepare the bridge.'
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
if (step.kind === 'transaction')
|
|
40
|
+
checkpoint =
|
|
41
|
+
await sdk.bridge.getDeliveryCheckpoint('base-to-subtensor')
|
|
42
|
+
const hash = await options.wallet.sendTransaction({
|
|
43
|
+
...transaction,
|
|
44
|
+
})
|
|
45
|
+
const receipt = await options.base.waitForTransactionReceipt({ hash })
|
|
46
|
+
if (receipt.status !== 'success')
|
|
47
|
+
throw new Error(`Transaction failed: ${hash}`)
|
|
48
|
+
if (step.kind === 'transaction') bridgeHash = hash
|
|
49
|
+
}
|
|
50
|
+
if (!checkpoint || !bridgeHash)
|
|
51
|
+
throw new Error('Missing bridge transaction or checkpoint.')
|
|
52
|
+
const source = await sdk.bridge.getSourceStatus({
|
|
53
|
+
direction: 'base-to-subtensor',
|
|
54
|
+
transactionHash: bridgeHash,
|
|
55
|
+
})
|
|
56
|
+
if (source.status !== 'confirmed') return { source, checkpoint }
|
|
57
|
+
// Poll again from this checkpoint while waiting; recovery requires a claim flow.
|
|
58
|
+
const delivery = await sdk.bridge.getDeliveryStatus({
|
|
59
|
+
direction: checkpoint.direction,
|
|
60
|
+
messageId: source.messageId,
|
|
61
|
+
fromBlock: checkpoint.fromBlock,
|
|
62
|
+
})
|
|
63
|
+
return { source, checkpoint, delivery }
|
|
64
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forevermoney/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Typed bridge and vault integration SDK for ForeverMoney",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -52,11 +52,6 @@
|
|
|
52
52
|
"canary": "npm run build && node scripts/live-canary.mjs",
|
|
53
53
|
"prepack": "npm run verify"
|
|
54
54
|
},
|
|
55
|
-
"dependencies": {
|
|
56
|
-
"@polkadot/util": "^13.5.9",
|
|
57
|
-
"@polkadot/util-crypto": "^13.5.9",
|
|
58
|
-
"ethers": "^6.12.2"
|
|
59
|
-
},
|
|
60
55
|
"devDependencies": {
|
|
61
56
|
"@types/node": "^22.4.1",
|
|
62
57
|
"fast-check": "^4.9.0",
|
|
@@ -70,5 +65,9 @@
|
|
|
70
65
|
},
|
|
71
66
|
"publishConfig": {
|
|
72
67
|
"access": "public"
|
|
68
|
+
},
|
|
69
|
+
"dependencies": {
|
|
70
|
+
"@polkadot-api/substrate-bindings": "0.21.1",
|
|
71
|
+
"viem": "2.56.3"
|
|
73
72
|
}
|
|
74
73
|
}
|