From 591adf352949905d3913d617986bc817e2e69ee4 Mon Sep 17 00:00:00 2001 From: Valia Fetisov Date: Tue, 29 Mar 2022 11:39:42 +0200 Subject: [PATCH 1/2] bidWithDai action --- bot-twitter/src/keeper.ts | 4 +- core/src/auctions.ts | 28 ++++++++++++-- core/src/constants/UNITS.ts | 2 + frontend/containers/AuctionsContainer.vue | 4 +- frontend/store/auctions.ts | 46 +++++++++++++++++++++-- 5 files changed, 72 insertions(+), 12 deletions(-) diff --git a/bot-twitter/src/keeper.ts b/bot-twitter/src/keeper.ts index df762efb2..8eb51b1ae 100644 --- a/bot-twitter/src/keeper.ts +++ b/bot-twitter/src/keeper.ts @@ -1,6 +1,6 @@ import { AuctionInitialInfo } from 'auctions-core/src/types'; import getSigner, { createSigner, setSigner } from 'auctions-core/src/signer'; -import { bidOnTheAuction, enrichAuction } from 'auctions-core/src/auctions'; +import { swapTheAuction, enrichAuction } from 'auctions-core/src/auctions'; import { authorizeCollateral, authorizeWallet, @@ -127,7 +127,7 @@ const checkAndParticipateIfPossible = async function (auction: AuctionInitialInf // bid on the Auction console.info(`keeper: auction "${auctionTransaction.id}": attempting swap execution`); - const bidHash = await bidOnTheAuction(ETHEREUM_NETWORK, auctionTransaction, walletAddress); + const bidHash = await swapTheAuction(ETHEREUM_NETWORK, auctionTransaction, walletAddress); console.info(`keeper: auction "${auctionTransaction.id}" was succesfully executed via "${bidHash}" transaction`); }; diff --git a/core/src/auctions.ts b/core/src/auctions.ts index 9fc621a48..0007840d8 100644 --- a/core/src/auctions.ts +++ b/core/src/auctions.ts @@ -4,7 +4,7 @@ import fetchAuctionsByCollateralType, { fetchAuctionStatus } from './fetch'; import { getCalleeData, getMarketPrice } from './calleeFunctions'; import { fetchCalcParametersByCollateralType } from './params'; import executeTransaction from './execute'; -import { RAY_NUMBER_OF_DIGITS, WAD_NUMBER_OF_DIGITS } from './constants/UNITS'; +import { RAY_NUMBER_OF_DIGITS, WAD_NUMBER_OF_DIGITS, NULL_BYTES } from './constants/UNITS'; import { getCalleeAddressByCollateralType } from './constants/CALLEES'; import { calculateAuctionDropTime, @@ -176,7 +176,27 @@ export const restartAuction = async function ( return executeTransaction(network, contractName, 'redo', [auction.index, profitAddress], notifier, false); }; -export const bidOnTheAuction = async function ( +export const bidWithDai = async function ( + network: string, + auction: Auction, + bidAmountDai: BigNumber, + profitAddress: string, + notifier?: Notifier +): Promise { + const contractName = getClipperNameByCollateralType(auction.collateralType); + const updatedAuction = await enrichAuctionWithActualNumbers(network, auction); + const collateralAmount = bidAmountDai.dividedBy(updatedAuction.unitPrice); + const contractParameters = [ + convertNumberTo32Bytes(auction.index), + collateralAmount.shiftedBy(WAD_NUMBER_OF_DIGITS).toFixed(0), + updatedAuction.unitPrice.shiftedBy(RAY_NUMBER_OF_DIGITS).toFixed(0), + profitAddress, + NULL_BYTES, + ]; + return executeTransaction(network, contractName, 'take', contractParameters, notifier); +}; + +export const bidWithCallee = async function ( network: string, auction: Auction, profitAddress: string, @@ -187,8 +207,8 @@ export const bidOnTheAuction = async function ( const contractName = getClipperNameByCollateralType(auction.collateralType); const contractParameters = [ convertNumberTo32Bytes(auction.index), - auction.collateralAmount.shiftedBy(WAD_NUMBER_OF_DIGITS).toFixed(), - auction.unitPrice.shiftedBy(RAY_NUMBER_OF_DIGITS).toFixed(), + auction.collateralAmount.shiftedBy(WAD_NUMBER_OF_DIGITS).toFixed(0), + auction.unitPrice.shiftedBy(RAY_NUMBER_OF_DIGITS).toFixed(0), calleeAddress, calleeData, ]; diff --git a/core/src/constants/UNITS.ts b/core/src/constants/UNITS.ts index 695d45918..165676e2c 100644 --- a/core/src/constants/UNITS.ts +++ b/core/src/constants/UNITS.ts @@ -1,5 +1,7 @@ import BigNumber from 'bignumber.js'; +export const NULL_BYTES = '0x'; + // Common Maker Units https://github.com/makerdao/dss/blob/master/DEVELOPING.md#units // WAD: fixed point decimal with 18 decimals (for basic quantities, e.g. balances) // RAY: fixed point decimal with 27 decimals (for precise quantites, e.g. ratios) diff --git a/frontend/containers/AuctionsContainer.vue b/frontend/containers/AuctionsContainer.vue index 53ce819bc..4932ad0ac 100644 --- a/frontend/containers/AuctionsContainer.vue +++ b/frontend/containers/AuctionsContainer.vue @@ -19,7 +19,7 @@ @authorizeWallet="authorizeWallet" @authorizeCollateral="authorizeCollateral" @restart="restartAuction" - @execute="bid" + @execute="bidWithCallee" @fetchTakeEventsFromAuction="fetchTakeEventsByAuctionId" /> @@ -114,7 +114,7 @@ export default Vue.extend({ 'fetchWalletAuthorizationStatus', 'fetchCollateralAuthorizationStatus', ]), - ...mapActions('auctions', ['bid', 'restart', 'fetchTakeEventsByAuctionId']), + ...mapActions('auctions', ['bidWithCallee', 'restart', 'fetchTakeEventsByAuctionId']), openWalletModal(): void { if (!this.hasAcceptedTerms) { this.$store.commit('modals/setTermsModal', true); diff --git a/frontend/store/auctions.ts b/frontend/store/auctions.ts index 8964a31ca..30b6ca551 100644 --- a/frontend/store/auctions.ts +++ b/frontend/store/auctions.ts @@ -1,16 +1,18 @@ import type { Auction, AuctionTransaction, TakeEvent } from 'auctions-core/src/types'; +import Vue from 'vue'; import { ActionContext } from 'vuex'; import { message } from 'ant-design-vue'; import { fetchAllAuctions, - bidOnTheAuction, + bidWithDai, + bidWithCallee, restartAuction, enrichAuctionWithPriceDropAndMarketValue, fetchTakeEvents, } from 'auctions-core/src/auctions'; import { checkAllCalcParameters } from 'auctions-core/src/params'; import { checkAllSupportedCollaterals } from 'auctions-core/src/addresses'; -import Vue from 'vue'; +import BigNumber from 'auctions-core/src/bignumber'; import getWallet from '~/lib/wallet'; import notifier from '~/lib/notifier'; @@ -169,7 +171,7 @@ export const actions = { refetchIntervalId = setInterval(() => dispatch('fetchWithoutLoading'), REFETCH_INTERVAL); updateAuctionsPricesIntervalId = setInterval(() => dispatch('updateAuctionsPrices'), TIMER_INTERVAL); }, - async bid( + async bidWithCallee( { getters, commit, rootGetters }: ActionContext, { id, alternativeDestinationAddress }: { id: string; alternativeDestinationAddress: string | undefined } ) { @@ -186,7 +188,7 @@ export const actions = { } commit('setIsBidding', true); try { - const transactionAddress = await bidOnTheAuction( + const transactionAddress = await bidWithCallee( network, auction, alternativeDestinationAddress || walletAddress, @@ -199,6 +201,42 @@ export const actions = { commit('setIsBidding', false); } }, + async bidWithDai( + { getters, commit, dispatch, rootGetters }: ActionContext, + { + id, + bidAmountDai, + alternativeDestinationAddress, + }: { id: string; bidAmountDai: BigNumber | string; alternativeDestinationAddress: string | undefined } + ) { + const auction = getters.getAuctionById(id); + if (!auction) { + message.error(`Bidding error: can not find auction with id "${id}"`); + return; + } + const network = rootGetters['network/getMakerNetwork']; + const walletAddress = getWallet().address; + if (!walletAddress) { + message.error('Bidding error: can not find wallet'); + return; + } + commit('setIsBidding', true); + try { + await bidWithDai( + network, + auction, + new BigNumber(bidAmountDai), + alternativeDestinationAddress || walletAddress, + notifier + ); + await dispatch('wallet/fetchWalletBalances', undefined, { root: true }); + await dispatch('fetch'); + } catch (error) { + console.error('Bidding error', error); + } finally { + commit('setIsBidding', false); + } + }, async restart({ getters, dispatch, commit, rootGetters }: ActionContext, id: string) { const network = rootGetters['network/getMakerNetwork']; const auction = getters.getAuctionById(id); From e5d38b5cf28a6cf5245aeb856c722107b5ef90aa Mon Sep 17 00:00:00 2001 From: Valia Fetisov Date: Tue, 29 Mar 2022 11:45:56 +0200 Subject: [PATCH 2/2] fix keeper --- bot-twitter/src/keeper.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot-twitter/src/keeper.ts b/bot-twitter/src/keeper.ts index 8eb51b1ae..34f71262a 100644 --- a/bot-twitter/src/keeper.ts +++ b/bot-twitter/src/keeper.ts @@ -1,6 +1,6 @@ import { AuctionInitialInfo } from 'auctions-core/src/types'; import getSigner, { createSigner, setSigner } from 'auctions-core/src/signer'; -import { swapTheAuction, enrichAuction } from 'auctions-core/src/auctions'; +import { bidWithCallee, enrichAuction } from 'auctions-core/src/auctions'; import { authorizeCollateral, authorizeWallet, @@ -127,7 +127,7 @@ const checkAndParticipateIfPossible = async function (auction: AuctionInitialInf // bid on the Auction console.info(`keeper: auction "${auctionTransaction.id}": attempting swap execution`); - const bidHash = await swapTheAuction(ETHEREUM_NETWORK, auctionTransaction, walletAddress); + const bidHash = await bidWithCallee(ETHEREUM_NETWORK, auctionTransaction, walletAddress); console.info(`keeper: auction "${auctionTransaction.id}" was succesfully executed via "${bidHash}" transaction`); };