Skip to content

Commit 8b499be

Browse files
authored
feat: fungible proxy (#12)
1 parent 0fca82c commit 8b499be

File tree

18 files changed

+1497
-697
lines changed

18 files changed

+1497
-697
lines changed

.github/workflows/build-and-test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ jobs:
2121
- name: Build and run conversion proxy unit tests
2222
working-directory: ./conversion_proxy
2323
run: cargo test
24+
- name: Build and run fungible proxy unit tests
25+
working-directory: ./fungible_proxy
26+
run: cargo test
2427
- name: Build and run fungible conversion proxy unit tests
2528
working-directory: ./fungible_conversion_proxy
2629
run: cargo test

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ hex = "0.4"
1515
near-sdk-sim = "3.2.0"
1616
conversion_proxy = { path = "./conversion_proxy" }
1717
fungible_conversion_proxy = { path = "./fungible_conversion_proxy" }
18+
fungible_proxy = { path = "./fungible_proxy" }
1819
mocks = { path = "./mocks" }
1920

2021
[profile.release]
@@ -27,4 +28,4 @@ panic = "abort"
2728
overflow-checks = true
2829

2930
[workspace]
30-
members = ["conversion_proxy", "fungible_conversion_proxy", "mocks"]
31+
members = ["conversion_proxy", "fungible_conversion_proxy", "fungible_proxy", "mocks"]

README.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,34 @@ Smart contracts on NEAR used by the
2121

2222
## Unit tests
2323

24+
Run all contracts unit tests like this:
25+
2426
```
2527
cd near-contracts/conversion_proxy
2628
cargo test
2729
cd near-contracts/fungible_conversion_proxy
2830
cargo test
31+
cd near-contracts/fungible_proxy
32+
cargo test
2933
cd near-contracts/mocks
3034
cargo test
3135
```
3236

3337
## Integration tests
3438

3539
```
40+
# To test everything
3641
./test.sh
42+
43+
# To test contracts one by one:
44+
cargo test conversion_proxy
45+
cargo test fungible_conversionproxy
46+
cargo test fungible_proxy
47+
48+
# To run integration tests one by one (examples with main transfers):
49+
cargo test conversion_proxy::test_transfer -- --exact
50+
cargo test fungible_conversionproxy::test_transfer -- --exact
51+
cargo test fungible_proxy::test_transfer -- --exact
3752
```
3853

3954
## Deploying contract
@@ -55,9 +70,44 @@ cargo
5570

5671
## Calling contract
5772

58-
The snippet below makes a NEAR payment for $80.50, with a $1.00 fee.
73+
Commands below assumes a few variables are set: `ACCOUNT_ID`, `BUILDER_ID` and `ISSUER_ID`.
74+
75+
This snippet makes a NEAR payment for $80.50, with a $1.00 fee.
5976

6077
```
61-
# set ACCOUNT_ID, BUILDER_ID and ISSUER_ID
6278
near call $ACCOUNT_ID transfer_with_reference '{"to": "'$ISSUER_ID'", "payment_reference": "0x1230012300001234", "amount": "8050", "currency": "USD", "fee_amount": "100", "fee_address": "'$BUILDER_ID'"}' --accountId $ACCOUNT_ID --gas 300000000000000 --deposit 30
6379
```
80+
81+
This snippet makes a fungible token payment, given that `fau.reqnetwork.testnet` is a fungible token address and the `fungible_proxy` contract is deployed at `pay.reqnetwork.testnet`.
82+
83+
```
84+
near call fau.reqnetwork.testnet ft_transfer_call '{"receiver_id": "pay.reqnetwork.testnet", "amount": "2500000000000000000", "msg": "{\"fee_address\": \"'$BUILDER_ID'\", \"fee_amount\": \"1000000000000000000\", \"payment_reference\": \"abc7c8bb1234fd12\", \"to\": \"'$ISSUER_ID'\"}"}' --accountId $ACCOUNT_ID --depositYocto 1 --gas 300000000000000
85+
```
86+
87+
## FAU tokens (testnet)
88+
89+
The FAU token at `fau.reqnetwork.testnet` has 18 decimals and a total supply of 1'000'000.00 FAU.
90+
It is based on the example at https://github.com/near/near-sdk-rs/tree/master/examples/fungible-token, slightly updated and deployed using the commands:
91+
92+
```
93+
./build.sh
94+
near create-account fau.reqnetwork.testnet --masterAccount reqnetwork.testnet --initialBalance 8
95+
near deploy -f --wasmFile ./res/fungible_token.wasm --accountId fau.reqnetwork.testnet --initFunction new_default_meta --initArgs '{"owner_id": "reqnetwork.testnet", "total_supply": "1000000000000000000000000"}'
96+
```
97+
98+
Get some FAU:
99+
100+
```
101+
# Register the account
102+
near call fau.reqnetwork.testnet storage_deposit '{"account_id": "'$ACCOUNT_ID'"}' --accountId $ACCOUNT_ID --amount 0.005
103+
# Transfer 1000.00 FAU to the account
104+
near call fau.reqnetwork.testnet ft_transfer '{"receiver_id": "'$ACCOUNT_ID'", "amount": "1000000000000000000000"}' --accountId reqnetwork.testnet --depositYocto 1
105+
```
106+
107+
To use FAU on a new proxy, you need to register it first:
108+
109+
```
110+
near call fau.reqnetwork.testnet storage_deposit '{"account_id": "'$PROXY_ADDRESS'"}' --accountId $ACCOUNT_ID --amount 0.005
111+
```
112+
113+
You need to run the same command for every account before they receive FAU, or the smart contract will panick with the error message `The account some_account is not registered`.

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ RUSTFLAGS='-C link-arg=-s' cargo build --all --target wasm32-unknown-unknown --r
55
mkdir -p ./out
66
cp target/wasm32-unknown-unknown/release/conversion_proxy.wasm ./out/
77
cp target/wasm32-unknown-unknown/release/fungible_conversion_proxy.wasm ./out/
8+
cp target/wasm32-unknown-unknown/release/fungible_proxy.wasm ./out/

conversion_proxy/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/bash
2-
cargo test -- --nocapture
2+
cargo test -- --nocapture

deploy.sh

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,55 @@
66
NEAR_ENV="testnet"
77
oracle_account_id="fpo.opfilabs.testnet"
88
provider_account_id="opfilabs.testnet"
9+
contract_name="conversion_proxy";
910

10-
while getopts ":a:ph" opt; do
11-
case $opt in
12-
h)
11+
die() { echo "$*" >&2; exit 2; } # complain to STDERR and exit with error
12+
needs_arg() { if [ -z "$OPTARG" ]; then die "Missing arg for --$OPT option"; fi; }
13+
14+
while getopts "pha:-:" OPT; do
15+
# Source: https://stackoverflow.com/a/28466267
16+
if [ "$OPT" = "-" ]; then # long option: reformulate OPT and OPTARG
17+
OPT="${OPTARG%%=*}" # extract long option name
18+
OPTARG="${OPTARG#$OPT}" # extract long option argument (may be empty)
19+
OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=`
20+
fi
21+
case "$OPT" in
22+
h | help)
1323
echo "Builds and deploys the contract with state initialization (first deployement)."
1424
echo "Defaults to testnet."
1525
echo ""
1626
echo "Options:"
17-
echo " -p : for prod deployment"
18-
echo " -a [account_id] : to override \$ACCOUNT_ID"
27+
echo " -p | --prod | --mainnet : for prod deployment"
28+
echo " -a [account_id] : to override \$ACCOUNT_ID"
29+
echo " Choose the contract to deploy with:"
30+
echo " --conversion_proxy [default]"
31+
echo " --fungible_proxy"
32+
echo " --fungible_conversionproxy"
1933
exit 0
2034
;;
21-
p)
22-
NEAR_ENV="mainnet"
23-
oracle_account_id="fpo.opfilabs.near"
24-
provider_account_id="opfilabs.near"
25-
;;
26-
a)
27-
ACCOUNT_ID="$OPTARG"
28-
;;
29-
\?)
30-
echo "Invalid option -$OPTARG" >&2
31-
exit 1
32-
;;
33-
:)
34-
echo "Option -$OPTARG needs a valid argument"
35-
exit 1
36-
;;
35+
p | prod | mainnet) NEAR_ENV="mainnet" ;;
36+
a | account_id) needs_arg; ACCOUNT_ID="$OPTARG" ;;
37+
conversion_proxy | fungible_proxy | fungible_conversion_proxy) contract_name="$OPT" ;;
38+
??* ) die "Unknown option --$OPT" ;; # bad long option
39+
? ) exit 2 ;; # bad short option (error reported via getopts)
3740
esac
38-
3941
done
4042

41-
printf "NEAR_ENV=%s\n" "$NEAR_ENV"
42-
printf "ACCOUNT_ID=%s\n" "$ACCOUNT_ID"
43+
if [ "$ACCOUNT_ID" = "" ]; then
44+
echo "Missing account ID";
45+
exit 1;
46+
fi
47+
48+
printf "Deploying %s on NEAR_ENV=%s with ACCOUNT_ID=%s\n\n" "$contract_name" "$NEAR_ENV" "$ACCOUNT_ID"
4349

4450
./build.sh
4551

46-
near deploy -f --wasmFile ./out/conversion_proxy.wasm \
47-
--accountId $ACCOUNT_ID \
48-
--initFunction new \
49-
--initArgs '{"oracle_account_id": "'$oracle_account_id'", "provider_account_id": "'$provider_account_id'"}'
52+
if [ "$contract_name" = "fungible_proxy" ]; then
53+
near deploy -f --wasmFile ./out/$contract_name.wasm \
54+
--accountId $ACCOUNT_ID
55+
else
56+
near deploy -f --wasmFile ./out/$contract_name.wasm \
57+
--accountId $ACCOUNT_ID \
58+
--initFunction new \
59+
--initArgs '{"oracle_account_id": "'$oracle_account_id'", "provider_account_id": "'$provider_account_id'"}'
60+
fi

fungible_conversion_proxy/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/bash
2-
cargo test -- --nocapture
2+
cargo test -- --nocapture

fungible_proxy/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "fungible_proxy"
3+
version = "0.0.1"
4+
authors = ["Request Network Foundation"]
5+
edition = "2018"
6+
7+
[lib]
8+
crate-type = ["cdylib", "rlib"]
9+
10+
[dependencies]
11+
near-sdk = "3.1.0"
12+
serde = "1.0.118"
13+
hex = "0.4"
14+
15+
[dev-dependencies]
16+
near-sdk-sim = "3.2.0"

0 commit comments

Comments
 (0)