• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

blockcoders / xcm-sdk / 9b76bb81-d93b-4a4c-83ca-13c110a708d6

pending completion
9b76bb81-d93b-4a4c-83ca-13c110a708d6

Pull #12

circleci

Jose Ramirez
Add badges
Pull Request #12: Milestone2

66 of 80 branches covered (82.5%)

Branch coverage included in aggregate %.

20 of 20 new or added lines in 2 files covered. (100.0%)

95 of 96 relevant lines covered (98.96%)

16.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.27
/src/provider.ts
1
import { ApiPromise, WsProvider } from '@polkadot/api'
3✔
2
import { Signer } from '@polkadot/types/types'
3
import { AddressOrPair, MultiLocationTypes, ExtrinsicParam } from './interfaces/generics'
4
import { TransferAssetsProps, LimitedTransferAssetsProps } from './interfaces/methods'
5
import { getPallet, parseGenericBody } from './utils'
3✔
6
import { makeXcmVersionedMultiLocation, makeAsssetMultiAsset, formatExtrinsicResponse } from './utils'
3✔
7

8
export class Provider {
3✔
9
  rpc: string
10
  signer: AddressOrPair
11
  injectorSigner: Signer | null = null
48✔
12

13
  constructor(rpc: string, signer: AddressOrPair) {
14
    this.rpc = rpc
48✔
15
    this.signer = signer
48✔
16
  }
17

18
  private async getApi() {
19
    const api = await ApiPromise.create({
42✔
20
      provider: new WsProvider(this.rpc),
21
    })
22

23
    if (this.injectorSigner) {
42✔
24
      api.setSigner(this.injectorSigner)
3✔
25
    }
26

27
    return api
42✔
28
  }
29

30
  private prepareExtrinsic(props: LimitedTransferAssetsProps) {
31
    const {
32
      destination: _destination,
33
      destinationValue: _destinationValue,
34
      destinationParents: _destinationParents,
35
      beneficiary: _beneficiary,
36
      beneficiaryValue: _beneficiaryValue,
37
      amount: _amount,
38
      feeAssetItem: _feeAssetItem,
39
      assetParents: _assetParents,
40
      weightLimit: _weightLimit,
41
      assetId: _assetId,
42
    } = props
24✔
43

44
    let dest = null
24✔
45
    let beneficiary = null
24✔
46
    let assets = null
24✔
47
    let feeAssetItem = null
24✔
48
    let weightLimit = null
24✔
49

50
    dest = makeXcmVersionedMultiLocation({
24✔
51
      target: _destination as MultiLocationTypes,
52
      value: _destinationValue,
53
      parents: _destinationParents,
54
    })
55

56
    beneficiary = makeXcmVersionedMultiLocation({
24✔
57
      target: _beneficiary as MultiLocationTypes,
58
      value: _beneficiaryValue,
59
    })
60

61
    assets = makeAsssetMultiAsset({
24✔
62
      amount: _amount,
63
      parents: _assetParents,
64
      assetId: _assetId,
65
    })
66

67
    feeAssetItem = _feeAssetItem || 0
24✔
68

69
    weightLimit = _weightLimit ? { Limited: _weightLimit } : 'Unlimited'
24!
70

71
    return {
24✔
72
      dest,
73
      beneficiary,
74
      assets,
75
      feeAssetItem,
76
      weightLimit,
77
    }
78
  }
79

80
  public async setInjectorSigner(signer: Signer) {
81
    this.injectorSigner = signer
6✔
82
  }
83

84
  public async reserveTransferAssets(props: TransferAssetsProps) {
85
    const api = await this.getApi()
6✔
86

87
    const pallet = getPallet(api)
6✔
88

89
    if (!api.tx[pallet]?.reserveTransferAssets) throw new Error('No reserveTransferAssets method found')
6!
90

91
    const { dest, beneficiary, assets, feeAssetItem } = this.prepareExtrinsic(props)
3✔
92

93
    return new Promise(async (res, rej) => {
3✔
94
      api.tx[pallet]
3!
95
        ?.reserveTransferAssets(dest, beneficiary, assets, feeAssetItem)
96
        .signAndSend(this.signer, ({ status, txHash, dispatchError, dispatchInfo }: any) => {
97
          formatExtrinsicResponse({
3✔
98
            api,
99
            res,
100
            rej,
101
            status,
102
            txHash,
103
            dispatchError,
104
            dispatchInfo,
105
          })
106
        })
107
    })
108
  }
109

110
  public async limitedReserveTransferAssets(props: LimitedTransferAssetsProps) {
111
    const api = await this.getApi()
6✔
112
    const pallet = getPallet(api)
6✔
113

114
    if (!api.tx[pallet]?.limitedReserveTransferAssets) throw new Error('No limitedReserveTransferAssets method found')
6!
115

116
    const { dest, beneficiary, assets, feeAssetItem, weightLimit } = this.prepareExtrinsic(props)
3✔
117

118
    return new Promise(async (res, rej) => {
3✔
119
      api.tx[pallet]
3!
120
        ?.limitedReserveTransferAssets(dest, beneficiary, assets, feeAssetItem, weightLimit)
121
        .signAndSend(this.signer, ({ status, txHash, dispatchError, dispatchInfo }: any) => {
122
          formatExtrinsicResponse({
3✔
123
            api,
124
            res,
125
            rej,
126
            status,
127
            txHash,
128
            dispatchError,
129
            dispatchInfo,
130
          })
131
        })
132
    })
133
  }
134

135
  public async teleportAssets(props: TransferAssetsProps) {
136
    const api = await this.getApi()
6✔
137

138
    const pallet = getPallet(api)
6✔
139

140
    if (!api.tx[pallet]?.teleportAssets) throw new Error('No teleportAssets method found')
6!
141

142
    const { dest, beneficiary, assets, feeAssetItem } = this.prepareExtrinsic(props)
3✔
143

144
    return new Promise(async (res, rej) => {
3✔
145
      api.tx[pallet]
3!
146
        ?.teleportAssets(dest, beneficiary, assets, feeAssetItem)
147
        .signAndSend(this.signer, ({ status, txHash, dispatchError, dispatchInfo }: any) => {
148
          formatExtrinsicResponse({
3✔
149
            api,
150
            res,
151
            rej,
152
            status,
153
            txHash,
154
            dispatchError,
155
            dispatchInfo,
156
          })
157
        })
158
    })
159
  }
160

161
  public async limitedTeleportAssets(props: LimitedTransferAssetsProps) {
162
    const api = await this.getApi()
18✔
163

164
    const pallet = getPallet(api)
18✔
165

166
    if (!api.tx[pallet]?.limitedTeleportAssets) throw new Error('No limitedTeleportAssets method found')
18!
167

168
    const { dest, beneficiary, assets, feeAssetItem, weightLimit } = this.prepareExtrinsic(props)
15✔
169

170
    return new Promise(async (res, rej) => {
15✔
171
      api.tx[pallet]
15!
172
        ?.limitedTeleportAssets(dest, beneficiary, assets, feeAssetItem, weightLimit)
173
        .signAndSend(this.signer, ({ status, txHash, dispatchError, dispatchInfo }: any) => {
174
          formatExtrinsicResponse({
15✔
175
            api,
176
            res,
177
            rej,
178
            status,
179
            txHash,
180
            dispatchError,
181
            dispatchInfo,
182
          })
183
        })
184
    })
185
  }
186

187
  public async customExtrinsic(props: ExtrinsicParam) {
188
    const api = await this.getApi()
6✔
189
    const pallet = getPallet(api, props.pallet)
6✔
190

191
    if (!api.tx[pallet][props.method]) {
6✔
192
      throw new Error(`${props.method} method unsupported`)
3✔
193
    }
194

195
    const body = parseGenericBody(props.body)
3✔
196

197
    let extrinsic = api.tx[pallet][props.method](...body)
3✔
198

199
    if (props.asSudo) {
3!
200
      extrinsic = api.tx.sudo.sudo(extrinsic)
×
201
    }
202

203
    return new Promise(async (res, rej) => {
3✔
204
      extrinsic.signAndSend(this.signer, ({ status, txHash, dispatchError, dispatchInfo }: any) => {
3✔
205
        formatExtrinsicResponse({
3✔
206
          api,
207
          res,
208
          rej,
209
          status,
210
          txHash,
211
          dispatchError,
212
          dispatchInfo,
213
        })
214
      })
215
    })
216
  }
217
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc