Skip to content

Commit

Permalink
project completed!
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed Oct 22, 2023
1 parent afc2aaa commit 76cfb02
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 34 deletions.
9 changes: 3 additions & 6 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import JoinsOrder from "./components/JoinsOrder.svelte";
import CreatorSwap from "./components/CreatorSwap.svelte";
import ReturnToOrder from "./components/ReturnToOrder.svelte";
import PartnerSwap from "./components/PartnerSwap.svelte";
// onMount(async () => {
Expand All @@ -31,12 +32,6 @@
$autonomousSwap = _autonomousSwap;
})
mainOrder.update(() => {
return ({
finalTransaction: null
})
});
</script>

Expand Down Expand Up @@ -64,6 +59,8 @@
{#if $mainOrder != null}
{#if $mainOrder.isCreator}
<CreatorSwap/>
{:else}
<PartnerSwap/>
{/if}
{/if}

Expand Down
4 changes: 2 additions & 2 deletions src/components/CreatorSwap.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import { signer, mainOrder, creatorSubOrder, autonomousSwap, generalState, creatorState, partnerState } from "../lib/stores.js";
import { CreatorState, PartnerState } from "../lib/enums.js";
import CreationStep from "./creator/CreationStep.svelte";
import Teste from "./creator/Teste.svelte";
import SwapSection from "./SwapSection.svelte";
import WaitingJoin from "./creator/WaitingJoin.svelte";
import { onMount, onDestroy } from "svelte";
import AllowanceStep from "./creator/AllowanceStep.svelte";
import Button from "./base/Button.svelte";
import FinalStep from "./creator/FinalStep.svelte";
import CancelButton from "./creator/CancelButton.svelte";
</script>
Expand All @@ -18,7 +18,7 @@
<!-- {#if $mainOrder.finalTransaction == null} -->
{#if !($partnerState == PartnerState.Completed && $creatorState == CreatorState.Completed)}
<div>
<h1 class="pb-5"><strong>Creator:</strong> {$mainOrder.creator}</h1>
<h1 class="pb-5 relative"> <CancelButton/> <strong class="ml-1">Creator:</strong> {$mainOrder.creator}</h1>
<hr class="mb-5 h-0.5 bg-black">
{#if $creatorState == CreatorState.WaitingForPartnerJoin}
<CreationStep isCreator={true}/>
Expand Down
18 changes: 15 additions & 3 deletions src/components/JoinsOrder.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { current_component } from "svelte/internal";
import { AddressLike, BytesLike, BigNumberish, ContractTransactionResponse, ethers } from "ethers";
import { autonomousSwap, partnerSubOrder, mainOrder, creatorState, generalState, partnerState } from "../lib/stores.js";
import { autonomousSwap, partnerSubOrder, mainOrder, creatorState, generalState, creatorSubOrder, partnerState } from "../lib/stores.js";
import { CreatorState, GeneralState, PartnerState } from "../lib/enums.js";
import Card from "./base/Card.svelte";
import ChooseToken from "./ChooseToken.svelte";
Expand Down Expand Up @@ -59,7 +59,19 @@
// console.log(logOutput)
let { creator, } = await _autonomousSwap.getOrderMembersById(logOutput.orderId as BytesLike);
console.log(creator)
// console.log(creator)
let _creatorSubOrder = await _autonomousSwap.getSubOrderByUser(creator, orderId);
creatorSubOrder.update(() => {
return ({
token: _creatorSubOrder.token,
interfaceID: _creatorSubOrder.interfaceID,
tokenId: _creatorSubOrder.tokenId,
quantity: _creatorSubOrder.quantity,
individualStatus: _creatorSubOrder.individualStatus
})
})
mainOrder.update(() => {
return ({
Expand All @@ -80,7 +92,7 @@
individualStatus: logOutput.newStatus
})
})
$creatorState = CreatorState.WaitingForPartnerJoin;
$partnerState = PartnerState.WaitingFirstConfirmation;
$generalState = GeneralState.OrderJoined;
}
Expand Down
73 changes: 51 additions & 22 deletions src/components/PartnerSwap.svelte
Original file line number Diff line number Diff line change
@@ -1,28 +1,57 @@
<script lang="ts">
import { ethers } from "ethers";
import { signer, mainOrder, creatorSubOrder, autonomousSwap, generalState, partnerState } from "../lib/stores.js";
import { PartnerState } from "../lib/enums.js";
import { signer, mainOrder, creatorSubOrder, autonomousSwap, generalState, creatorState, partnerState } from "../lib/stores.js";
import { CreatorState, PartnerState } from "../lib/enums.js";
import CreationStep from "./partner/CreationStep.svelte";
import SwapSection from "./SwapSection.svelte";
import WaitingJoin from "./partner/WaitingJoin.svelte";
import AllowanceStep from "./partner/AllowanceStep.svelte";
import Button from "./base/Button.svelte";
import FinalStep from "./partner/FinalStep.svelte";
</script>

<div>
<h1>PARTNER: {$mainOrder.partner}</h1>
{#if !$mainOrder.isCreator}
{#if $partnerState == PartnerState.WaitingFirstConfirmation}
<CreationStep/>
{:else if $partnerState == PartnerState.AllowingAutonomousSwap}
<p>teste2</p>
{:else if $partnerState == PartnerState.WaitingCreatorAllowance}
<p>teste3</p>
{:else if $partnerState == PartnerState.WaitingCreatorsFunding}
<p>teste4</p>
{:else if $partnerState == PartnerState.SendingTokens}
<p>teste5</p>
{:else if $partnerState == PartnerState.Completed}
<p>teste6</p>
{/if}

<SwapSection>
<!-- {#if $mainOrder.finalTransaction == null} -->
{#if !($partnerState == PartnerState.Completed && $creatorState == CreatorState.Completed)}
<div>
<h1 class="pb-5"><strong>Creator:</strong> {$mainOrder.creator}</h1>
<hr class="mb-5 h-0.5 bg-black">
{#if $creatorState == CreatorState.WaitingForPartnerJoin}
<CreationStep isCreator={true}/>
{:else if $creatorState == CreatorState.AllowingAutonomousSwap || $creatorState == CreatorState.WaitingPartnerAllowance}
<AllowanceStep isCreator={true}/>
{:else if $creatorState == CreatorState.SendingTokens}
<FinalStep isCreator={true} />
{:else if $creatorState == CreatorState.WaitingPartnersTransfer}
<p>Finished! Now, just await for the partner.</p>
{:else if $creatorState == CreatorState.Completed}
<p>Finished!</p>
{/if}
</div>

<div>
<h1 class="pb-5"><strong>Partner:</strong> {($mainOrder.partner != '0x0000000000000000000000000000000000000000' && $mainOrder.partner) ? $mainOrder.partner : 'waiting...'}</h1>
<hr class="mb-5 h-0.5 bg-black">
{#if $partnerState == null}
<WaitingJoin>partner.</WaitingJoin>
{:else if $partnerState == PartnerState.WaitingFirstConfirmation}
<CreationStep on:click={() => {$creatorState = CreatorState.AllowingAutonomousSwap; $partnerState = PartnerState.AllowingAutonomousSwap}}/>
{:else if $partnerState == PartnerState.AllowingAutonomousSwap || $partnerState == PartnerState.WaitingCreatorAllowance}
<AllowanceStep />
{#if $creatorState == CreatorState.WaitingPartnerAllowance && $partnerState == PartnerState.WaitingCreatorAllowance}
<Button isStep={true} on:click={() => {$creatorState = CreatorState.SendingTokens; $partnerState = PartnerState.WaitingCreatorsFunding}}>Next Step</Button>
{/if}
{:else if $partnerState == PartnerState.WaitingCreatorsFunding}
<FinalStep />
{:else if $partnerState == PartnerState.Completed}
<p>Finished!</p>
{/if}
</div>

{:else}
<p>WAITING</p>
<div class="col-span-2 ">
<h1 class="text-6xl">Order Completed!!!</h1>
</div>
{/if}
</div>
</SwapSection>
30 changes: 30 additions & 0 deletions src/components/creator/CancelButton.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<script lang="ts">
import Button from "../base/Button.svelte";
import { autonomousSwap, mainOrder, partnerSubOrder, creatorState, partnerState, creatorSubOrder, signer } from "../../lib/stores.js";
import { onDestroy } from "svelte";
import { ContractTransactionResponse } from "ethers";
let cancelled = false
let _autonomousSwap: AutonomousSwapContract;
const unsubscribe = autonomousSwap.subscribe((value) => {
_autonomousSwap = value;
})
onDestroy(unsubscribe);
async function cancelOrder(){
const transactionResponse: ContractTransactionResponse = await _autonomousSwap.cancelOrder($mainOrder.orderId);
const transactionReceipt = await transactionResponse.wait();
if (transactionReceipt.status == 1){
cancelled = true;
}
}
</script>

<div class="absolute -top-[150%]">
<Button on:click={cancelOrder}>
{cancelled ? 'Cancelled!!' : 'Cancel'}
</Button>
</div>
1 change: 0 additions & 1 deletion src/components/creator/FinalStep.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
console.log(event)
$partnerState = PartnerState.Completed;
$creatorState = CreatorState.Completed;
$mainOrder.finalTransaction = event.transactionHash;
partnerFinished = true;
_autonomousSwap.removeAllListeners();
Expand Down
152 changes: 152 additions & 0 deletions src/components/partner/AllowanceStep.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<script lang="ts">
import { CreatorState, PartnerState } from "../../lib/enums.js";
import { autonomousSwap, mainOrder, partnerSubOrder, creatorState, partnerState, creatorSubOrder, signer } from "../../lib/stores.js";
import Button from "../base/Button.svelte";
import { IERC1155, IERC721, IERC20} from '../../../core/typechain-types/index.js';
import * as IERC20json from '../../assets/IERC20.json';
import * as IERC721json from '../../assets/IERC721.json';
import * as IERC1155json from '../../assets/IERC1155.json';
import { ethers } from "ethers";
import { onMount } from "svelte";
import Loader from "../base/Loader.svelte";
export let isCreator = false;
let creatorAllowance = false;
let partnerAllowance = false;
let tokenTypes = {
ERC20: '0xec20ec20',
ERC721: '0x80ac58cd',
ERC1155: '0xd9b67a26'
};
let interfaceID;
if (isCreator){
interfaceID = $creatorSubOrder.interfaceID;
creatorResponse()
} else {
interfaceID = $partnerSubOrder.interfaceID;
partnerResponse()
}
async function creatorResponse(){
if (creatorAllowance) return;
let response = await isAllowed($creatorSubOrder.token, $mainOrder.creator, interfaceID, $creatorSubOrder.quantity, $creatorSubOrder.tokenId)
console.log(response)
if (response) {
creatorAllowance = true;
$creatorState = CreatorState.WaitingPartnerAllowance;
}
console.log(partnerAllowance)
setTimeout(creatorResponse, 1000);
}
async function partnerResponse(){
if (partnerAllowance) return;
let response = await isAllowed($partnerSubOrder.token, $mainOrder.partner, interfaceID, $partnerSubOrder.quantity, $partnerSubOrder.tokenId)
console.log(response)
if (response) {
partnerAllowance = true;
$partnerState = PartnerState.WaitingCreatorAllowance;
}
console.log(partnerAllowance)
setTimeout(partnerResponse, 1000);
}
async function isAllowed(tokenAddress, owner, interfaceID, quantity, tokenId):Promise<boolean> {
console.log(tokenAddress, interfaceID, quantity, tokenId)
let contract: IERC20 & IERC721 & IERC1155 & ethers.BaseContract;
let autonomousSwapAddress = await $autonomousSwap.getAddress();
let result;
if (interfaceID == tokenTypes.ERC20){
contract = new ethers.Contract(tokenAddress, IERC20json.abi, $signer) as IERC20 | any;
result = await contract.allowance(owner, autonomousSwapAddress);
console.log(result)
if (result >= quantity) {
return true;
} else {
return false;
}
} else
if (interfaceID == tokenTypes.ERC721){
contract = new ethers.Contract(tokenAddress, IERC721json.abi, $signer) as IERC20 | any;
result = await contract.getApproved(tokenId);
console.log(result)
if (result == autonomousSwapAddress) {
return true;
} else {
return false;
}
} else {
contract = new ethers.Contract(tokenAddress, IERC1155json.abi, $signer) as IERC20 | any;
result = await contract.isApprovedForAll(owner, autonomousSwapAddress);
console.log(result)
if (result) {
return true;
} else {
return false;
}
}
}
function getContract(address, interfaceID,): IERC20 & IERC721 & IERC1155 & ethers.BaseContract {
let contract: IERC20 & IERC721 & IERC1155 & ethers.BaseContract;
if (interfaceID == tokenTypes.ERC20){
return contract = new ethers.Contract(address, IERC20json.abi, $signer) as IERC20 | any;
} else
if (interfaceID == tokenTypes.ERC721){
return contract = new ethers.Contract(address, IERC721json.abi, $signer) as IERC721 | any;
} else {
return contract = new ethers.Contract(address, IERC1155json.abi, $signer) as IERC1155 | any;
}
}
async function requestAllowance(tokenAddress){
let autonomousSwapAddress = await $autonomousSwap.getAddress();
let transactionResponse;
let contract = getContract(tokenAddress, interfaceID);
if (interfaceID == tokenTypes.ERC20){
transactionResponse = await contract.approve(autonomousSwapAddress, $creatorSubOrder.quantity);
} else
if (interfaceID == tokenTypes.ERC721){
transactionResponse = await contract.approve(autonomousSwapAddress, $creatorSubOrder.tokenId);
} else {
transactionResponse = await contract.setApprovalForAll(autonomousSwapAddress, true);
}
const transactionReceipt = await transactionResponse.wait();
if (transactionReceipt.status == 1){
creatorAllowance = true;
}
}
$: console.log(creatorAllowance, partnerAllowance)
</script>


{#if isCreator}
{#if creatorAllowance}
<p>Allowance approved!</p>
{:else}
Awaiting Allowance ...
<Loader></Loader>
{/if}

{:else}
{#if partnerAllowance}
<p>Allowance approved!</p>
{:else}
<Button on:click={() => requestAllowance($partnerSubOrder.token)}>Give Allowance</Button>
{/if}
{/if}
Loading

0 comments on commit 76cfb02

Please sign in to comment.