Skip to content

Commit cc11748

Browse files
chore: bandaid CI and refactor patch deployment script (#16)
Co-authored-by: MantisClone <[email protected]>
1 parent a807820 commit cc11748

File tree

20 files changed

+195
-195
lines changed

20 files changed

+195
-195
lines changed

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

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
1-
name: build and test
1+
name: main
22

33
on:
44
push:
5+
branches:
6+
- "main"
57
pull_request:
6-
branches: [ "main" ]
8+
branches:
9+
- "main"
710
workflow_dispatch:
811

912
env:
1013
CARGO_TERM_COLOR: always
1114

1215
jobs:
13-
build:
14-
16+
build-and-test:
1517
runs-on: ubuntu-latest
16-
1718
steps:
19+
- name: Rust downgrade
20+
run: rustup default 1.67.0
1821
- uses: actions/checkout@v3
1922
- name: Install wasm32-unknown-unknown target
2023
run: rustup target add wasm32-unknown-unknown
21-
- name: Build and run conversion proxy unit tests
22-
working-directory: ./conversion_proxy
23-
run: cargo test
24-
- name: Build and run fungible proxy unit tests
25-
working-directory: ./fungible_proxy
26-
run: cargo test
27-
- name: Build and run fungible conversion proxy unit tests
28-
working-directory: ./fungible_conversion_proxy
29-
run: cargo test
30-
- name: Build and run mocks unit tests
24+
- name: Build contracts
25+
run: ./build.sh
26+
- name: Build mocks
3127
working-directory: ./mocks
32-
run: cargo test
33-
- name: Run integration tests
34-
run: ./test.sh
28+
run: ./build.sh
29+
- name: Unit and Integration Tests
30+
run: cargo test --all
31+
32+
lint:
33+
runs-on: ubuntu-latest
34+
steps:
35+
- uses: actions/checkout@v3
36+
- name: Cargo format check
37+
run: cargo fmt -- --check
38+
- name: Cargo clippy
39+
run: cargo clippy -- -D warnings

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
conversion_proxy/.cargo/
22
*/target/
3-
target/
4-
out/
3+
target/

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,12 @@ cargo
6363
./deploy.sh -a ACCOUNT_ID
6464
6565
# 2. For subsequent contract updates
66-
./patch-deploy.sh -a ACCOUNT_ID
66+
./deploy.sh -a ACCOUNT_ID --patch
6767
6868
# For both commands, use `-p` for production deployment.
69+
70+
# For more details and options:
71+
./deploy.sh --help
6972
```
7073

7174
## Calling contract

build.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
#!/bin/bash
22
set -e
33

4-
RUSTFLAGS='-C link-arg=-s' cargo build --all --target wasm32-unknown-unknown --release
5-
mkdir -p ./out
6-
cp target/wasm32-unknown-unknown/release/conversion_proxy.wasm ./out/
7-
cp target/wasm32-unknown-unknown/release/fungible_conversion_proxy.wasm ./out/
8-
cp target/wasm32-unknown-unknown/release/fungible_proxy.wasm ./out/
4+
RUSTFLAGS='-C link-arg=-s' cargo build --target wasm32-unknown-unknown --all --exclude mocks --release

conversion_proxy/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
[package]
22
name = "conversion_proxy"
33
version = "0.0.2"
4-
authors = ["Request Labs"]
4+
authors = ["Request Finance", "Request Network"]
55
edition = "2018"
66

77
[lib]
88
crate-type = ["cdylib", "rlib"]
9+
doctest = false
910

1011
[dependencies]
1112
near-sdk = "3.1.0"

conversion_proxy/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use near_sdk::json_types::{ValidAccountId, U128, U64};
33
use near_sdk::serde::{Deserialize, Serialize};
44
use near_sdk::serde_json::json;
55
use near_sdk::{
6-
env, near_bindgen, serde_json, AccountId, Balance, Gas, Promise, PromiseResult, Timestamp, log,
6+
env, log, near_bindgen, serde_json, AccountId, Balance, Gas, Promise, PromiseResult, Timestamp,
77
};
88

99
near_sdk::setup_alloc!();
@@ -218,7 +218,9 @@ impl ConversionProxy {
218218
} else {
219219
log!(
220220
"Failed to transfer to account {}. Returning attached deposit of {} to {}",
221-
payment_address, deposit.0, predecessor_account_id
221+
payment_address,
222+
deposit.0,
223+
predecessor_account_id
222224
);
223225
Promise::new(predecessor_account_id).transfer(deposit.into());
224226
false

deploy.sh

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ NEAR_ENV="testnet"
77
oracle_account_id="fpo.opfilabs.testnet"
88
provider_account_id="opfilabs.testnet"
99
contract_name="conversion_proxy";
10+
patch=false;
1011

1112
die() { echo "$*" >&2; exit 2; } # complain to STDERR and exit with error
1213
needs_arg() { if [ -z "$OPTARG" ]; then die "Missing arg for --$OPT option"; fi; }
@@ -20,21 +21,28 @@ while getopts "pha:-:" OPT; do
2021
fi
2122
case "$OPT" in
2223
h | help)
23-
echo "Builds and deploys the contract with state initialization (first deployement)."
24+
echo "Builds and deploys contracts, with or without state initialization."
2425
echo "Defaults to testnet."
2526
echo ""
2627
echo "Options:"
28+
echo " -h | --help : shows this help"
2729
echo " -p | --prod | --mainnet : for prod deployment"
2830
echo " -a [account_id] : to override \$ACCOUNT_ID"
31+
echo " --patch : to patch an existing contract (skip the init function, if any)"
32+
echo ""
2933
echo " Choose the contract to deploy with:"
3034
echo " --conversion_proxy [default]"
3135
echo " --fungible_proxy"
3236
echo " --fungible_conversionproxy"
3337
exit 0
3438
;;
39+
# Options
3540
p | prod | mainnet) NEAR_ENV="mainnet" ;;
3641
a | account_id) needs_arg; ACCOUNT_ID="$OPTARG" ;;
42+
patch) patch=true ;;
43+
# Contract to deploy
3744
conversion_proxy | fungible_proxy | fungible_conversion_proxy) contract_name="$OPT" ;;
45+
# Bad options
3846
??* ) die "Unknown option --$OPT" ;; # bad long option
3947
? ) exit 2 ;; # bad short option (error reported via getopts)
4048
esac
@@ -45,16 +53,26 @@ if [ "$ACCOUNT_ID" = "" ]; then
4553
exit 1;
4654
fi
4755

48-
printf "Deploying %s on NEAR_ENV=%s with ACCOUNT_ID=%s\n\n" "$contract_name" "$NEAR_ENV" "$ACCOUNT_ID"
56+
printf "Deploying %s on NEAR_ENV=%s with ACCOUNT_ID=%s (patch=%s)\n\n" "$contract_name" "$NEAR_ENV" "$ACCOUNT_ID" "$patch"
57+
4958

5059
./build.sh
5160

5261
if [ "$contract_name" = "fungible_proxy" ]; then
53-
near deploy -f --wasmFile ./out/$contract_name.wasm \
54-
--accountId $ACCOUNT_ID
62+
set -x
63+
near deploy -f --wasmFile ./target/wasm32-unknown-unknown/release/$contract_name.wasm \
64+
--accountId $ACCOUNT_ID
5565
else
56-
near deploy -f --wasmFile ./out/$contract_name.wasm \
57-
--accountId $ACCOUNT_ID \
66+
initParams="";
67+
if ! $patch ; then
68+
initParams="
5869
--initFunction new \
59-
--initArgs '{"oracle_account_id": "'$oracle_account_id'", "provider_account_id": "'$provider_account_id'"}'
70+
--initArgs '{"oracle_account_id": "'$oracle_account_id'", "provider_account_id": "'$provider_account_id'"}'";
71+
fi
72+
set -x
73+
near deploy -f --wasmFile ./target/wasm32-unknown-unknown/release/$contract_name.wasm \
74+
--accountId $ACCOUNT_ID \
75+
$initParams
6076
fi
77+
78+
set +x

fungible_conversion_proxy/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2018"
66

77
[lib]
88
crate-type = ["cdylib", "rlib"]
9+
doctest = false
910

1011
[dependencies]
1112
near-sdk = "3.1.0"

fungible_conversion_proxy/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use near_sdk::json_types::{Base64VecU8, ValidAccountId, U128, U64};
33
use near_sdk::serde::{Deserialize, Serialize};
44
use near_sdk::serde_json::json;
55
use near_sdk::{
6-
env, near_bindgen, serde_json, AccountId, Balance, Gas, Promise, PromiseResult, Timestamp, log,
6+
env, log, near_bindgen, serde_json, AccountId, Balance, Gas, Promise, PromiseResult, Timestamp,
77
};
88
near_sdk::setup_alloc!();
99

fungible_proxy/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2018"
66

77
[lib]
88
crate-type = ["cdylib", "rlib"]
9+
doctest = false
910

1011
[dependencies]
1112
near-sdk = "3.1.0"

fungible_proxy/src/lib.rs

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use near_sdk::json_types::{ValidAccountId, U128};
33
use near_sdk::log;
44
use near_sdk::serde::{Deserialize, Serialize};
55
use near_sdk::serde_json::json;
6-
use near_sdk::{
7-
env, near_bindgen, serde_json, AccountId, Balance, Gas, Promise
8-
};
6+
use near_sdk::{env, near_bindgen, serde_json, AccountId, Balance, Gas, Promise};
97
near_sdk::setup_alloc!();
108

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

134135
// Some tokens revert when calling `ft_transfer` with 0
135-
let payment_promise =
136-
if main_amount > 0 && args.fee_amount.0 > 0 {
137-
// Main case: amount and fee
138-
Promise::new(token_address.to_string())
139-
.function_call(
140-
"ft_transfer".into(),
141-
main_transfer_args,
142-
YOCTO_DEPOSIT,
143-
BASIC_GAS * 2,
144-
)
145-
.function_call(
146-
"ft_transfer".into(),
147-
fee_transfer_args,
148-
YOCTO_DEPOSIT,
149-
BASIC_GAS * 2,
150-
)
151-
} else if main_amount > 0 {
152-
// No fee
153-
Promise::new(token_address.to_string())
136+
let payment_promise = if main_amount > 0 && args.fee_amount.0 > 0 {
137+
// Main case: amount and fee
138+
Promise::new(token_address.to_string())
154139
.function_call(
155140
"ft_transfer".into(),
156141
main_transfer_args,
157142
YOCTO_DEPOSIT,
158143
BASIC_GAS * 2,
159144
)
160-
} else if args.fee_amount.0 > 0 {
161-
// Only fee payment
162-
Promise::new(token_address.to_string())
163145
.function_call(
164146
"ft_transfer".into(),
165147
fee_transfer_args,
166148
YOCTO_DEPOSIT,
167149
BASIC_GAS * 2,
168150
)
169-
} else {
170-
// No payment
171-
Promise::new(token_address.to_string())
172-
};
173-
174-
payment_promise
175-
.then(ext_self::on_transfer_with_reference(
176-
args,
177-
token_address,
178-
payer,
179-
main_amount.into(),
180-
&env::current_account_id(),
181-
NO_DEPOSIT,
182-
BASIC_GAS,
151+
} else if main_amount > 0 {
152+
// No fee
153+
Promise::new(token_address.to_string()).function_call(
154+
"ft_transfer".into(),
155+
main_transfer_args,
156+
YOCTO_DEPOSIT,
157+
BASIC_GAS * 2,
183158
)
184-
)
159+
} else if args.fee_amount.0 > 0 {
160+
// Only fee payment
161+
Promise::new(token_address.to_string()).function_call(
162+
"ft_transfer".into(),
163+
fee_transfer_args,
164+
YOCTO_DEPOSIT,
165+
BASIC_GAS * 2,
166+
)
167+
} else {
168+
// No payment
169+
Promise::new(token_address.to_string())
170+
};
171+
172+
payment_promise.then(ext_self::on_transfer_with_reference(
173+
args,
174+
token_address,
175+
payer,
176+
main_amount.into(),
177+
&env::current_account_id(),
178+
NO_DEPOSIT,
179+
BASIC_GAS,
180+
))
185181
}
186182

187183
#[private]
@@ -208,9 +204,16 @@ impl FungibleProxy {
208204
);
209205
0.to_string()
210206
} else {
211-
// return full amount for `ft_resolve_transfer` on the token contract
207+
// return full amount for `ft_resolve_transfer` on the token contract
212208
let change = (amount.0 + args.fee_amount.0).to_string();
213-
log!("Failed to transfer to account {}. Returning attached amount of {} of token {} to {}", args.to, change, token_address, payer);
209+
log!(
210+
"Transfer failed to {} or {}. Returning attached amount of {} of token {} to {}",
211+
args.to,
212+
args.fee_address,
213+
change,
214+
token_address,
215+
payer
216+
);
214217
change
215218
}
216219
}

mocks/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
[package]
22
name = "mocks"
33
version = "0.0.2"
4-
authors = ["Request Labs"]
4+
authors = ["Request Finance", "Request Network"]
55
edition = "2018"
66

77
[lib]
88
crate-type = ["cdylib", "rlib"]
9+
doctest = false
910

1011
[dependencies]
1112
near-sdk = "3.1.0"

mocks/build.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
set -e
33

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

0 commit comments

Comments
 (0)