Skip to content

Commit

Permalink
fixes for testnet
Browse files Browse the repository at this point in the history
  • Loading branch information
kuegi committed Sep 12, 2022
1 parent 927de89 commit d10a3ba
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
23 changes: 16 additions & 7 deletions ocean-client/src/programs/testnetbot-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class TestnetBotProgram extends CommonProgram {
const usdtPool = poolData.find(pool => pool.symbol === "DFI-USDT")
const dusdusdtPool = poolData.find(pool => pool.symbol === "DUSD-USDT")

if (!dusdPool?.priceRatio.ab || !usdtPool?.priceRatio.ba || !dusdusdtPool?.priceRatio.ba) {
if (!dusdPool?.priceRatio.ab || !usdtPool?.priceRatio.ab || !dusdusdtPool?.priceRatio.ba) {
console.error("couldn't get stable pool data")
return false
}
Expand All @@ -87,15 +87,24 @@ export class TestnetBotProgram extends CommonProgram {
console.log("got ratio in: " + ratioIn.toFixed(2) + " out: " + ratioOut.toFixed(2))

let path: PoolId[] | undefined
if (ratioIn.gt(1.01)) {
path = [{ id: +dusdPool.id }, { id: +dusdusdtPool.id }, { id: +usdtPool.id }]
let loops= 1
if (ratioIn.gt(1.05)) {
loops= ratioIn.toNumber()
path = [{ id: +usdtPool.id },{ id: +dusdPool.id }, { id: +dusdusdtPool.id }]
}
if (ratioOut.gt(1.01)) {
path = [{ id: +usdtPool.id }, { id: +dusdusdtPool.id }, { id: +dusdPool.id }]
if (ratioOut.gt(1.05)) {
loops= ratioOut.toNumber()
path = [{ id: +dusdusdtPool.id }, { id: +dusdPool.id },{ id: +usdtPool.id }]
}
if (path != undefined) {
let swap = await this.compositeswap(new BigNumber(100), 0, 0, path, new BigNumber(1).decimalPlaces(8))
let result = await this.waitForTx(swap.txId)
console.log("trying arbitrage with "+Math.floor(loops)+" loops")
let prevout= undefined
let swap= undefined
for (let i = 0; i < Math.floor(loops); i++) {
swap = await this.compositeswap(new BigNumber(1000), 5, 5, path, new BigNumber(1),prevout)
prevout= this.prevOutFromTx(swap)
}
let result = await this.waitForTx(swap!.txId)
if (result) {
console.log("done arbitrage")
telegram.send("done arbitrage")
Expand Down
9 changes: 5 additions & 4 deletions ocean-client/src/programs/vault-maxi-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ export class VaultMaxiProgram extends CommonProgram {
private readonly swapRewardsToMainColl: boolean
private negInterestWorkaround: boolean = false
private dusdCollValue: BigNumber = new BigNumber(0.99)
public readonly dusdTokenId : number

constructor(store: IStore, walletSetup: WalletSetup) {
super(store, walletSetup);

this.dusdTokenId = walletSetup.isTestnet()? 11 : 15
this.lmPair = this.settings.LMPair;
[this.assetA, this.assetB] = this.lmPair.split("-")
this.mainCollateralAsset = this.settings.mainCollateralAsset
Expand All @@ -74,7 +75,7 @@ export class VaultMaxiProgram extends CommonProgram {
async init(): Promise<boolean> {
let result = await super.init()
this.negInterestWorkaround = true
this.dusdCollValue = new BigNumber((await this.getCollateralToken("15")).factor)
this.dusdCollValue = new BigNumber((await this.getCollateralToken(""+this.dusdTokenId)).factor)
console.log((this.negInterestWorkaround ? "using negative interest workaround" : "") + " dusd CollValue is " + this.dusdCollValue.toFixed(3))
return result
}
Expand Down Expand Up @@ -372,7 +373,7 @@ export class VaultMaxiProgram extends CommonProgram {
+ "\n" + (this.keepWalletClean ? "trying to keep the wallet clean" : "ignoring dust and commissions")
+ "\n" + (this.isSingleMint ? ("minting only " + this.assetA) : "minting both assets")
+ "\nmain collateral asset is " + this.mainCollateralAsset
+ (this.settings.reinvestThreshold ?? -1 >= 0 ? ("\n" +this.swapRewardsToMainColl && this.mainCollateralAsset != "DFI" ? "will swap rewards to "+this.mainCollateralAsset+" before reinvest": "will directly reinvest DFI") : "")
+ (this.settings.reinvestThreshold ?? -1 >= 0 ? ("\n" +(this.swapRewardsToMainColl && this.mainCollateralAsset != "DFI" ? " will swap rewards to "+this.mainCollateralAsset+" before reinvest": "will directly reinvest DFI")) : "")
+ "\n" + (this.settings.autoDonationPercentOfReinvest > 0 ? autoDonationMessage : "auto donation is turned off")
+ "\n" + (this.settings.stableCoinArbBatchSize > 0 ? "searching for arbitrage with batches of size " + this.settings.stableCoinArbBatchSize : "not searching for stablecoin arbitrage")
+ "\nusing ocean at: " + this.walletSetup.url
Expand Down Expand Up @@ -1104,7 +1105,7 @@ export class VaultMaxiProgram extends CommonProgram {
return true
}
} else {
let mainTokenId = 15 //default DUSD
let mainTokenId = this.dusdTokenId //default DUSD
vault.collateralAmounts.forEach(coll => {
if (coll.symbol == this.mainCollateralAsset) {
mainTokenId = +coll.id
Expand Down
6 changes: 6 additions & 0 deletions ocean-client/src/testnet-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const MIN_TIME_PER_ACTION_MS = 300 * 1000 //min 5 minutes for action. probably o
export const VERSION = "v1.0"

export async function main(event: botEvent, context: any): Promise<Object> {
delay(5000) //triggered with maxi, give maxi headstart
console.log("testnetBot " + VERSION)
let blockHeight = 0
let ocean = process.env.VAULTMAXI_OCEAN_URL
Expand All @@ -35,6 +36,11 @@ export async function main(event: botEvent, context: any): Promise<Object> {
if (event) {
console.log("received event " + JSON.stringify(event))
}
while(settings.stateInformation.state != ProgramState.Idle) {
console.log("not idle, waiting 30 sec")
delay(30000)
settings = await store.fetchSettings()
}
const program = new TestnetBotProgram(store, new WalletSetup(settings, ocean))
await program.init()
if (!program.isTestNet()) {
Expand Down
15 changes: 10 additions & 5 deletions ocean-client/src/utils/store_aws_testnetbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ export class StoreAWSTestnetBot implements IStore {
let seedkey = process.env.DEFICHAIN_SEED_KEY ?? StoreKey.DeFiWalletSeed

let DeFiAddressKey = StoreKey.DeFiAddress
let StateKey = StoreKey.State

//store only allows to get 10 parameters per request
let parameters = (await this.ssm.getParameters({
Names: [
StoreKey.TelegramLogsChatId,
StoreKey.TelegramLogsToken,
DeFiAddressKey
DeFiAddressKey,
StateKey
]
}).promise()).Parameters ?? []

Expand All @@ -46,9 +48,11 @@ export class StoreAWSTestnetBot implements IStore {
this.settings.token = this.getValue(StoreKey.TelegramLogsChatId, parameters)
this.settings.chatId = this.getValue(StoreKey.TelegramLogsToken, parameters)
this.settings.address = this.getValue(DeFiAddressKey, parameters)

this.settings.stateInformation = ProgramStateConverter.fromValue(this.getValue(StateKey, parameters))

let seedList = decryptedSeed?.Parameter?.Value?.replace(/[ ,]+/g, " ")
this.settings.seed = seedList?.trim().split(' ') ?? []
console.log("read settings: "+JSON.stringify(this.settings))
return this.settings
}

Expand All @@ -72,10 +76,11 @@ export class StoreAWSTestnetBot implements IStore {
}

enum StoreKey {
TelegramLogsChatId = '/defichain-maxi/telegram/logs/chat-id',
TelegramLogsToken = '/defichain-maxi/telegram/logs/token',
TelegramLogsChatId = '/defichain-maxi/telegram/notifications/chat-id',
TelegramLogsToken = '/defichain-maxi/telegram/notifications/token',

DeFiAddress = '/defichain-maxi/wallet-reinvest/address',
DeFiAddress = '/defichain-maxi/wallet/address',
DeFiWalletSeed = '/defichain-maxi/wallet/seed',

State = '/defichain-maxi/state',
}
2 changes: 1 addition & 1 deletion ocean-client/src/vault-maxi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export async function main(event: maxiEvent, context: any): Promise<Object> {
//if DUSD loan is involved and current interest rate on DUSD is above LM rewards -> remove Exposure
if (settings.mainCollateralAsset === "DFI") {
const poolApr = (pool?.apr?.total ?? 0) * 100
const dusdToken = await program.getLoanToken("15")
const dusdToken = await program.getLoanToken(""+program.dusdTokenId)
let interest = +vault.loanScheme.interestRate + +dusdToken.interest
console.log("DUSD currently has a total interest of " + interest.toFixed(4) + " = " + vault.loanScheme.interestRate + " + " + dusdToken.interest + " vs APR of " + poolApr.toFixed(4))
if (interest > poolApr) {
Expand Down

0 comments on commit d10a3ba

Please sign in to comment.