@hikaru-fi/swap-router
Version:
Package for calculating optimal path for executing swaps
108 lines (101 loc) • 4.24 kB
JavaScript
const { toBN } = require("web3-utils");
const { WeightedPool } = require("../poolMath/weightedPool/weightedPool")
const { setPools, getPools } = require("../router/poolCache");
const { getSwap } = require("../router/swap");
const { SwapTypes } = require("../utils/types");
const poolJSON = {
"_id": {"$oid": "62b758ded43171bda6d0aa60"},
"address": "TCPZ7EuzWgxBJPsWajnxNgohMc7QrfZXtC",
"vaultAddress": "TT6iy6H7hjpVH7g8eYcyak8iu97XfKNC2u",
"factoryAddress": "TQ73aTFX1cPbem2ogXwuRxtMeM6BhkRCgt",
"poolManagerAddress": "TG7g7V5fmeiVpWWsxqnhX8g8S86w3wMtmG",
"swapFee": {"exact": "3000000000000000","approximate": 3000000000000000},
"name": "TestPool1",
"symbol": "TP1",
"tokens": [{
"address": "TBgegBXzJ7LcTE4uaxV66oH8RaC7iW4LNp",
"name":"WP DAI",
"symbol": "WP DAI",
"exchangeRate": 0.1,
"multiplier": {"exact": "1000000000000","approximate": 1e12},
"weight": {"exact": "300000000000000000","approximate": 3e17},
"balance": {"exact": "1000101999994900","approximate": 1000101999994900},
"decimal": 6,
"_id": {"$oid": "62b758ded43171bda6d0aa61"},
"createdAt": {"$date": {"$numberLong": "1656183006230"}},
"updatedAt": {"$date": {"$numberLong": "1656183015019"}}
},{
"address": "THiitjrLkf5hmK24FZZ5ABDPwEYq9LhMEF",
"name":"TTR",
"symbol": "TTR",
"exchangeRate": 0.1,
"multiplier": {"exact": "1","approximate": 1},
"weight": {"exact": "200000000000000000","approximate": 2e17},
"balance": {"exact": "99999611465657731308658","approximate": 99999611465657731308658},
"decimal": 18,
"_id": {"$oid": "62b758ded43171bda6d0aa62"},
"createdAt": {"$date": {"$numberLong": "1656183006230"}},
"updatedAt": {"$date": {"$numberLong": "1656183015022"}}
},{
"address": "TFfwjZbccda57u96kxjcNHovgKVXpKUZAQ",
"name":"ST",
"symbol": "ST",
"exchangeRate": 0.1,
"multiplier": {"exact": "10000000","approximate": 1e7},
"weight": {"exact": "500000000000000000","approximate": 5e17},
"balance": {"exact": "9999996000000000000000","approximate": 9999996000000000000000},
"decimal": 11,
"_id": {"$oid": "62b758ded43171bda6d0aa63"},
"createdAt": {"$date": {"$numberLong": "1656183006230"}},
"updatedAt": {"$date": {"$numberLong": "1656183015019"}}
}],
"lastModEvent": {
"block": 23,
"timestamp": 1656183015023,
"contract": "TSSMHYeV2uE9qYH95DqyoCuNCzEL1NvU3S",
"name": "Withdraw",
"transaction": "87671d23725c4bdef839320c9d6be8eb4de1da3fef57841180e39a96ff1214fd",
"result": {
"pool": "d6590288EDA39b0Db2465342dAE38De7154AB9A6",
"lpAmount": {"negative": 0,"words": [3075503,50870594,7883417],"length": 3,"red": null},
"tokensReceived": [{"negative": 0,"words": [0],"length": 1,"red": null},{
"negative": 0,
"words": [0],
"length": 1,
"red": null
},{"negative": 0,"words": [0],"length": 1,"red": null},{
"negative": 0,
"words": [57228300,35],
"length": 2,
"red": null
},{"negative": 0,"words": [0],"length": 1,"red": null}],
"user": "3Bf5C10904a8d90A87840b30f6Dfd1D1D1513255"
},
"resourceNode": "solidityNode",
"_id": {"$oid": "62b758e7d43171bda6d0e6ed"},
"createdAt": {"$date": {"$numberLong": "1656183015025"}},
"updatedAt": {"$date": {"$numberLong": "1656183015025"}}
},
"createdAt": {"$date": {"$numberLong": "1656183006230"}},
"updatedAt": {"$date": {"$numberLong": "1656183015025"}},
"__v": 0
}
function setupPools() {
const pool = WeightedPool.fromDBObj(poolJSON);
const poolDict = {};
poolDict[pool.address] = pool;
setPools(poolDict);
}
function testSwap() {
setupPools();
const resultSwap = getSwap(
'TBgegBXzJ7LcTE4uaxV66oH8RaC7iW4LNp',
'THiitjrLkf5hmK24FZZ5ABDPwEYq9LhMEF',
toBN(100),
'BuyTokens',
getPools(),
{}
)
console.log(resultSwap)
}
testSwap();