Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap on DeX Issue #443

Open
gsanjaime opened this issue Nov 24, 2022 · 0 comments
Open

Swap on DeX Issue #443

gsanjaime opened this issue Nov 24, 2022 · 0 comments

Comments

@gsanjaime
Copy link

Running the wiki example for swapping on Dex I get the follow error:

C:\PROYECTOS\NODE.JS\Acala\swap.js:29
const pool = await api.derive.dex.pool('DOT');
^

TypeError: Cannot read properties of undefined (reading 'pool')
at main (C:\INTOOLGES\PROYECTOS\NODE.JS\Acala\swap.js:29:39)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Swap on DeX
const { ApiPromise } = require('@polkadot/api');
const { WsProvider } = require('@polkadot/rpc-provider');
const { options } = require('@acala-network/api');
const { Fixed18, convertToFixed18, calcSwapTargetAmount } = require('@acala-network/app-util');

const { Keyring } = require('@polkadot/api');

async function main() {
const provider = new WsProvider('wss://karura.api.onfinality.io/public-ws');
const api = new ApiPromise(options({ provider }));
await api.isReady;

const keyring = new Keyring({ type: 'sr25519' });
const newPair = keyring.addFromUri('yourinput');
const address = newPair.address;
console.log(newPair.address); // you need to get test tokens

// DOT -> aUSD

// Set Supply Amount
const supply = 1

// Query Dex Pool
const pool = await api.derive.dex.pool('DOT');

// Query Exchange Fee
const exchangeFee = api.consts.dex.getExchangeFee;

// Calculate Target Currency Amount
const target = calcSwapTargetAmount(
    supply,
    convertToFixed18(pool.base),
    convertToFixed18(pool.other),
    convertToFixed18(exchangeFee),
    Fixed18.fromNature(0.005)
);

// Exec Exchange
await api.tx.dex.swapCurrency(
    'DOT',
    Fixed18.fromNatural(supply).innerToString(),
    'AUSD',
    Fixed18.fromNatural(target).innerToString()
).signAndSend(newPair);

// Ensure Amount
const dotAccount = await api.query.tokens.accounts(address, 'DOT');
console.log(dotAccount.toHuman());

const aUSDAccount = await api.query.tokens.accounts(address, 'AUSD');
console.log(aUSDAccount.toHuman());

}

main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant