Implementation of ERC20 Token
, Pair
,Factory
, Flash Swapper
, and WCSPR
Contract for the CasperLabs platform.
Quantstamp - commit 237cf3fa15ee95843603ab3857789cf2b53d58b7
There are 5 contracts in this folder
- ERC20 Token Contract
- Pair Contract
- Factory Contract
- FLASH SWAPPER Contract
- WCSPR Contract
https://docs.google.com/document/d/1gWQ3rlti59PuyohknkbpP59exC0YtNGuYDMQyuBDUws/edit?usp=sharing
- Interacting with the contract
- Deploying ERC20 contract manually
- Deploying WCSPR contract manually
- Deploying PAIR contract manually
- Deploying FACTORY contract manually
- Deploying FLASH SWAPPER contract manually
You need to have casper-client
and jq
installed on your system to run the examples. The instructions have been tested on Ubuntu 20.04.0 LTS.
You can install the required software by issuing the following commands. If you are on an up-to-date Casper node, you probably already have all of the prerequisites installed so you can skip this step.
sudo apt update
sudo apt install jq -y
Choose cutomize intallation to install nightly version Install the nightly version (by default stable toolchain is installed)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup install nightly-2022-08-29
rustup toolchain list
rustup default nightly-2022-08-29-x86_64-unknown-linux-gnu
rustup target add wasm32-unknown-unknown
rustup --version
sudo apt-get -y install cmake
Note:https://cgold.readthedocs.io/en/latest/first-step/installation.html
cmake --version
cargo install cargo-casper
echo "deb https://repo.casperlabs.io/releases" bionic main | sudo tee -a /etc/apt/sources.list.d/casper.list
curl -O https://repo.casperlabs.io/casper-repo-pubkey.asc
sudo apt-key add casper-repo-pubkey.asc
sudo apt update
sudo apt install libssl-dev
sudo apt install pkg-config
cargo +nightly-2022-08-29-x86_64-unknown-linux-gnu install casper-client
casper-client --version
casper-client --help
casper-client <command> --help
casper-client keygen keys
The keys can be funded from casper live website testnet faucet. Requires chrome browser and the casper signer extension. You should import the keys that were generated in the previous step
Make sure wasm32-unknown-unknown
is installed.
make prepare
It's also recommended to have wasm-strip available in your PATH to reduce the size of compiled Wasm.
Run this command to build specific smart contract.
make build-contract-erc20
make build-contract-wcspr
make build-contract-factory
make build-contract-flashswapper
make build-contract-pair
Run this command in main folder to build all Smart Contract.
make build-all
Run this command to run Test Cases.
make test-erc20
make test-wcspr
make test-factory
make test-flashswapper
make test-pair
Run this command in main folder to run all contract's Test Cases.
make test-all
Run this command in main folder to build & test all contract's
make all
All contracts have already being deployed. Inorder to interact with the specific contract you need to call it by its hash. The table below contains the contract hash (without the hash-
prefix) for all the contracts on public Casper networks:
Network | Contract Name | Account info contract hash |
---|---|---|
Testnet | ERC20 | hash-279445c140615fd511759dfb96c610dee212769913f61a57b0f9dde42d6a8d10 |
Testnet | WCSPR | hash-4f2d1b772147b9ce3706919fe0750af6964249b0931e2115045f97e1e135e80b |
Testnet | FLASHSWAPPER | hash-1c23f9e89033e5c2d2a21a6926411b2645c000cf43fc0db495821633da2aed6e |
Testnet | PAIR | hash-de6ba94b699dad44e12bf98e35c1122eed7dba9eed8af6d8952875afaec8c7dd |
Testnet | FACTORY | hash-13cc83616c3fb4e6ea22ead5e61eb6319d728783ed02eab51b1f442085e605a7 |
If you need to deploy the ERC20 contract
manually you need to pass the some parameters. Following is the command to deploy the ERC20 contract
.
sudo casper-client put-deploy \
--chain-name chain_name \
--node-address http://$NODE_ADDRESS:7777/ \
--secret-key path_to_secret_key.pem \
--session-path path_to_wasm_file \
--payment-amount 160000000000 \
--session-arg="public_key:public_key='Public Key In Hex'" \
--session-arg="name:string='token-name'" \
--session-arg="symbol:string='token-symbol'" \
--session-arg="decimals:u8='unsigned integer value'" \
--session-arg="initial_supply:u256='unsigned integer value'" \
--session-arg="contract_name:string='contract_name'"
Following are the ERC20's entry point methods.
-
Lets
self.get_caller()
send pool tokens to a recipient hash.Following is the table of parameters.
Parameter Name Type recipient Address amount U256 This method returns nothing.
-
Sends pool tokens from one hash to another.
User needs to call approve method before calling thetranfer_from
.Following is the table of parameters.
Parameter Name Type owner Address recipient Address amount U256 This method returns nothing.
Recommendation: The exploit is mitigated through use of functions that increase/decrease the allowance relative to its current value, such asincreaseAllowance()
anddecreaseAllowance()
, Pending community agreement on an ERC standard that would protect against this exploit, we recommend that developers of applications dependent on approve() / transferFrom() should keep in mind that they have to set allowance to 0 first and verify if it was used before setting the new value.
Note: Teams who decide to wait for such a standard should make these recommendations to app developers who work with their token contract. -
Lets
self.get_caller()
set their allowance for a spender.
user needs to call thisapprove
method before calling thetransfer_from
method.Following is the table of parameters.
Parameter Name Type spender Address amount U256 This method returns nothing.
Recommendation: The exploit is mitigated through use of functions that increase/decrease the allowance relative to its current value, such asincreaseAllowance()
anddecreaseAllowance()
, Pending community agreement on an ERC standard that would protect against this exploit, we recommend that developers of applications dependent on approve() / transferFrom() should keep in mind that they have to set allowance to 0 first and verify if it was used before setting the new value.
Note: Teams who decide to wait for such a standard should make these recommendations to app developers who work with their token contract. -
This method will return the balance of owner in
ERC20 Contract
.Following is the table of parameters.
Parameter Name Type address Address This method returns
U256
. -
Returns the amount of liquidity tokens owned by an hash that a spender is allowed to transfer via
transfer_from
.Following is the table of parameters.
Parameter Name Type owner Address spender Address This method returns
U256
. -
Increase the allowance of the spender.
Following is the table of parameters.
Parameter Name Type spender Address amount U256 This method returns nothing.
-
Decrease the allowance of the spender.
Following is the table of parameters.
Parameter Name Type spender Address amount U256 This method returns nothing.
-
Returns the total amount of pool tokens for a pair.
Following is the table of parameters.
Parameter Name Type This method returns
U256
. -
This method mints the number of tokens provided by user against the hash provided by user.
Following is the table of parameters.
Parameter Name Type to Address amount U256 This method returns nothing.
-
This method burns the number of tokens provided by user against the hash provided by user.
Following is the table of parameters.
Parameter Name Type from Address amount U256 This method returns nothing.
Note: Toburn
the tokens against the hash provided by user, User needs tomint
tokens first inERC20
. -
Returns the
name
of tokens for a pair.Following is the table of parameters.
Parameter Name Type This method returns
String
. -
Returns the
symbol
of tokens for a pair.Following is the table of parameters.
Parameter Name Type This method returns
String
. -
Returns the
decimals
of tokens for a pair.Following is the table of parameters.
Parameter Name Type This method returns
U8
.
If you need to deploy the WCSPR contract
manually you need to pass the some parameters. Following is the command to deploy the WCSPR contract
.
sudo casper-client put-deploy \
--chain-name chain_name \
--node-address http://$NODE_ADDRESS:7777/ \
--secret-key path_to_secret_key.pem \
--session-path path_to_wasm_file \
--payment-amount 345000000000 \
--session-arg="public_key:public_key='Public Key In Hex'" \
--session-arg="name:string='token-name'" \
--session-arg="symbol:string='token-symbol'" \
--session-arg="decimals:u8='unsigned integer value'" \
--session-arg="contract_name:string='contract_name'"
The wcspr
crate contains the implementation of the WCSPR standard.
It can be used as a library to build custom tokens. The code structure allows for easy entry points extensions and overrides.
The following code shows how to override the transfer
method to alwasy mint
one additional token for a sender.
#[derive(Default)]
struct Token(ContractStorage);
impl ContractContext for Token {
fn storage(&self) -> &ContractStorage {
&self.0
}
}
impl WCSPR for Token {}
impl Token {
fn constructor(& self, name: String, symbol: String, decimals: u8, initial_supply: U256) {
WCSPR::init(self, name, symbol, decimals);
WCSPR::mint(self, self.get_caller(), initial_supply);
}
}
The library comes with a vanilla implementation of the WCSPR contract that is
ready to use. It is implemented in wcspr/bin/wcspr_token.rs
and after
compilation the wcspr-token.wasm
file is produced.
The wcspr-tests
crate implements multiple integration test scenarios that
check the compatibility with the WCSPR standard.
Tests provide the WCSPRInstance
struct that can be reused in larger smart
contract projects with multiple WCSPR tokens and other smart contracts
to interact with the instance of an WCSPR token.
Tests are implemented in wcspr-tests/src/wcspr_tests.rs
.
The utility code after review and adoption should be moved to a separate repo
and eventually be added to casper-contract
and casper-engine-test-support
.
struct Token(TestContract);
impl Token {
pub fn new(env: &TestEnv, sender: Sender) -> Token {
Token(TestContract::new(env, "token.wasm", "token_name", sender, runtime_args! {
"initial_supply" => U256::from(1000)
}))
}
pub fn transfer(&self, sender: Sender, recipient: AccountHash, amount: u64) {
self.0.call_contract(
sender,
"transfer",
runtime_args! {
"recipient" => recipient,
"amount" => amount
},
);
}
pub fn balance_of(&self, account: AccountHash) -> u64 {
self.0
.query_dictionary("balances", account.to_string())
.unwrap_or_default()
}
}
#[test]
fn test_multiple_tokens() {
// Prepare the env and users.
let env = TestEnv::new();
let user1 = env.next_user();
let user2 = env.next_user();
// Deploy multiple instances of the same contract
// agains a single virtual machine.
let token1 = Token::new(&env, Sender(user1));
let token2 = Token::new(&env, Sender(user2));
// Transfer tokens.
let amount = 10;
token1.transfer(Sender(user1), user2, amount);
assert_eq!(token1.balance_of(user1), amount);
}
Following are the WCSPR's entry point methods.
-
Lets
self.get_caller()
send pool tokens to a recipient hash.Following is the table of parameters.
Parameter Name Type recipient Address amount U256 This method returns nothing.
-
Sends pool tokens from one hash to another.
User needs to callapprove
method before calling thetranfer_from
.Following is the table of parameters.
Parameter Name Type owner Address recipient Address amount U256 This method returns nothing.
Recommendation: The exploit is mitigated through use of functions that increase/decrease the allowance relative to its current value, such asincreaseAllowance()
anddecreaseAllowance()
, Pending community agreement on an ERC standard that would protect against this exploit, we recommend that developers of applications dependent on approve() / transferFrom() should keep in mind that they have to set allowance to 0 first and verify if it was used before setting the new value.
Note: Teams who decide to wait for such a standard should make these recommendations to app developers who work with their token contract. -
Lets
self.get_caller()
set their allowance for a spender.
user needs to call thisapprove
method before calling thetransfer_from
method.Following is the table of parameters.
Parameter Name Type spender Address amount U256 This method returns nothing.
Recommendation: The exploit is mitigated through use of functions that increase/decrease the allowance relative to its current value, such asincreaseAllowance()
anddecreaseAllowance()
, Pending community agreement on an ERC standard that would protect against this exploit, we recommend that developers of applications dependent on approve() / transferFrom() should keep in mind that they have to set allowance to 0 first and verify if it was used before setting the new value.
Note: Teams who decide to wait for such a standard should make these recommendations to app developers who work with their token contract. -
This method will return the balance of owner in
WCSPR Contract
.Following is the table of parameters.
Parameter Name Type address Address This method returns
U256
. -
Returns the amount of liquidity tokens owned by an hash that a spender is allowed to transfer via
transfer_from
.Following is the table of parameters.
Parameter Name Type owner Address spender Address This method returns
U256
. -
Returns the total amount of pool tokens for a pair.
Following is the table of parameters.
Parameter Name Type This method returns
U256
. -
This method deposits the number of tokens provided by user against the hash provided by user.
Following is the table of parameters.
Parameter Name Type amount U512 purse URef This method returns nothing.
-
This method withdraws the number of tokens provided by user against the hash provided by user.
Following is the table of parameters.
Parameter Name Type amount U512 purse URef This method returns nothing.
Note: Towithdraw
the tokens against the hash provided by user, User needs todeposit
tokens first inWCSPR
. -
Returns the
name
of tokens for a pair.Following is the table of parameters.
Parameter Name Type This method returns
String
. -
Returns the
symbol
of tokens for a pair.Following is the table of parameters.
Parameter Name Type This method returns
String
. -
This method returns the main purse.
Following is the table of parameters.
Parameter Name Type This method returns
URef
. -
This method returns the main purse balance.
Following is the table of parameters.
Parameter Name Type This method returns
U512
. -
Increase the allowance of the spender.
Following is the table of parameters.
Parameter Name Type spender Address amount U256 This method returns nothing.
-
Decrease the allowance of the spender.
Following is the table of parameters.
Parameter Name Type spender Address amount U256 This method returns nothing.
-
Returns the
decimals
of wcspr.Following is the table of parameters.
Parameter Name Type This method returns
U8
.
If you need to deploy the PAIR contract
manually you need to pass the hashes of the other contracts as parameter. Following is the command to deploy the PAIR contract
.
sudo casper-client put-deploy \
--chain-name chain_name \
--node-address http://$NODE_ADDRESS:7777/ \
--secret-key path_to_secret_key.pem \
--session-path path_to_wasm_file \
--payment-amount 440000000000 \
--session-arg="public_key:public_key='Public Key In Hex'" \
--session-arg="name:string='token-name'"
--session-arg="symbol:string='token-symbol'"
--session-arg="decimals:u8='unsigned integer value'"
--session-arg="initial_supply:u256='unsigned integer value'"
--session-arg="callee_package_hash:Key='Flash Swapper Contract Hash'"
--session-arg="factory_hash:Key='Hash of factory Contract'"
--session-arg="contract_name:string='contract_name'"
Before deploying PAIR Contract
, you would need to deploy other contracts first and pass hashes of these contracts to the respective parameters above. We have already deployed these contracts and the tables belows displays the hashes of the contracts.
Name | Network | Account info contract hash |
---|---|---|
Factory | Testnet | hash-13cc83616c3fb4e6ea22ead5e61eb6319d728783ed02eab51b1f442085e605a7 |
Flash Swapper | Testnet | hash-1c23f9e89033e5c2d2a21a6926411b2645c000cf43fc0db495821633da2aed6e |
For manual deployments of these contracts, following are the commands.
sudo casper-client put-deploy \
--chain-name chain_name \
--node-address http://$NODE_ADDRESS:7777/ \
--secret-key path_to_secret_key.pem \
--session-path path_to_wasm_file \
--payment-amount 150000000000 \
--session-arg="public_key:public_key='Public Key In Hex'" \
--session-arg="fee_to_setter:Key='Hash of fee-to-setter Contract'" \
--session-arg="contract_name:string='contract_name'"
sudo casper-client put-deploy \
--chain-name chain_name \
--node-address http://$NODE_ADDRESS:7777/ \
--secret-key path_to_secret_key.pem \
--session-path path_to_wasm_file \
--payment-amount 160000000000 \
--session-arg="public_key:public_key='Public Key In Hex'" \
--session-arg="uniswap_v2_factory:Key='Hash of factory Contract'" \
--session-arg="wcspr:Key='Hash of WCSPR Contract'" \
--session-arg="dai:Key='Hash of DAI Contract'" \
--session-arg="contract_name:string='contract_name'"
The pair
crate contains the implementation of the PAIR Contract and ERC20 standard.
It can be used as a library to create pairs of two erc20 tokens and it can be used to build custom tokens. The code structure allows for easy entry points extensions and overrides.
The following code shows how to override the transfer
method to alwasy mint
one additional token for a sender.
#[derive(Default)]
struct Pair(ContractStorage);
impl ContractContext for Pair {
fn storage(&self) -> &ContractStorage {
&self.0
}
}
impl PAIR for Pair {}
impl Pair {
fn constructor(& self, name: String, symbol: String, decimals: u8, initial_supply: U256, nonce:U256, domain_separator: String, permit_type_hash: String, contract_hash: ContractHash) {
PAIR::init(self, name, symbol, decimals, domain_separator, permit_type_hash, Key::from(contract_hash));
PAIR::mint(self, self.get_caller(), initial_supply);
}
}
The library comes with a vanilla implementation of the PAIR contract and ERC20 contract that is
ready to use. It is implemented in pair/bin/pair_token.rs
and after
compilation the pair-token.wasm
file is produced.
The pair-tests
crate implements multiple integration test scenarios that
check the compatibility with the ERC20 standard.
Tests provide the ERC20Instance
struct that can be reused in larger smart
contract projects with multiple ERC20 tokens and other smart contracts
to interact with the instance of an ERC20 token.
Tests are implemented in pair-tests/src/pair_tests.rs
.
struct Token(TestContract);
impl Token {
pub fn new(env: &TestEnv, sender: Sender) -> Token {
Token(TestContract::new(env, "token.wasm", "token_name", sender, runtime_args! {
"initial_supply" => U256::from(1000)
}))
}
pub fn transfer(&self, sender: Sender, recipient: AccountHash, amount: u64) {
self.0.call_contract(
sender,
"transfer",
runtime_args! {
"recipient" => recipient,
"amount" => amount
},
);
}
pub fn balance_of(&self, account: AccountHash) -> u64 {
self.0
.query_dictionary("balances", account.to_string())
.unwrap_or_default()
}
}
#[test]
fn test_multiple_tokens() {
// Prepare the env and users.
let env = TestEnv::new();
let user1 = env.next_user();
let user2 = env.next_user();
// Deploy multiple instances of the same contract
// agains a single virtual machine.
let token1 = Token::new(&env, Sender(user1));
let token2 = Token::new(&env, Sender(user2));
// Transfer tokens.
let amount = 10;
token1.transfer(Sender(user1), user2, amount);
assert_eq!(token1.balance_of(user1), amount);
}
Following are the PAIR's entry point methods.
-
This function is to pause the pair contract functionalities
Following is the table of parameters.
Parameter Name Type This method returns nothing.
This function is to unpause the pair contract functionalities
Following is the table of parameters.
Parameter Name Type This method returns nothing.
-
Returns the
name
of the pair.Following is the table of parameters.
Parameter Name Type This method returns
String
. -
Returns the
name
of the pair.Following is the table of parameters.
Parameter Name Type This method returns
String
. -
Returns the
decimals
of the pair.Following is the table of parameters.
Parameter Name Type This method returns
U8
. -
Lets
self.get_caller
send pool tokens to a recipient hash.Following is the table of parameters.
Parameter Name Type recipient Address amount U256 This method returns nothing.
-
Sends pool tokens from one hash to another.
Note: User needs to callapprove
method before calling thetranfer_from
.Following is the table of parameters.
Parameter Name Type owner Address recipient Address amount U256 This method returns nothing.
-
Swaps tokens. For regular swaps,
data.length
must be0
.
Note: To call this method explicitly, User needs to deploy aFactory contract
first and call a methodcreate_pair
which invokes theinitialize
methods ofPair contract
that's how thePair contract
can access thetoken0
andtoken1
after this user needs to minttoken0
andtoken1
by calling anmint
method inpair contract
or you can transfer some tokens to it, so they have some balance in them. To call theswap
method the user needs to have some balance inreserve0
andreserve1
.Following is the table of parameters.
Parameter Name Type amount0_out U256 amount1_out U256 to Key data String This method returns nothing.
-
Note: To call this method explicitly, User needs to deploy aFactory contract
first and call a methodcreate_pair
which invokes theinitialize
methods ofPair contract
that's how thePair contract
can access thetoken0
andtoken1
after this user needs to minttoken0
andtoken1
by calling anmint
method inPair contract
or you can transfer some tokens to it, so they have some balance in them. To call theskim
method the user needs to have some balance inreserve0
andreserve1
.Following is the table of parameters.
Parameter Name Type to Key This method returns nothing.
-
Note: To call this method explicitly, User needs to deploy aFactory contract
first and call a methodcreate_pair
which invokes theinitialize
methods ofPair contract
that's how thePair contract
can access thetoken0
andtoken1
after this user needs to minttoken0
andtoken1
by calling anmint
method inPair contract
or you can transfer some tokens to it, so they have some balance in them.Following is the table of parameters.
Parameter Name Type This method returns nothing.
-
Lets
self.get_caller()
set their allowance for a spender.
user needs to call thisapprove
method before calling thetransfer_from
method.Following is the table of parameters.
Parameter Name Type spender Address amount U256 This method returns nothing.
Recommendation: The exploit is mitigated through use of functions that increase/decrease the allowance relative to its current value, such asincreaseAllowance()
anddecreaseAllowance()
, Pending community agreement on an ERC standard that would protect against this exploit, we recommend that developers of applications dependent on approve() / transferFrom() should keep in mind that they have to set allowance to 0 first and verify if it was used before setting the new value.
Note: Teams who decide to wait for such a standard should make these recommendations to app developers who work with their token contract. -
Returns the amount of pool tokens owned by a hash.
Following is the table of parameters.
Parameter Name Type address Address This method returns
U256
. -
Returns the amount of liquidity tokens owned by an hash that a spender is allowed to transfer via
transfer_from
.Following is the table of parameters.
Parameter Name Type owner Address spender Address This method returns
U256
.
Recommendation: The exploit is mitigated through use of functions that increase/decrease the allowance relative to its current value, such asincreaseAllowance()
anddecreaseAllowance()
, Pending community agreement on an ERC standard that would protect against this exploit, we recommend that developers of applications dependent on approve() / transferFrom() should keep in mind that they have to set allowance to 0 first and verify if it was used before setting the new value.
Note: Teams who decide to wait for such a standard should make these recommendations to app developers who work with their token contract. -
Increase the allowance of the spender.
Following is the table of parameters.
Parameter Name Type spender Address amount U256 This method returns nothing.
-
Decrease the allowance of the spender.
Following is the table of parameters.
Parameter Name Type spender Address amount U256 This method returns nothing.
-
Returns the total amount of pool tokens for a pair.
Following is the table of parameters.
Parameter Name Type This method returns
U256
. -
Creates pool tokens.
Note: To call this method explicitly, User needs to deploy aFactory contract
first and call a methodcreate_pair
which invokes theinitialize
methods ofPair contract
that's how thePair contract
can access thetoken0
andtoken1
, To call the mint user needs to do all the above steps so he can proceed flawlessly.Following is the table of parameters.
Parameter Name Type to Key This method returns
U256
. -
Destroys pool tokens.
Note: User needs to mint tokens before burning them. And user needs to deploy aFactory contract
first and call a methodcreate_pair
which invokes theinitialize
method ofPair contract
that's how thePair contract
can access thetoken0
andtoken1
. To call the burn user needs to do all the above steps so he can proceed flawlessly.Following is the table of parameters.
Parameter Name Type to Key This method returns
Tuple(U256, U256)
. -
Returns the Treasury Fee for a pair.
Following is the table of parameters.
Parameter Name Type This method returns
U256
. -
sets the treasury fee for a pair.
Note: treasury_fee_percent Cannot be more than30
and less than 3. If it’s more than30
it will set it as30
and if it's less than 3 it will set it as '3'.Following is the table of parameters.
Parameter Name Type treasury_fee U256 This method returns nothing.
-
Returns the hash of the pair token with the
lower sort order
.Following is the table of parameters.
Parameter Name Type This method returns
Key
. -
Returns the address of the pair token with the
higher sort order
.Following is the table of parameters.
Parameter Name Type This method returns
Key
. -
Sets the
token0
andtoken1
in pair contract.
Note: This method will be called byFactory contract
only and the user needs to pass the factory hash to make sure is it a factory or not.Following is the table of parameters.
Parameter Name | Type |
---|---|
token0 | Key |
token1 | Key |
This method returns nothing.
-
Removes the
token0
andtoken1
in pair contract.Following is the table of parameters.
Parameter Name | Type |
---|
This method returns nothing.
-
Returns the reserves of token0 and token1 used to price trades and distribute liquidity. Also returns the block_time_stamp
(mod 2**32)
of the last block during which an interaction occured for the pair.Following is the table of parameters.
Parameter Name Type This method returns
Tupe3(U128, U128, u64)
.
If you need to deploy the FACTORY contract
manually you need to pass the some parameters. Following is the command to deploy the FACTORY contract
.
sudo casper-client put-deploy \
--chain-name chain_name \
--node-address http://$NODE_ADDRESS:7777/ \
--secret-key path_to_secret_key.pem \
--session-path path_to_wasm_file \
--payment-amount 150000000000 \
--session-arg="public_key:public_key='Public Key In Hex'" \
--session-arg="fee_to_setter:Key='Account Hash of a user'" \
--session-arg="contract_name:string='contract_name'"
Implementation of the FACTORY standard for the Casper platform.
The factory
crate contains the implementation of the FACTORY standard.
It can be used as a library to creat pairs by deploying the PAIR Contract. The code structure allows for easy entry points extensions and overrides.
The library comes with a vanilla implementation of the FACTORY contract that is
ready to use. It is implemented in factory/bin/factory.rs
and after
compilation the factory.wasm
file is produced.
The factory-tests
crate implements multiple integration test scenarios that
check the compatibility with the FACTORY standard.
Tests provide the FACTORYInstance
struct that can be reused in larger smart
contract projects with multiple FACTORY and other smart contracts
to interact with the instance of an FACTORY.
Tests are implemented in factory-tests/src/factory_tests.rs
.
Following are the FACTORY's entry point methods.
-
Creates a pair for
token_a
andtoken_b
if one doesn't exist already.
Note:token_a
andtoken_b
are interchangeable and The user needs to deploy the pair contract before calling the create pair method so he can pass thePair contract
hash as a parameter which allows theFactory contract
to call theinitialize
methods ofPair Contract
.Following is the table of parameters.
Parameter Name Type token_a Key token_b Key pair_hash Key This method returns nothing.
-
Removes an existing pair for
token_a
andtoken_b
.Following is the table of parameters.
Parameter Name Type pair_hash Key This method returns nothing.
-
Returns the hash of the pair for
token0
andtoken1
, if it has been created, else“Hash-0000000000000000000000000000000000000000000000000000000000000000”
.
Note:token0
andtoken1
are interchangeable.Following is the table of parameters.
Parameter Name Type token0 Key token1 Key This method returns
Key
. -
Returns the hash of
fee_to
.Following is the table of parameters.
Parameter Name Type This method returns
Key
. -
Returns the hash of
fee_to_setter
.Following is the table of parameters.
Parameter Name Type This method returns
Key
. -
Returns the list of all pairs created.
Following is the table of parameters.
Parameter Name Type This method returns
list of Keys
. -
Returns the total number of pairs created through the
factory
so far.Following is the table of parameters.
Parameter Name Type This method returns
U256
. -
this will set the hash of
fee_to
Note: Onlyfee_to_setter
can set thefee_to
Following is the table of parameters.
Parameter Name Type fee_to Key This method returns nothing.
-
this will set the Hash of
fee_to_setter
Note: Onlyfee_to_setter
can set thefee_to_setter
Following is the table of parameters.
Parameter Name Type fee_to_setter Key This method returns nothing.
-
This function is to set the white list addresses which is only possible if the caller matched with owners's hash.
Following is the table of parameters.
Parameter Name Type white_list Key This method returns nothing.
If you need to deploy the Flash swapper contract
manually you need to pass the hashes of the other contracts as parameter. Following is the command to deploy the Flash Swapper contract
.
sudo casper-client put-deploy \
--chain-name chain_name \
--node-address http://$NODE_ADDRESS:7777/ \
--secret-key path_to_secret_key.pem \
--session-path path_to_wasm_file \
--payment-amount 160000000000 \
--session-arg="public_key:public_key='Public Key In Hex'" \
--session-arg="uniswap_v2_factory:Key='Hash of factory Contract'" \
--session-arg="wcspr:Key='Hash of WCSPR Contract'" \
--session-arg="dai:Key='Hash of DAI Contract'" \
--session-arg="contract_name:string='contract_name'"
Before deploying Flash Swapper Contract
, you would need to deploy other contracts first and pass hashes of these contracts to the respective parameters above. We have already deployed these contracts and the tables belows displays the hashes of the contracts.
Name | Network | Account info contract hash |
---|---|---|
Factory | Testnet | hash-13cc83616c3fb4e6ea22ead5e61eb6319d728783ed02eab51b1f442085e605a7 |
Wcspr | Testnet | hash-4f2d1b772147b9ce3706919fe0750af6964249b0931e2115045f97e1e135e80b |
Dai | Testnet | hash-ffb8fa3073c7623484f76d79bc8baad110b24936b92d5ebc854d401895e88c95 |
For manual deployments of these contracts, following are the commands.
sudo casper-client put-deploy \
--chain-name chain_name \
--node-address http://$NODE_ADDRESS:7777/ \
--secret-key path_to_secret_key.pem \
--session-path path_to_wasm_file \
--payment-amount 150000000000 \
--session-arg="public_key:public_key='Public Key In Hex'" \
--session-arg="fee_to_setter:Key='Hash of fee-to-setter Contract'" \
--session-arg="contract_name:string='contract_name'"
sudo casper-client put-deploy \
--chain-name chain_name \
--node-address http://$NODE_ADDRESS:7777/ \
--secret-key path_to_secret_key.pem \
--session-path path_to_wasm_file \
--payment-amount 345000000000 \
--session-arg="public_key:public_key='Public Key In Hex'" \
--session-arg="name:string='token-name'" \
--session-arg="symbol:string='token-symbol'" \
--session-arg="decimals:u8='unsigned integer value'" \
--session-arg="contract_name:string='contract_name'"
sudo casper-client put-deploy \
--chain-name chain_name \
--node-address http://$NODE_ADDRESS:7777/ \
--secret-key path_to_secret_key.pem \
--session-path path_to_wasm_file \
--payment-amount 160000000000 \
--session-arg="public_key:public_key='Public Key In Hex'" \
--session-arg="name:string='token-name'" \
--session-arg="symbol:string='token-symbol'" \
--session-arg="decimals:u8='unsigned integer value'" \
--session-arg="contract_name:string='contract_name'"
Following are the Flash Swapper's entry point methods.
-
This method will start swap.
Special Instructions: The user needs to call this (start_swap) method first to call theuniswap_v2_call
method.Start_swap
method will further call 3 methods -
simple_flash_loan This method will be invoked if both tokens (token_borrow and token_pay) are the same.
-
simple_flash_swap This method will be invoked if both tokens (token_borrow and token_pay) are not the same. one of them must be equal to “Hash-0000000000000000000000000000000000000000000000000000000000000000”
-
triangular_flash_swap This method will be invoked if both tokens (token_borrow and token_pay) are not the same. The above mthods will invoke the swap methods of
Pair
Contract by using thepermissioned_pair_address
. And then theswap
method will invoke theuniswap_v2_call
method.Following is the table of parameters.
Parameter Name Type token_borrow Key amount U256 token_pay Key user_data String This method returns nothing.
-
This method is called by
swap
method ofpair contract
.
the sender must be aFlash Swapper Contract
hash if user data has some value.Uniswap_v2_call
must be called from a contract. Users cannot directly invoke this method.Following is the table of parameters.
Parameter Name Type sender Key amount0 U256 amount1 U256 data String This method returns nothing.
-
Return the purse.
Following is the table of parameters.
Parameter Name Type This method returns
URef
.