-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0c040b
commit 8692ea7
Showing
10 changed files
with
274 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
```k | ||
module MX-RUST-MODULES-MANAGED-BUFFER | ||
imports private MX-RUST-REPRESENTATION | ||
imports private RUST-SHARED-SYNTAX | ||
// -------------------------------------- | ||
syntax MxRustType ::= "managedBufferType" [function, total] | ||
rule managedBufferType | ||
=> rustStructType | ||
( #token("ManagedBuffer", "Identifier"):Identifier | ||
, ( mxRustStructField | ||
( #token("mx_buffer_id", "Identifier"):Identifier | ||
, MxRust#buffer | ||
) | ||
, .MxRustStructFields | ||
) | ||
) | ||
rule mxValueToRust | ||
( #token("ManagedBuffer", "Identifier") | ||
, V:MxValue | ||
) | ||
=> mxToRustTyped(managedBufferType, mxListValue(V , .MxValueList)) | ||
rule rustValueToMx | ||
( struct | ||
( #token("ManagedBuffer", "Identifier"):Identifier | ||
, #token("mx_buffer_id", "Identifier"):Identifier |-> VecValueId:Int | ||
.Map | ||
) | ||
) | ||
=> mxRustGetBuffer(ptr(VecValueId)) | ||
// -------------------------------------- | ||
endmodule | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mxListValue(mxStringValue("MyToken")), mxListValue(mxStringValue("MTKN")), mxIntValue(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
setCallee("Owner"); | ||
|
||
push mxListValue(); | ||
push mxStringValue("decimals"); | ||
push mxIntValue(0); | ||
push mxTransfersValue(); | ||
push mxIntValue(0); | ||
push mxStringValue("TestContract"); | ||
call 6 MX#managedExecuteOnDestContext; | ||
check_eq mxIntValue(0); | ||
|
||
push_return_value; | ||
check_eq mxIntValue(18); | ||
|
||
push mxListValue(); | ||
push mxStringValue("name"); | ||
push mxIntValue(0); | ||
push mxTransfersValue(); | ||
push mxIntValue(0); | ||
push mxStringValue("TestContract"); | ||
call 6 MX#managedExecuteOnDestContext; | ||
check_eq mxIntValue(0); | ||
|
||
push_return_value; | ||
check_eq mxStringValue("MyToken"); | ||
|
||
push mxListValue(); | ||
push mxStringValue("symbol"); | ||
push mxIntValue(0); | ||
push mxTransfersValue(); | ||
push mxIntValue(0); | ||
push mxStringValue("TestContract"); | ||
call 6 MX#managedExecuteOnDestContext; | ||
check_eq mxIntValue(0); | ||
|
||
push_return_value; | ||
check_eq mxStringValue("MTKN") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// This contract is a translation of | ||
// https://github.com/Pi-Squared-Inc/pi2-examples/blob/b63d0a78922874a486be8a0395a627425fb5a052/solidity/src/tokens/SomeToken.sol | ||
// | ||
// The main change is that the contract does not issue specific error objects | ||
// (e.g. ERC20InsufficientBalance), it just calls `require!` with various | ||
// (string) explanations. | ||
// | ||
// Also, the `totalSupply` endpoint is declared implicitely as a view for | ||
// `s_total_supply`. | ||
|
||
#![no_std] | ||
|
||
#[allow(unused_imports)] | ||
use multiversx_sc::imports::*; | ||
|
||
#[multiversx_sc::contract] | ||
pub trait Erc20Token { | ||
#[view(totalSupply)] | ||
#[storage_mapper("total_supply")] | ||
fn s_total_supply(&self) -> SingleValueMapper<BigUint>; | ||
|
||
#[view(getName)] | ||
#[storage_mapper("name")] | ||
fn s_name(&self) -> SingleValueMapper<ManagedBuffer>; | ||
|
||
#[view(getSymbol)] | ||
#[storage_mapper("symbol")] | ||
fn s_symbol(&self) -> SingleValueMapper<ManagedBuffer>; | ||
|
||
#[view(getBalances)] | ||
#[storage_mapper("balances")] | ||
fn s_balances(&self, address: &ManagedAddress) -> SingleValueMapper<BigUint>; | ||
|
||
#[view(getAllowances)] | ||
#[storage_mapper("allowances")] | ||
fn s_allowances(&self, account: &ManagedAddress, spender: &ManagedAddress) -> SingleValueMapper<BigUint>; | ||
|
||
#[event("Transfer")] | ||
fn transfer_event(&self, #[indexed] from: &ManagedAddress, #[indexed] to: &ManagedAddress, value: &BigUint); | ||
#[event("Approval")] | ||
fn approval_event(&self, #[indexed] owner: &ManagedAddress, #[indexed] spender: &ManagedAddress, value: &BigUint); | ||
|
||
|
||
#[init] | ||
fn init(&self, name: &ManagedBuffer, symbol: &ManagedBuffer, init_supply: &BigUint) { | ||
self.s_name().set_if_empty(name); | ||
self.s_symbol().set_if_empty(symbol); | ||
self._mint(&self.blockchain().get_caller(), init_supply); | ||
} | ||
|
||
#[upgrade] | ||
fn upgrade(&self) {} | ||
|
||
#[view(decimals)] | ||
fn decimals(&self) -> u8 { | ||
return 18; | ||
} | ||
|
||
// Already declared above | ||
// #[view(totalSupply)] | ||
// fn total_supply(&self) -> BigUint { | ||
// return self.s_total_supply().get(); | ||
// } | ||
|
||
#[view(name)] | ||
fn name(&self) -> ManagedBuffer { | ||
return self.s_name().get(); | ||
} | ||
|
||
#[view(symbol)] | ||
fn symbol(&self) -> ManagedBuffer { | ||
return self.s_symbol().get(); | ||
} | ||
|
||
#[view(balanceOf)] | ||
fn balance_of(&self, account: &ManagedAddress) -> BigUint { | ||
self.s_balances(account).get() | ||
} | ||
|
||
#[endpoint(transfer)] | ||
fn transfer(&self, to: &ManagedAddress, value: BigUint) -> bool { | ||
let owner = self.blockchain().get_caller(); | ||
self._transfer(&owner, to, &value); | ||
true | ||
} | ||
|
||
#[view(allowance)] | ||
fn allowance(&self, owner: &ManagedAddress, spender: &ManagedAddress) -> BigUint { | ||
self.s_allowances(owner, spender).get() | ||
} | ||
|
||
#[endpoint(approve)] | ||
fn approve(&self, spender: &ManagedAddress, value: &BigUint) -> bool { | ||
let owner = self.blockchain().get_caller(); | ||
self._approve(&owner, spender, value, true); | ||
true | ||
} | ||
|
||
#[endpoint(transferFrom)] | ||
fn transfer_from(&self, from: &ManagedAddress, to: &ManagedAddress, value: &BigUint) -> bool { | ||
let spender = self.blockchain().get_caller(); | ||
self._spend_allowance(from, &spender, value); | ||
self._transfer(from, to, value); | ||
return true; | ||
} | ||
|
||
fn _transfer(&self, from: &ManagedAddress, to: &ManagedAddress, value: &BigUint) { | ||
require!(!from.is_zero(), "Invalid sender"); | ||
require!(!to.is_zero(), "Invalid receiver"); | ||
self._update(from, to, value); | ||
self.transfer_event(from, to, value); | ||
} | ||
|
||
fn _update(&self, from: &ManagedAddress, to: &ManagedAddress, value: &BigUint) { | ||
if from.is_zero() { | ||
self.s_total_supply().set(self.s_total_supply().get() + value); | ||
} else { | ||
let from_balance = self.s_balances(from).get(); | ||
require!(value <= &from_balance, "Insufficient balance"); | ||
self.s_balances(from).set(self.s_balances(from).get() - value); | ||
}; | ||
|
||
if to.is_zero() { | ||
self.s_total_supply().set(self.s_total_supply().get() - value); | ||
} else { | ||
self.s_balances(to).set(self.s_balances(to).get() + value); | ||
} | ||
} | ||
|
||
fn _mint(&self, account: &ManagedAddress, value: &BigUint) { | ||
require!(!account.is_zero(), "Zero address"); | ||
self._update(&ManagedAddress::zero(), account, value); | ||
} | ||
|
||
fn _approve(&self, owner: &ManagedAddress, spender: &ManagedAddress, value: &BigUint, emit_event: bool) { | ||
require!(!owner.is_zero(), "Invalid approver"); | ||
require!(!spender.is_zero(), "Invalid spender"); | ||
self.s_allowances(owner, spender).set(value); | ||
if emit_event { | ||
self.approval_event(owner, spender, value); | ||
} | ||
} | ||
|
||
fn _spend_allowance(&self, owner: &ManagedAddress, spender: &ManagedAddress, value: &BigUint) { | ||
let current_allowance = self.allowance(owner, spender); | ||
require!(value <= ¤t_allowance, "Insuficient allowance"); | ||
self._approve(owner, spender, &(current_allowance - value), false); | ||
} | ||
|
||
} |