Skip to content

Commit

Permalink
feat: fungible proxy (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
yomarion authored Apr 12, 2023
1 parent 0fca82c commit 8b499be
Show file tree
Hide file tree
Showing 18 changed files with 1,497 additions and 697 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
- name: Build and run conversion proxy unit tests
working-directory: ./conversion_proxy
run: cargo test
- name: Build and run fungible proxy unit tests
working-directory: ./fungible_proxy
run: cargo test
- name: Build and run fungible conversion proxy unit tests
working-directory: ./fungible_conversion_proxy
run: cargo test
Expand Down
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ hex = "0.4"
near-sdk-sim = "3.2.0"
conversion_proxy = { path = "./conversion_proxy" }
fungible_conversion_proxy = { path = "./fungible_conversion_proxy" }
fungible_proxy = { path = "./fungible_proxy" }
mocks = { path = "./mocks" }

[profile.release]
Expand All @@ -27,4 +28,4 @@ panic = "abort"
overflow-checks = true

[workspace]
members = ["conversion_proxy", "fungible_conversion_proxy", "mocks"]
members = ["conversion_proxy", "fungible_conversion_proxy", "fungible_proxy", "mocks"]
54 changes: 52 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,34 @@ Smart contracts on NEAR used by the

## Unit tests

Run all contracts unit tests like this:

```
cd near-contracts/conversion_proxy
cargo test
cd near-contracts/fungible_conversion_proxy
cargo test
cd near-contracts/fungible_proxy
cargo test
cd near-contracts/mocks
cargo test
```

## Integration tests

```
# To test everything
./test.sh
# To test contracts one by one:
cargo test conversion_proxy
cargo test fungible_conversionproxy
cargo test fungible_proxy
# To run integration tests one by one (examples with main transfers):
cargo test conversion_proxy::test_transfer -- --exact
cargo test fungible_conversionproxy::test_transfer -- --exact
cargo test fungible_proxy::test_transfer -- --exact
```

## Deploying contract
Expand All @@ -55,9 +70,44 @@ cargo

## Calling contract

The snippet below makes a NEAR payment for $80.50, with a $1.00 fee.
Commands below assumes a few variables are set: `ACCOUNT_ID`, `BUILDER_ID` and `ISSUER_ID`.

This snippet makes a NEAR payment for $80.50, with a $1.00 fee.

```
# set ACCOUNT_ID, BUILDER_ID and ISSUER_ID
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
```

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`.

```
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
```

## FAU tokens (testnet)

The FAU token at `fau.reqnetwork.testnet` has 18 decimals and a total supply of 1'000'000.00 FAU.
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:

```
./build.sh
near create-account fau.reqnetwork.testnet --masterAccount reqnetwork.testnet --initialBalance 8
near deploy -f --wasmFile ./res/fungible_token.wasm --accountId fau.reqnetwork.testnet --initFunction new_default_meta --initArgs '{"owner_id": "reqnetwork.testnet", "total_supply": "1000000000000000000000000"}'
```

Get some FAU:

```
# Register the account
near call fau.reqnetwork.testnet storage_deposit '{"account_id": "'$ACCOUNT_ID'"}' --accountId $ACCOUNT_ID --amount 0.005
# Transfer 1000.00 FAU to the account
near call fau.reqnetwork.testnet ft_transfer '{"receiver_id": "'$ACCOUNT_ID'", "amount": "1000000000000000000000"}' --accountId reqnetwork.testnet --depositYocto 1
```

To use FAU on a new proxy, you need to register it first:

```
near call fau.reqnetwork.testnet storage_deposit '{"account_id": "'$PROXY_ADDRESS'"}' --accountId $ACCOUNT_ID --amount 0.005
```

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`.
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ RUSTFLAGS='-C link-arg=-s' cargo build --all --target wasm32-unknown-unknown --r
mkdir -p ./out
cp target/wasm32-unknown-unknown/release/conversion_proxy.wasm ./out/
cp target/wasm32-unknown-unknown/release/fungible_conversion_proxy.wasm ./out/
cp target/wasm32-unknown-unknown/release/fungible_proxy.wasm ./out/
2 changes: 1 addition & 1 deletion conversion_proxy/test.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
cargo test -- --nocapture
cargo test -- --nocapture
67 changes: 39 additions & 28 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,55 @@
NEAR_ENV="testnet"
oracle_account_id="fpo.opfilabs.testnet"
provider_account_id="opfilabs.testnet"
contract_name="conversion_proxy";

while getopts ":a:ph" opt; do
case $opt in
h)
die() { echo "$*" >&2; exit 2; } # complain to STDERR and exit with error
needs_arg() { if [ -z "$OPTARG" ]; then die "Missing arg for --$OPT option"; fi; }

while getopts "pha:-:" OPT; do
# Source: https://stackoverflow.com/a/28466267
if [ "$OPT" = "-" ]; then # long option: reformulate OPT and OPTARG
OPT="${OPTARG%%=*}" # extract long option name
OPTARG="${OPTARG#$OPT}" # extract long option argument (may be empty)
OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=`
fi
case "$OPT" in
h | help)
echo "Builds and deploys the contract with state initialization (first deployement)."
echo "Defaults to testnet."
echo ""
echo "Options:"
echo " -p : for prod deployment"
echo " -a [account_id] : to override \$ACCOUNT_ID"
echo " -p | --prod | --mainnet : for prod deployment"
echo " -a [account_id] : to override \$ACCOUNT_ID"
echo " Choose the contract to deploy with:"
echo " --conversion_proxy [default]"
echo " --fungible_proxy"
echo " --fungible_conversionproxy"
exit 0
;;
p)
NEAR_ENV="mainnet"
oracle_account_id="fpo.opfilabs.near"
provider_account_id="opfilabs.near"
;;
a)
ACCOUNT_ID="$OPTARG"
;;
\?)
echo "Invalid option -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG needs a valid argument"
exit 1
;;
p | prod | mainnet) NEAR_ENV="mainnet" ;;
a | account_id) needs_arg; ACCOUNT_ID="$OPTARG" ;;
conversion_proxy | fungible_proxy | fungible_conversion_proxy) contract_name="$OPT" ;;
??* ) die "Unknown option --$OPT" ;; # bad long option
? ) exit 2 ;; # bad short option (error reported via getopts)
esac

done

printf "NEAR_ENV=%s\n" "$NEAR_ENV"
printf "ACCOUNT_ID=%s\n" "$ACCOUNT_ID"
if [ "$ACCOUNT_ID" = "" ]; then
echo "Missing account ID";
exit 1;
fi

printf "Deploying %s on NEAR_ENV=%s with ACCOUNT_ID=%s\n\n" "$contract_name" "$NEAR_ENV" "$ACCOUNT_ID"

./build.sh

near deploy -f --wasmFile ./out/conversion_proxy.wasm \
--accountId $ACCOUNT_ID \
--initFunction new \
--initArgs '{"oracle_account_id": "'$oracle_account_id'", "provider_account_id": "'$provider_account_id'"}'
if [ "$contract_name" = "fungible_proxy" ]; then
near deploy -f --wasmFile ./out/$contract_name.wasm \
--accountId $ACCOUNT_ID
else
near deploy -f --wasmFile ./out/$contract_name.wasm \
--accountId $ACCOUNT_ID \
--initFunction new \
--initArgs '{"oracle_account_id": "'$oracle_account_id'", "provider_account_id": "'$provider_account_id'"}'
fi
2 changes: 1 addition & 1 deletion fungible_conversion_proxy/test.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
cargo test -- --nocapture
cargo test -- --nocapture
16 changes: 16 additions & 0 deletions fungible_proxy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "fungible_proxy"
version = "0.0.1"
authors = ["Request Network Foundation"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
near-sdk = "3.1.0"
serde = "1.0.118"
hex = "0.4"

[dev-dependencies]
near-sdk-sim = "3.2.0"
Loading

0 comments on commit 8b499be

Please sign in to comment.