-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from dappforce/xcm
Add XCM
- Loading branch information
Showing
6 changed files
with
202 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ build/ | |
# Optional npm cache directory | ||
.npm | ||
.vscode | ||
.yarn | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
import { Storage } from '@acala-network/sdk/utils/storage' | ||
import { AnyApi, FixedPointNumber as FN } from '@acala-network/sdk-core' | ||
import { combineLatest, map, Observable } from 'rxjs' | ||
|
||
import { SubmittableExtrinsic } from '@polkadot/api/types' | ||
import { DeriveBalancesAll } from '@polkadot/api-derive/balances/types' | ||
import { ISubmittableResult } from '@polkadot/types/types' | ||
import { | ||
createPolkadotXCMAccount, | ||
createPolkadotXCMAsset, | ||
createPolkadotXCMDest, | ||
createRouteConfigs, | ||
validateAddress | ||
} from '@polkawallet/bridge/utils' | ||
import { BalanceData, BasicToken, ChainId, chains, TransferParams } from '@polkawallet/bridge' | ||
import { BalanceAdapter, BalanceAdapterConfigs } from '@polkawallet/bridge/balance-adapter' | ||
import { ApiNotFound, InvalidAddress, TokenNotFound } from '@polkawallet/bridge/errors' | ||
import { BaseCrossChainAdapter } from '@polkawallet/bridge/base-chain-adapter' | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore | ||
chains.subsocial = { | ||
id: 'subsocial', | ||
display: 'Subsocial', | ||
type: 'substrate', | ||
icon: 'https://sub.id/images/subsocial.svg', | ||
paraChainId: 2100, | ||
ss58Prefix: 28 | ||
} | ||
export const subsocialRouteConfigs = createRouteConfigs('subsocial' as any, [ | ||
{ | ||
to: 'hydradx', | ||
token: 'SUB', | ||
xcm: { | ||
fee: { token: 'SUB', amount: '63199000' }, | ||
}, | ||
}, | ||
]) | ||
|
||
export const subsocialTokensConfig: Record<string, BasicToken> = { | ||
SUB: { name: 'SUB', symbol: 'SUB', decimals: 10, ed: '100000000' }, | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
const createBalanceStorages = (api: AnyApi) => { | ||
return { | ||
balances: (address: string) => | ||
Storage.create<DeriveBalancesAll>({ | ||
api, | ||
path: 'derive.balances.all', | ||
params: [ address ], | ||
}), | ||
} | ||
} | ||
|
||
class SubsocialBalanceAdapter extends BalanceAdapter { | ||
private storages: ReturnType<typeof createBalanceStorages> | ||
|
||
constructor ({ api, chain, tokens }: BalanceAdapterConfigs) { | ||
super({ api, chain, tokens }) | ||
this.storages = createBalanceStorages(api) | ||
} | ||
|
||
public subscribeBalance ( | ||
token: string, | ||
address: string | ||
): Observable<BalanceData> { | ||
const storage = this.storages.balances(address) | ||
|
||
if (token !== this.nativeToken) { | ||
throw new TokenNotFound(token) | ||
} | ||
|
||
return storage.observable.pipe( | ||
map((data) => ({ | ||
free: FN.fromInner(data.freeBalance.toString(), this.decimals), | ||
locked: FN.fromInner(data.lockedBalance.toString(), this.decimals), | ||
reserved: FN.fromInner(data.reservedBalance.toString(), this.decimals), | ||
available: FN.fromInner( | ||
data.availableBalance.toString(), | ||
this.decimals | ||
), | ||
})) | ||
) | ||
} | ||
} | ||
|
||
class SubsocialBaseAdapter extends BaseCrossChainAdapter { | ||
private balanceAdapter?: SubsocialBalanceAdapter | ||
|
||
public async init (api: AnyApi) { | ||
this.api = api | ||
|
||
await api.isReady | ||
|
||
const chain = this.chain.id as ChainId | ||
|
||
this.balanceAdapter = new SubsocialBalanceAdapter({ | ||
chain, | ||
api, | ||
tokens: subsocialTokensConfig, | ||
}) | ||
} | ||
|
||
public subscribeTokenBalance ( | ||
token: string, | ||
address: string | ||
): Observable<BalanceData> { | ||
if (!this.balanceAdapter) { | ||
throw new ApiNotFound(this.chain.id) | ||
} | ||
|
||
return this.balanceAdapter.subscribeBalance(token, address) | ||
} | ||
|
||
public subscribeMaxInput ( | ||
token: string, | ||
address: string, | ||
to: ChainId | ||
): Observable<FN> { | ||
if (!this.balanceAdapter) { | ||
throw new ApiNotFound(this.chain.id) | ||
} | ||
|
||
return combineLatest({ | ||
txFee: this.estimateTxFee({ | ||
amount: FN.ZERO, | ||
to, | ||
token, | ||
address, | ||
signer: address, | ||
}), | ||
balance: this.balanceAdapter | ||
.subscribeBalance(token, address) | ||
.pipe(map((i) => i.available)), | ||
}).pipe( | ||
map(({ balance, txFee }) => { | ||
const tokenMeta = this.balanceAdapter?.getToken(token) | ||
const feeFactor = 1.2 | ||
const fee = FN.fromInner(txFee, tokenMeta?.decimals).mul( | ||
new FN(feeFactor) | ||
) | ||
|
||
// always minus ed | ||
return balance | ||
.minus(fee) | ||
.minus(FN.fromInner(tokenMeta?.ed || '0', tokenMeta?.decimals)) | ||
}) | ||
) | ||
} | ||
|
||
public createTx ( | ||
params: TransferParams | ||
): | ||
| SubmittableExtrinsic<'promise', ISubmittableResult> | ||
| SubmittableExtrinsic<'rxjs', ISubmittableResult> { | ||
if (!this.api) throw new ApiNotFound(this.chain.id) | ||
|
||
const { address, amount, to, token } = params | ||
|
||
if (!validateAddress(address)) throw new InvalidAddress(address) | ||
|
||
const toChain = chains[to] | ||
|
||
if (token !== this.balanceAdapter?.nativeToken) { | ||
throw new TokenNotFound(token) | ||
} | ||
|
||
const accountId = this.api?.createType('AccountId32', address).toHex() | ||
const paraChainId = toChain.paraChainId | ||
const rawAmount = amount.toChainData() | ||
|
||
return this.api?.tx.polkadotXcm.limitedReserveTransferAssets( | ||
createPolkadotXCMDest(this.api, paraChainId), | ||
createPolkadotXCMAccount(this.api, accountId), | ||
createPolkadotXCMAsset(this.api, rawAmount, 'NATIVE'), | ||
0, | ||
this.getDestWeight(token, to)?.toString() as any | ||
) | ||
} | ||
} | ||
|
||
export class SubsocialAdapter extends SubsocialBaseAdapter { | ||
constructor () { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore | ||
super(chains.subsocial, subsocialRouteConfigs, subsocialTokensConfig) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters