AF
Size: a a a
AF
AF
AF
AF
AF
import "allocator/arena";
export { memory };
import { context, storage, near } from "./near";
// --- contract code goes below
function balanceKey(address: string): string {
return "balances:" + address;
}
function approvedKey(from: string, to: string): string {
return "approved:" + from + ":" + to;
}
let TOTAL_SUPPLY: u64 = 1000000;
export function init(initialOwner: string): void {
near.log("initialOwner: " + initialOwner);
assert(storage.getItem("init") == null, "Already initialized token supply");
storage.setU64(balanceKey(initialOwner), TOTAL_SUPPLY);
storage.setItem("init", "done");
}
export function totalSupply(): string {
return TOTAL_SUPPLY.toString();
}
export function balanceOf(tokenOwner: string): u64 {
let ownerKey = balanceKey(tokenOwner);
near.log("balanceOf: " + tokenOwner);
let result = storage.getU64(ownerKey);
near.log("result: " + result.toString());
return result;
}
export function allowance(tokenOwner: string, spender: string): u64 {
let spenderKey = approvedKey(tokenOwner, spender);
return storage.getU64(spenderKey);
}
export function transfer(to: string, tokens: u64): boolean {
near.log("transfer: " + to + " tokens: " + tokens.toString());
let fromKey = balanceKey(context.sender);
let toKey = balanceKey(to);
near.log("from: " + fromKey + " to: " + toKey);
let fromAmount = storage.getU64(fromKey);
assert(fromAmount >= tokens, "not enough tokens on account");
storage.setU64(fromKey, fromAmount - tokens);
storage.setU64(toKey, storage.getU64(toKey) + tokens);
return true;
}
export function approve(spender: string, tokens: u64): boolean {
let spenderKey = approvedKey(context.sender, spender);
storage.setU64(spenderKey, tokens);
return true;
}
export function transferFrom(from: string, to: string, tokens: u64): boolean {
let fromKey = balanceKey(from);
let toKey = balanceKey(to);
let spenderKey = approvedKey(context.sender, to);
let fromAmount = storage.getU64(fromKey);
assert(fromAmount >= tokens, "not enough tokens on account");
storage.setU64(fromKey, fromAmount - tokens);
let approvedAmount = storage.getU64(spenderKey)
assert(fromAmount >= tokens, "not enough tokens approved");
storage.setU64(spenderKey, approvedAmount - tokens);
storage.setU64(toKey, storage.getU64(toKey) + tokens);
return true;
}
AF
NK
AF
#[eth_abi(Endpoint, Client)]
pub trait TokenContract {
fn constructor(&mut self, _total_supply: U256);
/// What is the balance of a particular account?
#[constant]
fn balanceOf(&mut self, _owner: Address) -> U256;
/// Total amount of tokens
#[constant]
fn totalSupply(&mut self) -> U256;
/// Transfer the balance from owner's account to another account
fn transfer(&mut self, _to: Address, _amount: U256) -> bool;
/// Send _value amount of tokens from address _from to address _to
/// The transferFrom method is used for a withdraw workflow, allowing contracts to send
/// tokens on your behalf, for example to "deposit" to a contract address and/or to charge
/// fees in sub-currencies; the command should fail unless the _from account has
/// deliberately authorized the sender of the message via some mechanism; we propose
/// these standardized APIs for approval:
fn transferFrom(&mut self, _from: Address, _to: Address, _amount: U256) -> bool;
/// Allow _spender to withdraw from your account, multiple times, up to the _value amount.
/// If this function is called again it overwrites the current allowance with _value.
fn approve(&mut self, _spender: Address, _value: U256) -> bool;
/// Check the amount of tokens spender have right to spend on behalf of owner
fn allowance(&mut self, _owner: Address, _spender: Address) -> U256;
#[event]
fn Transfer(&mut self, indexed_from: Address, indexed_to: Address, _value: U256);
#[event]
fn Approval(&mut self, indexed_owner: Address, indexed_spender: Address, _value: U256);
}
fn total_supply_key() -> H256 {
H256::from([2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
}
fn owner_key() -> H256 {
H256::from([3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
}
// Reads balance by address
fn read_balance_of(owner: &Address) -> U256 {
U256::from_big_endian(ð::read(&balance_key(owner)))
}
// Reads allowance value using key
// Key generated by allowance_key function
fn read_allowance(key: &H256) -> U256 {
U256::from_big_endian(ð::read(key))
}
// Writes allowance value
// Key generated by allowance_key function
fn write_allowance(key: &H256, value: U256) {
eth::write(key, &value.into())
}
// Generates the "allowance" storage key to map owner and spender
fn allowance_key(owner: &Address, spender: &Address) -> H256 {
let mut keccak = Keccak::new_keccak256();
let mut res = H256::zero();
keccak.update("allowance_key".as_ref());
keccak.update(owner.as_ref());
keccak.update(spender.as_ref());
keccak.finalize(&mut res[..]);
res
}
// Generates a balance key for some address.
// Used to map balances with their owners.
fn balance_key(address: &Address) -> H256 {
let mut key = H256::from(*address);
key.as_fixed_bytes_mut()[0] = 1; // just a naiive "namespace";
key
}
NK
AF
AF
AF
pwasm-token-example
год уже как на ГХf
AF