Skip to content

Commit

Permalink
near/testing: improve near tests for decimal point shifts
Browse files Browse the repository at this point in the history
              force algorand and indexer to update to what we are about to run in production
              add checks for fees being passed for P3 messages
  • Loading branch information
jumpsiegel committed Sep 8, 2022
1 parent bc70663 commit 6235c3b
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 46 deletions.
2 changes: 1 addition & 1 deletion algorand/sandbox-algorand/images/algod/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ COPY . /tmp
RUN /tmp/images/algod/install.sh \
-d "${BIN_DIR}" \
-u "https://github.com/algorand/go-algorand" \
-b "v3.6.2-stable" \
-b "v3.9.2-stable" \
-s ""

# Configure network
Expand Down
1 change: 1 addition & 0 deletions algorand/sandbox-algorand/images/indexer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ COPY images/indexer/start.sh /tmp/start.sh
COPY images/indexer/install.sh /tmp/install.sh

# Install indexer binaries.

RUN /tmp/install.sh

CMD ["/tmp/start.sh"]
1 change: 1 addition & 0 deletions algorand/sandbox-algorand/images/indexer/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# URL - Git repository URL.
# BRANCH - Git branch to clone.
# SHA - (optional) Specific commit hash to checkout.

set -e

# Sometimes indexer is disabled, detect the missing build config.
Expand Down
8 changes: 6 additions & 2 deletions near/contracts/ft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,16 @@ impl FTContract {
if payload.is_empty() {
p = [p, vec![0; 24], (fee as u64).to_be_bytes().to_vec()].concat();
if p.len() != 133 {
env::panic_str(&format!("paylod1 formatting errro len = {}", p.len()));
env::panic_str(&format!("payload1 formatting error len = {}", p.len()));
}
} else {
if fee != 0 {
env::panic_str("Payload3 does not support fees");
}

p = [p, hex::decode(&payload).unwrap()].concat();
if p.len() != (133 + (payload.len() / 2)) {
env::panic_str(&format!("paylod3 formatting errro len = {}", p.len()));
env::panic_str(&format!("payload3 formatting error len = {}", p.len()));
}
}

Expand Down
18 changes: 16 additions & 2 deletions near/contracts/token-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ impl TokenBridge {
env::panic_str("nfee >= namount");
}

env::log_str(&format!(
"token-bridge/{}#{}: vaa_transfer {} {} {} {} {}",
file!(),
line!(),
amount.1, fee.1, namount, nfee, near_mult));

env::log_str(&format!(
"token-bridge/{}#{}: vaa_transfer calling ft_transfer against {} for {} from {} to {}",
file!(),
Expand Down Expand Up @@ -975,6 +981,10 @@ impl TokenBridge {
env::panic_str("EmptyTransfer");
}

if !payload.is_empty() && nfee != 0 {
env::panic_str("Payload3 does not support fees");
}

if nfee >= namount {
env::panic_str("TransferFeeExceedsDeposit");
}
Expand Down Expand Up @@ -1498,6 +1508,10 @@ impl TokenBridge {
env::panic_str("EmptyTransfer");
}

if !tp.payload.is_empty() && nfee != 0 {
env::panic_str("Payload3 does not support fees");
}

if namount > (u64::MAX as u128) || nfee > (u64::MAX as u128) {
env::panic_str("transfer exceeds max bridged token amount");
}
Expand Down Expand Up @@ -1529,12 +1543,12 @@ impl TokenBridge {
if tp.payload.is_empty() {
p = [p, vec![0; 24], (nfee as u64).to_be_bytes().to_vec()].concat();
if p.len() != 133 {
env::panic_str(&format!("paylod1 formatting errro len = {}", p.len()));
env::panic_str(&format!("payload1 formatting error len = {}", p.len()));
}
} else {
p = [p, hex::decode(&tp.payload).unwrap()].concat();
if p.len() != (133 + (tp.payload.len() / 2)) {
env::panic_str(&format!("paylod3 formatting errro len = {}", p.len()));
env::panic_str(&format!("payload3 formatting error len = {}", p.len()));
}
}

Expand Down
116 changes: 75 additions & 41 deletions near/test/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ import {

const wh = require("@certusone/wormhole-sdk");

import { parseSequenceFromLogAlgorand, parseSequenceFromLogNear } from "@certusone/wormhole-sdk/lib/cjs/bridge";
import {
parseSequenceFromLogAlgorand,
parseSequenceFromLogNear,
} from "@certusone/wormhole-sdk/lib/cjs/bridge";

import {
getMessageFee,
Expand Down Expand Up @@ -520,25 +523,21 @@ async function testNearSDK() {
wrappedTransfer = signedVAA;
}

console.log(await userAccount.viewFunction(
randoToken,
"ft_balance_of",
{
console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: userAccount.accountId,
}
));
})
);

console.log(await userAccount.viewFunction(
randoToken,
"ft_balance_of",
{
console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: token_bridge,
}
));
})
);

let randoTransfer;
{
console.log("transfer rando token from near to algorand");
console.log("YYY transfer rando token from near to algorand");
let s = await transferTokenFromNear(
userAccount,
core_bridge,
Expand Down Expand Up @@ -566,27 +565,27 @@ async function testNearSDK() {
console.log(_parseVAAAlgorand(randoTransfer));

if (s[1] != getEmitterAddressNear(token_bridge)) {
console.log("Unexpected emitter address: " + s[1] + "!=" + getEmitterAddressNear(token_bridge));
process.exit(1);
console.log(
"Unexpected emitter address: " +
s[1] +
"!=" +
getEmitterAddressNear(token_bridge)
);
process.exit(1);
}
}

console.log(await userAccount.viewFunction(
randoToken,
"ft_balance_of",
{
console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: userAccount.accountId,
}
));
})
);

console.log(await userAccount.viewFunction(
randoToken,
"ft_balance_of",
{
console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: token_bridge,
}
));

})
);

let nearTransfer;
{
Expand Down Expand Up @@ -639,7 +638,7 @@ async function testNearSDK() {

let randoAssetId;
{
console.log("redeeming our near native asset on Algorand");
console.log("YYY redeeming our near native asset on Algorand");
const tx = await redeemOnAlgorand(
algoClient,
algoToken,
Expand Down Expand Up @@ -721,18 +720,17 @@ async function testNearSDK() {
).vaaBytes;
}

console.log("transfering rando from Algo To Near... getting the vaa");
console.log("YYY transfering rando from Algo To Near... getting the vaa");
let transferAlgoToNearRando;
{
const AmountToTransfer: number = 100;
const Fee: number = 20;
const Fee: number = 0;
const transferTxs = await transferFromAlgorand(
algoClient,
algoToken,
algoCore,
algoWallet.addr,
randoAssetId,
BigInt(AmountToTransfer),
BigInt(20) * BigInt("1000000000") * BigInt("100000000"),
myAddress,
CHAIN_ID_NEAR,
BigInt(Fee)
Expand Down Expand Up @@ -793,12 +791,37 @@ async function testNearSDK() {
);

console.log(
"redeeming Rando on Near: " + uint8ArrayToHex(transferAlgoToNearRando)
"YYY redeeming Rando on Near: " + uint8ArrayToHex(transferAlgoToNearRando)
);

console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: userAccount.accountId,
})
);

console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: token_bridge,
})
);

console.log(
await redeemOnNear(user2Account, token_bridge, transferAlgoToNearRando)
);

console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: userAccount.accountId,
})
);

console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: token_bridge,
})
);

console.log("redeeming NEAR on Near");
console.log(
await redeemOnNear(user2Account, token_bridge, transferAlgoToNearNEAR)
Expand Down Expand Up @@ -854,21 +877,20 @@ async function testNearSDK() {
await redeemOnNear(user2Account, token_bridge, transferAlgoToNearP3)
);

console.log("transfering rando from Algo To Near... getting the vaa");
console.log("YYY P3 transfering rando from Algo To Near... getting the vaa");
let transferAlgoToNearRandoP3;
{
const AmountToTransfer: number = 100;
const Fee: number = 20;
const Fee: number = 0;
const transferTxs = await transferFromAlgorand(
algoClient,
algoToken,
algoCore,
algoWallet.addr,
randoAssetId,
BigInt(AmountToTransfer),
BigInt(1) * BigInt("1000000000") * BigInt("100000000"),
userAccount2Address,
CHAIN_ID_NEAR,
BigInt(Fee),
BigInt(Fee) * BigInt("100000000"),
hexToUint8Array("ff")
);
const transferResult = await signSendAndConfirmAlgorand(
Expand All @@ -888,12 +910,24 @@ async function testNearSDK() {
).vaaBytes;
}

console.log("redeeming P3 random on Near");
console.log("YYY redeeming P3 random on Near");
console.log(
await redeemOnNear(user2Account, token_bridge, transferAlgoToNearRandoP3)
);
}

console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: userAccount.accountId,
})
);

console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: token_bridge,
})
);

console.log("What next?");
}

Expand Down

0 comments on commit 6235c3b

Please sign in to comment.