Skip to content

Commit

Permalink
Swap briq factory text, fix exporting sets, add migration TX to factory
Browse files Browse the repository at this point in the history
  • Loading branch information
wraitii committed Dec 4, 2023
1 parent 74cc497 commit 663bc4f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/builder/ChainBriqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { legacyChainBriqs } from './ChainBriqsLegacy';
// TODO: there can technically be more than whatever is supported by number
type BALANCE = { ft_balance: number; nft_ids: string[] };

class NotEnoughBriqs extends Error {
export class NotEnoughBriqs extends Error {
material: string;
constructor(material: string) {
super(`Not enough Briqs with material ${material}`);
Expand Down
24 changes: 20 additions & 4 deletions src/builder/ChainBriqsLegacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { maybeStore } from '@/chain/WalletLoading';
import { setChainBriqsStoreComplete } from './ChainBriqsAsync';
import { Notification } from '@/Notifications';
import { getPremigrationNetwork } from '@/chain/Network';
import { NotEnoughBriqs } from './ChainBriqs';

/**
* Responsible for maintaining the state of 'on-chain' briqs, as opposed to local set-briqs.
Expand Down Expand Up @@ -121,6 +122,8 @@ export class LegacyChainBriqs implements perUserStorable {
}
if (update.status === 'DELETING_SOON')
bal -= update.quantity;
else
bal += update.quantity;
}
this.balance = bal;
for (const item of promises)
Expand All @@ -133,10 +136,23 @@ export class LegacyChainBriqs implements perUserStorable {
return this.balance;
}

migrateBriqs() {
if (this.getNbBriqs() <= 0)
return;
// TODO -> TX
/**
* Check that we have enough on-chain briqs available,
* and if not return NFTs that can be used to complement.
* Note that this function won't swap existing NFTs that are unavailable.
* @param usageByMaterial entry balance
* @returns a list of NFT briqs to replace.
*/
findRealBriqs(usageByMaterial: { [material: string]: { ft_balance: number; nft_ids: string[] } }) {
const swaps = [] as Briq[];
for (const mat in usageByMaterial)
if (usageByMaterial[mat].ft_balance > this.balance)
throw new NotEnoughBriqs(mat);
return swaps;
}

show(quantity: number, tx_hash: string, date?: number) {
return this._add('TENTATIVE', quantity, tx_hash, false, date);
}

hide(quantity: number, tx_hash: string, date?: number) {
Expand Down
4 changes: 4 additions & 0 deletions src/chain/Marketplaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ export function getBoxLink(marketplace: string, network: CHAIN_NETWORKS, collect
export function getSetMarketplaceUrl() {
return 'https://unframed.co/collection/0x01435498bf393da86b4733b9264a86b58a42b31f8d8b8ba309593e5c17847672';
}

export function getBriqLink(marketplace: string, network: CHAIN_NETWORKS) {
return 'https://element.market/collections/briq-token';
}
8 changes: 4 additions & 4 deletions src/chain/contracts/briq_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export default class BriqContract {
}

buyTransaction(erc20_contract: ERC20Contract, amount: number, approval: number.BigNumberish) {
return migrateBriqsIfNeeded([
return [
erc20_contract.contract.populateTransaction['approve'](this.contract.address, cairo.uint256(BigInt(approval))),
this.contract.populateTransaction.buy(`${amount}`),
erc20_contract.contract.populateTransaction['approve'](this.contract.address, cairo.uint256(0n)),
]);
];
}

async buy(erc20_contract: ERC20Contract, amount: number, approval: number.BigNumberish) {
Expand All @@ -49,14 +49,14 @@ export default class BriqContract {

export class BriqFactoryOnDojoContract extends BriqContract {
buyTransaction(erc20_contract: ERC20Contract, amount: number, approval: number.BigNumberish) {
return [
return migrateBriqsIfNeeded([
erc20_contract.contract.populateTransaction['approve'](this.contract.address, cairo.uint256(BigInt(approval))),
{
contractAddress: this.contract.address,
entrypoint: 'buy',
calldata: [1, `${amount}`],
},
erc20_contract.contract.populateTransaction['approve'](this.contract.address, cairo.uint256(0n)),
]
]);
}
}
8 changes: 5 additions & 3 deletions src/components/BuyBriqsWidget.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import { chainBriqs } from '@/builder/ChainBriqs';
import MenuLike from './generic/MenuLike.vue';
import Tooltip from './generic/Tooltip.vue';
import { APP_ENV } from '@/Meta';
import { getSetMarketplaceUrl } from '@/chain/Marketplaces';
import { getBriqLink } from '@/chain/Marketplaces';
import { backendManager } from '@/Backend';
import { maybeStore } from '@/chain/WalletLoading';
import { getCurrentNetwork } from '@/chain/Network';
const props = defineProps<{
Expand Down Expand Up @@ -198,8 +199,9 @@ const cancelBuy = () => {
<div class="my-4 text-sm flex justify-between items-center gap-8">
<div>
<p class="mt-1" v-if="parameters._data">1 briq = {{ readableNumber(price_ber_briq) }} {{ readableUnit(price_ber_briq) }}</p>
<p class="my-2 flex items-center">
<i class="mr-2 far fa-circle-exclamation text-primary"/> <span>Please be aware that the price of briq is currently very high due to demand. You can also get briqs by buying sets from <a class="text-primary" :href="getSetMarketplaceUrl()">Unframed</a> and disassembling them.</span>
<p class="my-2 border-l-4 border-primary pl-2 font-medium">
You can also buy briqs on the secondary market!<br>
Check out <a :href="getBriqLink('element', getCurrentNetwork())!" target="_blank" class="text-primary">Element</a> marketplace.
</p>
</div>
<i @click.stop.prevent="toggledDetails=!toggledDetails" :class="`p-2 cursor-pointer hover:bg-grad-light rounded fa-solid fa-chevron-${toggledDetails ? 'up' : 'down'}`"/>
Expand Down
24 changes: 21 additions & 3 deletions src/components/builder/modals/ExportSet.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,13 @@ watch([setData, toRef(chainBriqs.value, 'byMaterial')], () => {
try {
es.swapForRealBriqs(chainBriqs.value!);
} catch (err) {
exportSet.value = undefined;
return;
// Temp for migration
try {
es.swapForRealBriqs(legacyChainBriqs.current!);
} catch (err) {
exportSet.value = undefined;
return;
}
}
exportSet.value = es;
}, {
Expand Down Expand Up @@ -141,6 +146,11 @@ const buyBriqsAndMint = async (data: unknown) => {
briqTransaction.value = contractStore.briq_factory?.buyTransaction(contractStore.eth_bridge_contract!, data.briqs, data.price);
// Don't notify in case we stop.
briqPendingObject.value = chainBriqs.value?.show('0x1', data.briqs, '');
if (legacyChainBriqs.current?.getNbBriqs()) {
const legacyBriqNb = legacyChainBriqs.current?.getNbBriqs() || 0;
chainBriqs.value?.show('0x1', legacyBriqNb, 'hack', false);
legacyChainBriqs.current?.hide(legacyBriqNb, 'hack');
}
// This will update ExportSet through watchers, but might take some time, so nextTick.
nextTick(() => {
startMinting();
Expand Down Expand Up @@ -242,8 +252,16 @@ const startMinting = async () => {
} catch (err: any) {
// Reset the briqs if we bought some.
// TODO: this is fairly hacky.
if (briqPendingObject.value)
if (briqPendingObject.value) {
chainBriqs.value!.removeMetadataItem(briqPendingObject.value);
// Super hacky part.
const legacyHacks = legacyChainBriqs.current?.metadata.filter(x => x.tx_hash === 'hack');
for (const hack of legacyHacks || []) {
const legacyBriqNb = hack.quantity;
chainBriqs.value?.hide('0x1', legacyBriqNb, 'hack');
legacyChainBriqs.current?.show(legacyBriqNb, 'hack');
}
}
if (err?.message === 'User abort') {
pushPopup('error', 'Mint error', 'Minting transaction aborted.');
Expand Down

0 comments on commit 663bc4f

Please sign in to comment.