Skip to content

Commit

Permalink
udpate
Browse files Browse the repository at this point in the history
  • Loading branch information
blockchain-develop committed Aug 12, 2022
0 parents commit 34aabc8
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 0 deletions.
Empty file added README.md
Empty file.
80 changes: 80 additions & 0 deletions token-in-native/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Token in native

## Prerequisites

[Install Aptos CLI](https://aptos.dev/cli-tools/aptos-cli-tool/install-aptos-cli)

[Create an account and fund](https://aptos.dev/cli-tools/aptos-cli-tool/use-aptos-cli)

## Build Module

```
aptos move compile --package-dir . --named-addresses NamedAddr=0x697c173eeb917c95a382b60f546eb73a4c6a2a7b2d79e6c56c87104f9c04345f
```

```
{
"Result": [
"697C173EEB917C95A382B60F546EB73A4C6A2A7B2D79E6C56C87104F9C04345F::usdt"
]
}
```

## Publish Module

```
aptos move publish --package-dir . --named-addresses NamedAddr=0x697c173eeb917c95a382b60f546eb73a4c6a2a7b2d79e6c56c87104f9c04345f
```

check publish successful.

```
aptos account list --query modules --account 0x697c173eeb917c95a382b60f546eb73a4c6a2a7b2d79e6c56c87104f9c04345f
```

we can see account modules.

```
{
"Result": [
{
"bytecode": "0xa11ceb0b050000000501000202020407061a0820200a400500000001000004757364740855534454436f696e0b64756d6d795f6669656c64697c173eeb917c95a382b60f546eb73a4c6a2a7b2d79e6c56c87104f9c04345f000201020100",
"abi": {
"address": "0x697c173eeb917c95a382b60f546eb73a4c6a2a7b2d79e6c56c87104f9c04345f",
"name": "usdt",
"friends": [],
"exposed_functions": [],
"structs": [
{
"name": "USDTCoin",
"is_native": false,
"abilities": [],
"generic_type_params": [],
"fields": [
{
"name": "dummy_field",
"type": "bool"
}
]
}
]
}
}
]
}
```

## initilize usdt

```
aptos move run --function-id 0x1::managed_coin::initialize --type-args 0x697c173eeb917c95a382b60f546eb73a4c6a2a7b2d79e6c56c87104f9c04345f::usdt::USDTCoin --args string:usdt string:USDT u64:6 bool:false
```

## register recipient


## mint


## transfer

10 changes: 10 additions & 0 deletions token-in-native/contract/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "usdt"
version = "0.0.0"

[dependencies]
MoveStdlib = { git = "https://github.com/move-language/move.git", subdir = "language/move-stdlib", rev = "main" }

[addresses]
std = "0x1"
NamedAddr = "_"
3 changes: 3 additions & 0 deletions token-in-native/contract/sources/usdt.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module NamedAddr::usdt {
struct USDTCoin {}
}
21 changes: 21 additions & 0 deletions token/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Token

## Prerequisites

[Install Aptos CLI](https://aptos.dev/cli-tools/aptos-cli-tool/install-aptos-cli)

[Create an account and fund](https://aptos.dev/cli-tools/aptos-cli-tool/use-aptos-cli)

## Build Module

```
aptos move compile --package-dir . --named-addresses NamedAddr=0x697c173eeb917c95a382b60f546eb73a4c6a2a7b2d79e6c56c87104f9c04345f
```

```
{
"Result": [
"697C173EEB917C95A382B60F546EB73A4C6A2A7B2D79E6C56C87104F9C04345F::usdt"
]
}
```
9 changes: 9 additions & 0 deletions token/contract/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "usdt"
version = "0.0.0"

[dependencies]
MoveStdlib = { git = "https://github.com/move-language/move.git", subdir = "language/move-stdlib", rev = "main" }

[addresses]
NamedAddr = "_"
130 changes: 130 additions & 0 deletions token/contract/sources/usdt.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/// This module defines a minimal Coin and Balance.
module NamedAddr::usdt {
use std::signer;

/// Address of the owner of this module
const MODULE_OWNER: address = @NamedAddr;

/// Error codes
const ENOT_MODULE_OWNER: u64 = 0;
const EINSUFFICIENT_BALANCE: u64 = 1;
const EALREADY_HAS_BALANCE: u64 = 2;

struct Coin has store {
value: u64
}

/// Struct representing the balance of each address.
struct Balance has key {
coin: Coin
}

/// Publish an empty balance resource under `account`'s address. This function must be called before
/// minting or transferring to the account.
public fun publish_balance(account: &signer) {
let empty_coin = Coin { value: 0 };
assert!(!exists<Balance>(signer::address_of(account)), EALREADY_HAS_BALANCE);
move_to(account, Balance { coin: empty_coin });
}

/// Mint `amount` tokens to `mint_addr`. Mint must be approved by the module owner.
public fun mint(module_owner: &signer, mint_addr: address, amount: u64) acquires Balance {
// Only the owner of the module can initialize this module
assert!(signer::address_of(module_owner) == MODULE_OWNER, ENOT_MODULE_OWNER);

// Deposit `amount` of tokens to `mint_addr`'s balance
deposit(mint_addr, Coin { value: amount });
}

/// Returns the balance of `owner`.
public fun balance_of(owner: address): u64 acquires Balance {
borrow_global<Balance>(owner).coin.value
}

/// Transfers `amount` of tokens from `from` to `to`.
public fun transfer(from: &signer, to: address, amount: u64) acquires Balance {
let check = withdraw(signer::address_of(from), amount);
deposit(to, check);
}

/// Withdraw `amount` number of tokens from the balance under `addr`.
fun withdraw(addr: address, amount: u64) : Coin acquires Balance {
let balance = balance_of(addr);
// balance must be greater than the withdraw amount
assert!(balance >= amount, EINSUFFICIENT_BALANCE);
let balance_ref = &mut borrow_global_mut<Balance>(addr).coin.value;
*balance_ref = balance - amount;
Coin { value: amount }
}

/// Deposit `amount` number of tokens to the balance under `addr`.
fun deposit(addr: address, check: Coin) acquires Balance{
let balance = balance_of(addr);
let balance_ref = &mut borrow_global_mut<Balance>(addr).coin.value;
let Coin { value } = check;
*balance_ref = balance + value;
}

#[test(account = @0x1)]
#[expected_failure] // This test should abort
fun mint_non_owner(account: signer) acquires Balance {
// Make sure the address we've chosen doesn't match the module
// owner address
publish_balance(&account);
assert!(signer::address_of(&account) != MODULE_OWNER, 0);
mint(&account, @0x1, 10);
}

#[test(account = @NamedAddr)]
fun mint_check_balance(account: signer) acquires Balance {
let addr = signer::address_of(&account);
publish_balance(&account);
mint(&account, @NamedAddr, 42);
assert!(balance_of(addr) == 42, 0);
}

#[test(account = @0x1)]
fun publish_balance_has_zero(account: signer) acquires Balance {
let addr = signer::address_of(&account);
publish_balance(&account);
assert!(balance_of(addr) == 0, 0);
}

#[test(account = @0x1)]
#[expected_failure(abort_code = 2)] // Can specify an abort code
fun publish_balance_already_exists(account: signer) {
publish_balance(&account);
publish_balance(&account);
}

#[test]
#[expected_failure]
fun balance_of_dne() acquires Balance {
balance_of(@0x1);
}

#[test]
#[expected_failure]
fun withdraw_dne() acquires Balance {
// Need to unpack the coin since `Coin` is a resource
Coin { value: _ } = withdraw(@0x1, 0);
}

#[test(account = @0x1)]
#[expected_failure] // This test should fail
fun withdraw_too_much(account: signer) acquires Balance {
let addr = signer::address_of(&account);
publish_balance(&account);
Coin { value: _ } = withdraw(addr, 1);
}

#[test(account = @NamedAddr)]
fun can_withdraw_amount(account: signer) acquires Balance {
publish_balance(&account);
let amount = 1000;
let addr = signer::address_of(&account);
mint(&account, addr, amount);
let Coin { value } = withdraw(addr, amount);
assert!(value == amount, 0);
}
}

0 comments on commit 34aabc8

Please sign in to comment.