Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
dydxsample
const Web3 = require('web3')
const {Solo, BigNumber} = require('./dist/js/src/index.js')
const {AmountReference, AmountDenomination, ConfirmationType} = require('./dist/js/src/types.js')
process.on('unhandledRejection', console.dir)
const WETH_Market_ID = new BigNumber('0')
const DAI_Market_ID = new BigNumber('1')
const USDC_Market_ID = new BigNumber('2')
/**
* Make Markets.
* @param solo
* @param accounts
* @param numMarkets
* @return {Promise<void>}
*/
const setupMarkets = async (solo, accounts, numMarkets = 3) => {
const priceOracle = solo.testing.priceOracle.getAddress()
const interestSetter = solo.testing.interestSetter.getAddress()
const marginPremium = new BigNumber(0)
const spreadPremium = new BigNumber(0)
await Promise.all([
solo.testing.priceOracle.setPrice(
solo.testing.tokenA.getAddress(),
new BigNumber('200'),
),
solo.testing.priceOracle.setPrice(
solo.testing.tokenB.getAddress(),
new BigNumber('1.03'),
),
solo.testing.priceOracle.setPrice(
solo.testing.tokenC.getAddress(),
new BigNumber('1'),
),
])
const tokens = [
solo.testing.tokenA.getAddress(),
solo.testing.tokenB.getAddress(),
solo.testing.tokenC.getAddress(),
]
for (let i = 0; i < numMarkets && i < tokens.length; i += 1) {
await solo.admin.addMarket(
tokens[i],
priceOracle,
interestSetter,
marginPremium,
spreadPremium,
{from: accounts[0]},
)
}
}
/**
* Set access allowance at any tokens.
* @param solo
* @param userAddress
* @return {Promise<void>}
*/
const allowances = async (solo, userAddress) => {
const tokens = [
solo.testing.tokenA.getAddress(),
solo.testing.tokenB.getAddress(),
solo.testing.tokenC.getAddress(),
]
for (let i = 0; i < tokens.length; i += 1) {
await solo.token.setMaximumSoloAllowance(
tokens[i],
userAddress,
{from: userAddress},
)
}
}
/**
* Add Deposit action to Operation.
* @param operation
* @param userAddress
* @param accountNum
* @param amount
* @param marketId
* @return {Promise<void>}
*/
const addDepositOpe = async (operation, userAddress, accountNum = '0', amount = '200e18', marketId = '1') => {
operation
.deposit({
primaryAccountOwner: userAddress,
primaryAccountId: new BigNumber(accountNum),
marketId: new BigNumber(marketId),
amount: {
value: new BigNumber(amount),
reference: AmountReference.Delta,
denomination: AmountDenomination.Actual,
},
from: userAddress,
})
}
/**
* Add Withdraw action to operation.
* @param operation
* @param userAddress
* @param accountNum
* @param amount
* @param marketId
* @return {Promise<void>}
*/
const addWithdrawOpe = async (operation, userAddress, accountNum = '0', amount = '-0.8e18', marketId = '0') => {
operation.withdraw({
primaryAccountOwner: userAddress,
primaryAccountId: new BigNumber(accountNum),
marketId: new BigNumber(marketId), // WETH Market ID
amount: {
value: new BigNumber(amount),
reference: AmountReference.Delta,
denomination: AmountDenomination.Actual,
},
to: userAddress
})
}
/**
* Commit actions in operation.
* This method send transaction to ethereum.
*
* @param operation
* @param userAddress
* @return {Promise<Object|void|Promise<TxResult>|*>}
*/
const commit = async (operation, userAddress) => {
return operation.commit({
from: userAddress,
gasPrice: '1000000000',
confirmationType: ConfirmationType.Confirmed,
})
}
/**
* Some token issue for Sample.
*
* @param solo
* @param accounts
* @return {Promise<void>}
*/
const setupTokenIssue = async (solo, accounts) => {
// account[9] is investor
await solo.testing.tokenA.issue(new BigNumber('1000e18'), accounts[9]) // weth
await solo.testing.tokenB.issue(new BigNumber('20000e18'), accounts[9]) // dai
await solo.testing.tokenC.issue(new BigNumber('20000e18'), accounts[9]) // usdc
// account[0] is borrower.
// await solo.testing.tokenA.issue(new BigNumber('2e18'), accounts[0]) // weth
await solo.testing.tokenB.issue(new BigNumber('230e18'), accounts[0]) // dai
// await solo.testing.tokenC.issue(new BigNumber('10e18'), accounts[0]) // usdc
await allowances(solo, accounts[0])
await allowances(solo, accounts[1])
await allowances(solo, accounts[9])
await solo.testing.tokenB.transfer(accounts[9], solo.testing.exchangeWrapper.getAddress(), new BigNumber('200e18'))
// deposit for supply enough volume
const operation = await solo.operation.initiate()
addDepositOpe(operation, accounts[9], '0', '1000e18', '0')
addDepositOpe(operation, accounts[9], '0', '19000e18', '1')
addDepositOpe(operation, accounts[9], '0', '20000e18', '2')
await commit(operation, accounts[9])
}
/**
* Main Function
* @return {Promise<void>}
*/
const main = async () => {
const web3 = new Web3('http://localhost:8445')
const accounts = await web3.eth.getAccounts()
const solo = new Solo(
web3.currentProvider, // Valid web3 provider
'1001',
{
testing: true,
defaultAccount: accounts[1]
}
)
await solo.testing.evm.resetEVM(2)
await setupMarkets(solo, accounts)
await setupTokenIssue(solo, accounts)
const operation = await solo.operation.initiate()
addWithdrawOpe(operation, accounts[0], '0', '-1e18', WETH_Market_ID)
addDepositOpe(operation, accounts[0], '0', '230e18', DAI_Market_ID)
await commit(operation, accounts[0])
const balances = await solo.getters.getAccountBalances(
accounts[0], // Account Owner
new BigNumber('0'), // Account Number
)
console.log(JSON.stringify(balances, null, " "))
}
try {
main().then()
} catch (e) {
console.err(e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.