Skip to content

Commit

Permalink
Merge pull request #26 from interlay/tom/usdt-support
Browse files Browse the repository at this point in the history
Tom/usdt support
  • Loading branch information
tomjeatt committed Dec 12, 2022
2 parents 40200b7 + 6693a33 commit 1083856
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 48 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@interlay/bridge",
"version": "0.1.8",
"version": "0.1.9",
"description": "polkawallet bridge sdk",
"main": "build/index.js",
"typings": "build/index.d.ts",
Expand Down
129 changes: 129 additions & 0 deletions src/adapters/interlay.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { FixedPointNumber } from "@acala-network/sdk-core";
import { firstValueFrom } from "rxjs";

import { ApiProvider } from "../api-provider";
import { chains, ChainName } from "../configs";
import { Bridge } from "..";
import { PolkadotAdapter } from "./polkadot";
import { InterlayAdapter } from "./interlay";
// import { StatemintAdapter } from "./statemint";

describe("interlay-adapter should work", () => {
jest.setTimeout(30000);

const testAccount = "5GREeQcGHt7na341Py6Y6Grr38KUYRvVoiFSiDB52Gt7VZiN";
const provider = new ApiProvider("mainnet");

async function connect(chains: ChainName[]) {
// return firstValueFrom(provider.connectFromChain([chain], { karura: ["wss://crosschain-dev.polkawallet.io:9907"] }));
return firstValueFrom(provider.connectFromChain(chains, undefined));
}

test("connect interlay to do xcm", async () => {
// const fromChains = ["interlay", "polkadot", "statemint"] as ChainName[];
const fromChains = ["interlay", "polkadot"] as ChainName[];

await connect(fromChains);

const interlay = new InterlayAdapter();
const polkadot = new PolkadotAdapter();
// const statemint = new StatemintAdapter();

await interlay.setApi(provider.getApi(fromChains[0]));
await polkadot.setApi(provider.getApi(fromChains[1]));
// await statemint.setApi(provider.getApi(fromChains[2]));

// const bridge = new Bridge({
// adapters: [interlay, polkadot, statemint],
// });

const bridge = new Bridge({
adapters: [interlay, polkadot],
});

expect(
bridge.router.getDestinationChains({
from: chains.interlay,
token: "DOT",
}).length
).toEqual(1);

// expect(
// bridge.router.getDestinationChains({ from: chains.interlay, token: "USDT" })
// .length
// ).toEqual(1);

const adapter = bridge.findAdapter(fromChains[0]);

async function runMyTestSuit(to: ChainName, token: string) {
if (adapter) {
const balance = await firstValueFrom(
adapter.subscribeTokenBalance(token, testAccount)
);

console.log(
`balance ${token}: free-${balance.free.toNumber()} locked-${balance.locked.toNumber()} available-${balance.available.toNumber()}`
);
expect(balance.available.toNumber()).toBeGreaterThanOrEqual(0);
expect(balance.free.toNumber()).toBeGreaterThanOrEqual(
balance.available.toNumber()
);
expect(balance.free.toNumber()).toEqual(
balance.locked.add(balance.available).toNumber()
);

const inputConfig = await firstValueFrom(
adapter.subscribeInputConfigs({
to,
token,
address: testAccount,
signer: testAccount,
})
);

console.log(
`inputConfig: min-${inputConfig.minInput.toNumber()} max-${inputConfig.maxInput.toNumber()} ss58-${
inputConfig.ss58Prefix
}`
);
expect(inputConfig.minInput.toNumber()).toBeGreaterThan(0);
expect(inputConfig.maxInput.toNumber()).toBeLessThanOrEqual(
balance.available.toNumber()
);

const destFee = adapter.getCrossChainFee(token, to);

console.log(
`destFee: fee-${destFee.balance.toNumber()} ${destFee.token}`
);
if (to != "polkadot") {
expect(destFee.balance.toNumber()).toBeGreaterThan(0);
} else {
expect(destFee.balance.toNumber()).toEqual(0.1);
}

const tx = adapter.createTx({
amount: FixedPointNumber.fromInner("10000000000", 10),
to,
token,
address: testAccount,
signer: testAccount,
});

expect(tx.method.section).toEqual("xTokens");
// expect(tx.args.length).toEqual(4);
// if (to === "statemint") {
// expect(tx.method.method).toEqual("transferMulticurrencies");
// } else {
// expect(tx.method.method).toEqual("transfer");
// }

expect(tx.args.length).toEqual(4);
expect(tx.method.method).toEqual("transfer");
}
}

await runMyTestSuit("polkadot", "DOT");
// await runMyTestSuit("statemint", "USDT");
});
});
34 changes: 29 additions & 5 deletions src/adapters/interlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ export const interlayRoutersConfig: Omit<CrossChainRouterConfigs, "from">[] = [
{
to: "polkadot",
token: "DOT",
xcm: { fee: { token: "DOT", amount: "0" }, weightLimit: DEST_WEIGHT },
xcm: {
fee: { token: "DOT", amount: "1000000000" },
weightLimit: DEST_WEIGHT,
},
},
// {
// to: "statemint",
// token: "USDT",
// // todo: determine fee amount - current value is a placeholder
// xcm: { fee: { token: "USDT", amount: "10" }, weightLimit: DEST_WEIGHT },
// xcm: {
// fee: { token: "DOT", amount: "1000000000" },
// weightLimit: DEST_WEIGHT,
// },
// },
];

Expand Down Expand Up @@ -64,12 +69,12 @@ export const interlayTokensConfig: Record<

const KINTSUGI_SUPPORTED_TOKENS: Record<string, unknown> = {
KSM: { Token: "KSM" },
USDT: { ForeignAsset: 3 },
// USDT: { ForeignAsset: 3 },
};

const INTERLAY_SUPPORTED_TOKENS: Record<string, unknown> = {
DOT: { Token: "DOT" },
USDT: { ForeignAsset: 2 },
// USDT: { ForeignAsset: 2 },
};

const getSupportedTokens = (chainname: string): Record<string, unknown> => {
Expand Down Expand Up @@ -232,6 +237,25 @@ class BaseInterlayAdapter extends BaseCrossChainAdapter {
};
}

if (
isChainEqual(toChain, "statemine") ||
isChainEqual(toChain, "statemint")
) {
const destFee = this.getCrossChainFee(token, to);
const destWeight = this.getDestWeight(token, to);

// do the needful, use multi currencies
return this.api.tx.xTokens.transferMulticurrencies(
[
[tokenId, amount.toChainData()],
[{ Token: destFee.token }, destFee.balance.toChainData()],
],
1,
{ V1: dst },
destWeight?.toString()
);
}

return this.api.tx.xTokens.transfer(
tokenId,
amount.toChainData(),
Expand Down
44 changes: 2 additions & 42 deletions src/adapters/statemint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ import {
} from "../types";

export const statemintRoutersConfig: Omit<CrossChainRouterConfigs, "from">[] = [
{
to: "polkadot",
token: "DOT",
xcm: {
// TODO: fee might need tweaking, to be checked in tests
fee: { token: "DOT", amount: "3549633" },
weightLimit: "Unlimited",
},
},
{
to: "interlay",
token: "USDT",
Expand All @@ -36,35 +27,6 @@ export const statemintRoutersConfig: Omit<CrossChainRouterConfigs, "from">[] = [
];

export const statemineRoutersConfig: Omit<CrossChainRouterConfigs, "from">[] = [
{
to: "kusama",
token: "KSM",
xcm: {
fee: { token: "KSM", amount: "106666660" },
weightLimit: "Unlimited",
},
},
{
to: "karura",
token: "RMRK",
xcm: {
fee: { token: "RMRK", amount: "6400000" },
weightLimit: "Unlimited",
},
},
{
to: "karura",
token: "ARIS",
xcm: {
fee: { token: "ARIS", amount: "6400000" },
weightLimit: "Unlimited",
},
},
{
to: "karura",
token: "USDT",
xcm: { fee: { token: "USDT", amount: "640" }, weightLimit: "Unlimited" },
},
{
to: "kintsugi",
token: "USDT",
Expand All @@ -78,13 +40,11 @@ export const statemineTokensConfig: Record<
> = {
statemine: {
KSM: { name: "KSM", symbol: "KSM", decimals: 12, ed: "3333333" },
RMRK: { name: "RMRK", symbol: "RMRK", decimals: 10, ed: "100000000" },
ARIS: { name: "ARIS", symbol: "ARIS", decimals: 8, ed: "10000000" },
USDT: { name: "USDT", symbol: "USDT", decimals: 8, ed: "1000" },
USDT: { name: "USDT", symbol: "USDT", decimals: 6, ed: "1000" },
},
statemint: {
DOT: { name: "DOT", symbol: "DOT", decimals: 10, ed: "1000000000" },
USDT: { name: "USDT", symbol: "USDT", decimals: 8, ed: "1000" },
USDT: { name: "USDT", symbol: "USDT", decimals: 6, ed: "1000" },
},
};

Expand Down

0 comments on commit 1083856

Please sign in to comment.