AcalaNetwork / acala.js

https://developer.acala.network/
Apache License 2.0
59 stars 26 forks source link

Swap on DeX Issue #443

Open gsanjaime opened 1 year ago

gsanjaime commented 1 year ago

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()