Skip to content

Commit

Permalink
feat: add erc1155 extension contract
Browse files Browse the repository at this point in the history
  • Loading branch information
y-pakorn committed Oct 12, 2021
1 parent 3165be8 commit 5435e4e
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion lib/src/utils/erc1155.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import '../ethers/ethers.dart';

/// Dart Class for ERC1155 Contract, A standard API for fungibility-agnostic and gas-efficient tokens within smart contracts.
class ContractERC1155 {
/// Minimal abi interface of ERC20
/// Minimal abi interface of ERC1155
static const abi = [
'function balanceOf(address,uint) view returns (uint)',
'function balanceOfBatch(address[],uint[]) view returns (uint[])',
Expand All @@ -13,6 +13,10 @@ class ContractERC1155 {
'function setApprovedForAll(address spender, bool approved)',
'function safeTransferFrom(address, address, uint, uint, bytes)',
'function safeBatchTransferFrom(address, address, uint[], uint[], bytes)',
'function totalSupply(uint256 id) view returns (uint256)',
'function exists(uint256 id) view returns (bool)',
'function burn(address account, uint256 id, uint256 value)',
'function burnBatch(address account, uint256[] ids, uint256[] values)',
'event ApprovalForAll(address indexed account, address indexed operator, bool approved)',
'event TransferBatch(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] values)',
'event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value)',
Expand Down Expand Up @@ -180,3 +184,60 @@ class ContractERC1155 {
return _uri.replaceAll('{id}', id.toString());
}
}

/// Dart Class for ERC1155Burnable Contract that allows token holders to destroy both their own tokens and those that they have been approved to use.
class ContractERC1155Burnable extends ContractERC1155 with ERC1155Supply {
/// Instantiate ERC1155 Contract using default abi if [abi] is not `null`.
///
/// [isReadOnly] is determined by whether [providerOrSigner] is [Signer] or not.
ContractERC1155Burnable(String address, dynamic providerOrSigner,
[dynamic abi])
: super(address, providerOrSigner, abi);
}

/// Dart Class for ERC1155Supply Contract that adds tracking of total supply per id to normal ERC1155.
class ContractERC1155Supply extends ContractERC1155 with ERC1155Supply {
/// Instantiate ERC1155 Contract using default abi if [abi] is not `null`.
///
/// [isReadOnly] is determined by whether [providerOrSigner] is [Signer] or not.
ContractERC1155Supply(String address, dynamic providerOrSigner, [dynamic abi])
: super(address, providerOrSigner, abi);
}

/// Dart Class for both [ContractERC1155Supply] and [ContractERC1155Burnable] combined.
class ContractERC1155SupplyBurnable extends ContractERC1155
with ERC1155Supply, ERC1155Burnable {
/// Instantiate ERC1155 Contract using default abi if [abi] is not `null`.
///
/// [isReadOnly] is determined by whether [providerOrSigner] is [Signer] or not.
ContractERC1155SupplyBurnable(String address, dynamic providerOrSigner,
[dynamic abi])
: super(address, providerOrSigner, abi);
}

/// Dart Mixin for ERC1155Burnable that allows token holders to destroy both their own tokens and those that they have been approved to use.
mixin ERC1155Burnable on ContractERC1155 {
Future<TransactionResponse> burn(String address, int id, BigInt value) =>
contract.send('burn', [address, id, value.toString()]);

Future<TransactionResponse> burnBatch(
String address, List<int> ids, List<BigInt> values) =>
contract.send('burn', [
address,
ids,
values.map((e) => e.toString()).toList(),
]);
}

/// Dart Mixin for ERC1155Supply that adds tracking of total supply per id to normal ERC1155.
mixin ERC1155Supply on ContractERC1155 {
/// Indicates weither any token exist with a given [id], or not.
Future<bool> exists(int id) async {
return contract.call<bool>('exists', [id]);
}

/// Total amount of tokens in with a given [id].
Future<BigInt> totalSupply(int id) async {
return contract.call<BigInt>('totalSupply', [id]);
}
}

0 comments on commit 5435e4e

Please sign in to comment.