Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bid with DAI logic #163

Merged
merged 2 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bot-twitter/src/keeper.ts
Original file line number Diff line number Diff line change
@@ -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 { bidWithCallee, enrichAuction } from 'auctions-core/src/auctions';
import {
authorizeCollateral,
authorizeWallet,
Expand Down Expand Up @@ -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 bidWithCallee(ETHEREUM_NETWORK, auctionTransaction, walletAddress);
console.info(`keeper: auction "${auctionTransaction.id}" was succesfully executed via "${bidHash}" transaction`);
};

Expand Down
28 changes: 24 additions & 4 deletions core/src/auctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<string> {
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,
Expand All @@ -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,
];
Expand Down
2 changes: 2 additions & 0 deletions core/src/constants/UNITS.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions frontend/containers/AuctionsContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@authorizeWallet="authorizeWallet"
@authorizeCollateral="authorizeCollateral"
@restart="restartAuction"
@execute="bid"
@execute="bidWithCallee"
@fetchTakeEventsFromAuction="fetchTakeEventsByAuctionId"
/>
</div>
Expand Down Expand Up @@ -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);
Expand Down
46 changes: 42 additions & 4 deletions frontend/store/auctions.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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<State, State>,
{ id, alternativeDestinationAddress }: { id: string; alternativeDestinationAddress: string | undefined }
) {
Expand All @@ -186,7 +188,7 @@ export const actions = {
}
commit('setIsBidding', true);
try {
const transactionAddress = await bidOnTheAuction(
const transactionAddress = await bidWithCallee(
network,
auction,
alternativeDestinationAddress || walletAddress,
Expand All @@ -199,6 +201,42 @@ export const actions = {
commit('setIsBidding', false);
}
},
async bidWithDai(
{ getters, commit, dispatch, rootGetters }: ActionContext<State, State>,
{
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<State, State>, id: string) {
const network = rootGetters['network/getMakerNetwork'];
const auction = getters.getAuctionById(id);
Expand Down