Skip to content

Commit

Permalink
fix: ERC20 total supply should be static. Added swappedSupply with ci…
Browse files Browse the repository at this point in the history
…rculating supply
  • Loading branch information
veeso committed Jan 20, 2024
1 parent 1627b4d commit e87138f
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 17 deletions.
2 changes: 2 additions & 0 deletions ethereum/fly/app/src/js/components/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ChangeOwnerForm from './ChangeOwner/ChangeOwnerForm';
import Balance from './Balance';
import TransferForm from './Transfer/TransferForm';
import RenounceOwnershipForm from './RenounceOwnership/RenounceOwnershipForm';
import SwappedSupply from './SwappedSupply';

const Form = () => {
const { status } = useMetaMask();
Expand All @@ -16,6 +17,7 @@ const Form = () => {
status === 'connected' ? (
<Container.FlexCols className="gap-8">
<Balance />
<SwappedSupply />
<Card>
<TransferForm />
</Card>
Expand Down
28 changes: 28 additions & 0 deletions ethereum/fly/app/src/js/components/Form/SwappedSupply.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from 'react';
import { useConnectedMetaMask, useMetaMask } from 'metamask-react';
import Web3Client from '../../web3/Web3Client';
import Container from '../reusable/Container';
import Heading from '../reusable/Heading';
import { ChainId } from '../MetamaskConnect';

const SwappedSupply = () => {
const { account, ethereum, chainId } = useConnectedMetaMask();
const [swappedSupply, setSwappedSupply] = React.useState(0);

React.useEffect(() => {
const client = new Web3Client(account, ethereum, chainId as ChainId);
client.swappedSupply().then((supply) => {
setSwappedSupply(supply);
});
});

return (
<Container.Container>
<Heading.H2>
Total circulating supply (swapped): {swappedSupply}
</Heading.H2>
</Container.Container>
);
};

export default SwappedSupply;
7 changes: 6 additions & 1 deletion ethereum/fly/app/src/js/web3/Web3Client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Web3 from 'web3';

import { ABI, CONTRACT_ADDRESS } from './contracts/MyToken';
import { ABI, CONTRACT_ADDRESS } from './contracts/Fly';
import { ChainId } from '../components/MetamaskConnect';

export default class Web3Client {
Expand Down Expand Up @@ -38,6 +38,11 @@ export default class Web3Client {
return contract.methods.balanceOf(address).call();
}

async swappedSupply(): Promise<number> {
const contract = this.getContract();
return contract.methods.swappedSupply().call();
}

private getContract() {
return new this.web3.eth.Contract(ABI, CONTRACT_ADDRESS[this.chainId]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,20 @@ export const ABI = [
{
inputs: [
{
internalType: 'string',
name: 'name',
type: 'string',
},
{
internalType: 'string',
name: 'symbol',
type: 'string',
internalType: 'address',
name: '_initialOwner',
type: 'address',
},
{
internalType: 'address',
name: 'initialOwner',
name: '_fly_canister_address',
type: 'address',
},
{
internalType: 'uint256',
name: 'initialSupply',
name: '_swapFee',
type: 'uint256',
},
{
internalType: 'uint8',
name: 'decs',
type: 'uint8',
},
],
stateMutability: 'nonpayable',
type: 'constructor',
Expand Down Expand Up @@ -165,6 +155,31 @@ export const ABI = [
name: 'Approval',
type: 'event',
},
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: 'address',
name: '_from',
type: 'address',
},
{
indexed: true,
internalType: 'bytes32',
name: '_principal',
type: 'bytes32',
},
{
indexed: false,
internalType: 'uint256',
name: '_amount',
type: 'uint256',
},
],
name: 'FlySwapped',
type: 'event',
},
{
anonymous: false,
inputs: [
Expand Down Expand Up @@ -289,6 +304,19 @@ export const ABI = [
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'getFlyCanisterAddress',
outputs: [
{
internalType: 'address',
name: '',
type: 'address',
},
],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'name',
Expand Down Expand Up @@ -322,6 +350,63 @@ export const ABI = [
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [
{
internalType: 'uint256',
name: '_swapFee',
type: 'uint256',
},
],
name: 'setSwapFee',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [
{
internalType: 'bytes32',
name: '_recipient',
type: 'bytes32',
},
{
internalType: 'uint256',
name: '_amount',
type: 'uint256',
},
],
name: 'swap',
outputs: [],
stateMutability: 'payable',
type: 'function',
},
{
inputs: [],
name: 'swapFee',
outputs: [
{
internalType: 'uint256',
name: '',
type: 'uint256',
},
],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'swappedSupply',
outputs: [
{
internalType: 'uint256',
name: '',
type: 'uint256',
},
],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'symbol',
Expand All @@ -348,6 +433,24 @@ export const ABI = [
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{
internalType: 'address',
name: '_recipient',
type: 'address',
},
{
internalType: 'uint256',
name: '_amount',
type: 'uint256',
},
],
name: 'transcribeSwap',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [
{
Expand Down
12 changes: 12 additions & 0 deletions ethereum/fly/contracts/Fly.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ contract Fly is ERC20, Ownable {
return _decimals;
}

function totalSupply() public view virtual override returns (uint256) {
return 8_880_101_010_000_000_000;
}

/**
* @dev Returns the total supply of tokens swapped from Ethereum blockchain to IC. Basically it's the circulating supply of the token on Ethereum.
* @return The total supply of tokens swapped from Ethereum blockchain to IC.
*/
function swappedSupply() public view returns (uint256) {
return ERC20.totalSupply();
}

/**
* @dev Returns the address of the fly canister.
* @return The address of the fly canister.
Expand Down
10 changes: 9 additions & 1 deletion ethereum/fly/test/Fly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ethers } from "hardhat";
import { Fly } from "../typechain-types";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";

// const INITIAL_SUPPLY = "8880101010000000000"; // 8 milions
const TOTAL_SUPPLY = "8880101010000000000"; // 8 milions
const NAME = "Fly";
const SYMBOL = "FLY";
const DECIMALS = 12;
Expand Down Expand Up @@ -119,6 +119,14 @@ describe("Fly", () => {
expect(await token.balanceOf(owner.address)).to.equal(750);
});

it("should get total supply and swapped supply", async () => {
const { flyCanister, owner, token } = deploy;
await token.transcribeSwap(owner.address, 1_000);

expect(await token.totalSupply()).to.equal(TOTAL_SUPPLY);
expect(await token.swappedSupply()).to.equal(1_000);
});

it("Should update swap fee", async () => {
const { token } = deploy;
await token.setSwapFee(200);
Expand Down

0 comments on commit e87138f

Please sign in to comment.