Skip to content

chore: fix CI and refactor patch deployment script #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
name: build and test
name: main

on:
push:
branches:
- "main"
pull_request:
branches: [ "main" ]
branches:
- "main"
workflow_dispatch:

env:
CARGO_TERM_COLOR: always

jobs:
build:

build-and-test:
runs-on: ubuntu-latest

steps:
- name: Rust downgrade
run: rustup default 1.67.0
- uses: actions/checkout@v3
- name: Install wasm32-unknown-unknown target
run: rustup target add wasm32-unknown-unknown
- 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
- name: Build and run mocks unit tests
- name: Build contracts
run: ./build.sh
- name: Build mocks
working-directory: ./mocks
run: cargo test
- name: Run integration tests
run: ./test.sh
run: ./build.sh
- name: Unit and Integration Tests
run: cargo test --all

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Cargo format check
run: cargo fmt -- --check
- name: Cargo clippy
run: cargo clippy -- -D warnings
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
conversion_proxy/.cargo/
*/target/
target/
out/
target/
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ cargo
./deploy.sh -a ACCOUNT_ID

# 2. For subsequent contract updates
./patch-deploy.sh -a ACCOUNT_ID
./deploy.sh -a ACCOUNT_ID --patch

# For both commands, use `-p` for production deployment.

# For more details and options:
./deploy.sh --help
```

## Calling contract
Expand Down
6 changes: 1 addition & 5 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#!/bin/bash
set -e

RUSTFLAGS='-C link-arg=-s' cargo build --all --target wasm32-unknown-unknown --release
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/
RUSTFLAGS='-C link-arg=-s' cargo build --target wasm32-unknown-unknown --all --exclude mocks --release
3 changes: 2 additions & 1 deletion conversion_proxy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[package]
name = "conversion_proxy"
version = "0.0.2"
authors = ["Request Labs"]
authors = ["Request Finance", "Request Network"]
edition = "2018"

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

[dependencies]
near-sdk = "3.1.0"
Expand Down
6 changes: 4 additions & 2 deletions conversion_proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use near_sdk::json_types::{ValidAccountId, U128, U64};
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::serde_json::json;
use near_sdk::{
env, near_bindgen, serde_json, AccountId, Balance, Gas, Promise, PromiseResult, Timestamp, log,
env, log, near_bindgen, serde_json, AccountId, Balance, Gas, Promise, PromiseResult, Timestamp,
};

near_sdk::setup_alloc!();
Expand Down Expand Up @@ -218,7 +218,9 @@ impl ConversionProxy {
} else {
log!(
"Failed to transfer to account {}. Returning attached deposit of {} to {}",
payment_address, deposit.0, predecessor_account_id
payment_address,
deposit.0,
predecessor_account_id
);
Promise::new(predecessor_account_id).transfer(deposit.into());
false
Expand Down
32 changes: 25 additions & 7 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ NEAR_ENV="testnet"
oracle_account_id="fpo.opfilabs.testnet"
provider_account_id="opfilabs.testnet"
contract_name="conversion_proxy";
patch=false;

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; }
Expand All @@ -20,21 +21,28 @@ while getopts "pha:-:" OPT; do
fi
case "$OPT" in
h | help)
echo "Builds and deploys the contract with state initialization (first deployement)."
echo "Builds and deploys contracts, with or without state initialization."
echo "Defaults to testnet."
echo ""
echo "Options:"
echo " -h | --help : shows this help"
echo " -p | --prod | --mainnet : for prod deployment"
echo " -a [account_id] : to override \$ACCOUNT_ID"
echo " --patch : to patch an existing contract (skip the init function, if any)"
echo ""
echo " Choose the contract to deploy with:"
echo " --conversion_proxy [default]"
echo " --fungible_proxy"
echo " --fungible_conversionproxy"
exit 0
;;
# Options
p | prod | mainnet) NEAR_ENV="mainnet" ;;
a | account_id) needs_arg; ACCOUNT_ID="$OPTARG" ;;
patch) patch=true ;;
# Contract to deploy
conversion_proxy | fungible_proxy | fungible_conversion_proxy) contract_name="$OPT" ;;
# Bad options
??* ) die "Unknown option --$OPT" ;; # bad long option
? ) exit 2 ;; # bad short option (error reported via getopts)
esac
Expand All @@ -45,16 +53,26 @@ if [ "$ACCOUNT_ID" = "" ]; then
exit 1;
fi

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


./build.sh

if [ "$contract_name" = "fungible_proxy" ]; then
near deploy -f --wasmFile ./out/$contract_name.wasm \
--accountId $ACCOUNT_ID
set -x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this for debugging? Do you want to keep it in there?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I plan to keep it here to show the commands that are run, so it's easier to understand what's going on, especially if something went wrong, but not only.

near deploy -f --wasmFile ./target/wasm32-unknown-unknown/release/$contract_name.wasm \
--accountId $ACCOUNT_ID
else
near deploy -f --wasmFile ./out/$contract_name.wasm \
--accountId $ACCOUNT_ID \
initParams="";
if ! $patch ; then
initParams="
--initFunction new \
--initArgs '{"oracle_account_id": "'$oracle_account_id'", "provider_account_id": "'$provider_account_id'"}'
--initArgs '{"oracle_account_id": "'$oracle_account_id'", "provider_account_id": "'$provider_account_id'"}'";
fi
set -x
near deploy -f --wasmFile ./target/wasm32-unknown-unknown/release/$contract_name.wasm \
--accountId $ACCOUNT_ID \
$initParams
fi

set +x
1 change: 1 addition & 0 deletions fungible_conversion_proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2018"

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

[dependencies]
near-sdk = "3.1.0"
Expand Down
2 changes: 1 addition & 1 deletion fungible_conversion_proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use near_sdk::json_types::{Base64VecU8, ValidAccountId, U128, U64};
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::serde_json::json;
use near_sdk::{
env, near_bindgen, serde_json, AccountId, Balance, Gas, Promise, PromiseResult, Timestamp, log,
env, log, near_bindgen, serde_json, AccountId, Balance, Gas, Promise, PromiseResult, Timestamp,
};
near_sdk::setup_alloc!();

Expand Down
1 change: 1 addition & 0 deletions fungible_proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2018"

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

[dependencies]
near-sdk = "3.1.0"
Expand Down
89 changes: 46 additions & 43 deletions fungible_proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use near_sdk::json_types::{ValidAccountId, U128};
use near_sdk::log;
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::serde_json::json;
use near_sdk::{
env, near_bindgen, serde_json, AccountId, Balance, Gas, Promise
};
use near_sdk::{env, near_bindgen, serde_json, AccountId, Balance, Gas, Promise};
near_sdk::setup_alloc!();

const NO_DEPOSIT: Balance = 0;
Expand Down Expand Up @@ -119,7 +117,10 @@ impl FungibleProxy {
let reference_vec: Vec<u8> = hex::decode(args.payment_reference.replace("0x", ""))
.expect("Payment reference value error");
assert_eq!(reference_vec.len(), 8, "Incorrect payment reference length");
assert!(args.fee_amount.0 <= amount.0, "amount smaller than fee_amount");
assert!(
args.fee_amount.0 <= amount.0,
"amount smaller than fee_amount"
);
let main_amount = amount.0 - args.fee_amount.0;
let main_transfer_args =
json!({ "receiver_id": args.to.to_string(), "amount":main_amount.to_string(), "memo": None::<String> })
Expand All @@ -132,56 +133,51 @@ impl FungibleProxy {
.into_bytes();

// Some tokens revert when calling `ft_transfer` with 0
let payment_promise =
if main_amount > 0 && args.fee_amount.0 > 0 {
// Main case: amount and fee
Promise::new(token_address.to_string())
.function_call(
"ft_transfer".into(),
main_transfer_args,
YOCTO_DEPOSIT,
BASIC_GAS * 2,
)
.function_call(
"ft_transfer".into(),
fee_transfer_args,
YOCTO_DEPOSIT,
BASIC_GAS * 2,
)
} else if main_amount > 0 {
// No fee
Promise::new(token_address.to_string())
let payment_promise = if main_amount > 0 && args.fee_amount.0 > 0 {
// Main case: amount and fee
Promise::new(token_address.to_string())
.function_call(
"ft_transfer".into(),
main_transfer_args,
YOCTO_DEPOSIT,
BASIC_GAS * 2,
)
} else if args.fee_amount.0 > 0 {
// Only fee payment
Promise::new(token_address.to_string())
.function_call(
"ft_transfer".into(),
fee_transfer_args,
YOCTO_DEPOSIT,
BASIC_GAS * 2,
)
} else {
// No payment
Promise::new(token_address.to_string())
};

payment_promise
.then(ext_self::on_transfer_with_reference(
args,
token_address,
payer,
main_amount.into(),
&env::current_account_id(),
NO_DEPOSIT,
BASIC_GAS,
} else if main_amount > 0 {
// No fee
Promise::new(token_address.to_string()).function_call(
"ft_transfer".into(),
main_transfer_args,
YOCTO_DEPOSIT,
BASIC_GAS * 2,
)
)
} else if args.fee_amount.0 > 0 {
// Only fee payment
Promise::new(token_address.to_string()).function_call(
"ft_transfer".into(),
fee_transfer_args,
YOCTO_DEPOSIT,
BASIC_GAS * 2,
)
} else {
// No payment
Promise::new(token_address.to_string())
};

payment_promise.then(ext_self::on_transfer_with_reference(
args,
token_address,
payer,
main_amount.into(),
&env::current_account_id(),
NO_DEPOSIT,
BASIC_GAS,
))
}

#[private]
Expand All @@ -208,9 +204,16 @@ impl FungibleProxy {
);
0.to_string()
} else {
// return full amount for `ft_resolve_transfer` on the token contract
// return full amount for `ft_resolve_transfer` on the token contract
let change = (amount.0 + args.fee_amount.0).to_string();
log!("Failed to transfer to account {}. Returning attached amount of {} of token {} to {}", args.to, change, token_address, payer);
log!(
"Transfer failed to {} or {}. Returning attached amount of {} of token {} to {}",
args.to,
args.fee_address,
change,
token_address,
payer
);
change
}
}
Expand Down
3 changes: 2 additions & 1 deletion mocks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[package]
name = "mocks"
version = "0.0.2"
authors = ["Request Labs"]
authors = ["Request Finance", "Request Network"]
edition = "2018"

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

[dependencies]
near-sdk = "3.1.0"
Expand Down
2 changes: 0 additions & 2 deletions mocks/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
set -e

RUSTFLAGS='-C link-arg=-s' cargo build --target wasm32-unknown-unknown
mkdir -p ../out
cp ../target/wasm32-unknown-unknown/debug/mocks.wasm ../out/
Loading