@gateway.fm/gtw-dvf-client-js
Version:
DVF client js lib with gateway.fm rpc endpoints
84 lines (67 loc) • 2.47 kB
JavaScript
#!/usr/bin/env node
/*
DO NOT EDIT THIS FILE BY HAND!
Examples are generated using helpers/buildExamples.js script.
Check README.md for more details.
*/
const HDWalletProvider = require('@truffle/hdwallet-provider')
const sw = require('starkware_crypto')
const Web3 = require('web3')
const DVF = require('../src/dvf')
const envVars = require('./helpers/loadFromEnvOrConfig')(
process.env.CONFIG_FILE_NAME
)
const logExampleResult = require('./helpers/logExampleResult')(__filename)
const ethPrivKey = envVars.ETH_PRIVATE_KEY
// NOTE: you can also generate a new key using:`
// const starkPrivKey = dvf.stark.createPrivateKey()
const starkPrivKey = envVars.STARK_PRIVATE_KEY
const rpcUrl = envVars.RPC_URL
const provider = new HDWalletProvider(ethPrivKey, rpcUrl)
const web3 = new Web3(provider)
provider.engine.stop()
const dvfConfig = {
api: envVars.API_URL,
dataApi: envVars.DATA_API_URL,
useAuthHeader: true,
wallet: {
type: 'tradingKey',
meta: {
starkPrivateKey: starkPrivKey
}
},
apiKey: envVars.API_KEY
// Add more variables to override default values
}
;(async () => {
const dvf = await DVF(web3, dvfConfig)
dvf.config.useAuthHeader = true
const waitForDepositCreditedOnChain = require('./helpers/waitForDepositCreditedOnChain')
if (process.env.DEPOSIT_FIRST === 'true') {
const depositETHResponse = await dvf.deposit('KON', 100, starkPrivKey)
const depositUSDTResponse = await dvf.deposit('DVF', 200, starkPrivKey)
await waitForDepositCreditedOnChain(dvf, depositETHResponse)
await waitForDepositCreditedOnChain(dvf, depositUSDTResponse)
}
const pool = 'KONDVF'
// Amm deposit consist of 2 orders, one for each of the pool tokens.
// The tokens need to be supplied in a specific ratio. This call fetches
// order data from Deversifi API via Gateway.FM rpc endpoint, given one of the tokens and desired deposit
// amount for that token.
const ammFundingOrderData = await dvf.getAmmFundingOrderData({
pool,
token: 'KON',
amount: 100
})
// This call signs the orders contained in the ammFundingOrderData before
// posting them to Deversifi API via Gateway.FM rpc endpoint. NOTE: if the orders are pre-signed, the
// method will post them as is.
const ammPostFundingOrderResponse = await dvf.postAmmFundingOrders(
ammFundingOrderData
)
logExampleResult(ammPostFundingOrderResponse)
})()
.catch(error => {
console.error(error)
process.exit(1)
})