Skip to content

Commit

Permalink
Merge branch 'felix/sdk' into graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
huyao committed Oct 31, 2023
2 parents 64f9e22 + 1f64f89 commit f90e0e4
Show file tree
Hide file tree
Showing 31 changed files with 8,456 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ jobs:
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "0.7.0"
- run: cd contracts && scarb test
- run: cd contracts
75 changes: 75 additions & 0 deletions contracts/src/wrap_factory.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

#[starknet::contract]
mod WrapFactory {
use openzeppelin::token::erc20::erc20::ERC20;
use starknet::ContractAddress;
use starknet::{ get_caller_address, get_contract_address};
use zeroable::Zeroable;
use instaswap::erc1155::{IERC1155, IERC1155Dispatcher, IERC1155DispatcherTrait};
use starknet::class_hash::ClassHash;

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
CreateWrapAddress: CreateWrapAddress,
}

#[derive(Drop, starknet::Event)]
struct CreateWrapAddress {
erc1155_address: ContractAddress,
token_id: u256,
wrap_address: ContractAddress,
}

#[storage]
struct Storage {
map: LegacyMap::<(ContractAddress, u256), ContractAddress>, // map of (erc1155_address, token_id) to wrap_address
wrap_hash: felt252, // hash of Wrap class
}

#[constructor]
fn constructor(
ref self: ContractState,
wrap_hash_: felt252,
) {
self.wrap_hash.write(wrap_hash_);
}

#[external(v0)]
#[generate_trait]
impl WrapFactoryInterfaceImpl of WrapFactoryInterface {
fn create_wrap_address(ref self: ContractState, erc1155_address: ContractAddress, token_id: u256, name: felt252, symbol: felt252) {
let wrap_address = self.map.read((erc1155_address, token_id));
let wrap_hash = self.wrap_hash.read();
assert(wrap_address.is_zero(), 'Already wrapped');
let mut calldata = Default::default();
erc1155_address.serialize(ref calldata);
token_id.serialize(ref calldata);
name.serialize(ref calldata);
symbol.serialize(ref calldata);
let (address, _) = starknet::deploy_syscall(wrap_hash.try_into().unwrap(), 0, calldata.span(), false)
.unwrap();
// emit event
self
.emit(
Event::CreateWrapAddress(
CreateWrapAddress {
erc1155_address: erc1155_address,
token_id: token_id,
wrap_address: address,
}
)
);
self.map.write((erc1155_address, token_id), address);

}

fn get_wrap_address(self: @ContractState, erc1155_address: ContractAddress, token_id: u256) -> ContractAddress {
let wrap_address = self.map.read((erc1155_address, token_id));
assert(!wrap_address.is_zero(), 'Not wrapped');
wrap_address
}

}

}
20 changes: 20 additions & 0 deletions examples/interface/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"overrides": [],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"project": "./tsconfig.json",
"sourceType": "module"
},
"plugins": ["react", "@typescript-eslint"],
"root": true,
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["off"]
}
}
25 changes: 25 additions & 0 deletions examples/interface/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
.vite
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
Binary file added examples/interface/bun.lockb
Binary file not shown.
13 changes: 13 additions & 0 deletions examples/interface/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Create Starknet</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
Loading

0 comments on commit f90e0e4

Please sign in to comment.