near/p3: Fix P3

This commit is contained in:
Josh Siegel 2022-10-04 13:47:55 +00:00 committed by jumpsiegel
parent 58cd430541
commit d48b13ceee
5 changed files with 228 additions and 62 deletions

View File

@ -2,6 +2,15 @@ const algosdk = require("@certusone/wormhole-sdk/node_modules/algosdk");
import { calcLogicSigAccount } from "@certusone/wormhole-sdk/lib/cjs/algorand";
import {
coalesceChainName,
ChainId,
coalesceChainId,
uint8ArrayToHex,
toChainName,
getOriginalAssetAlgorand,
} from "@certusone/wormhole-sdk";
export async function getNativeAlgoAddress(
algoClient: any,
token_bridge: any,
@ -25,7 +34,7 @@ async function firstTransaction() {
let token;
let appid;
const mainnet = false;
const mainnet = true;
if (mainnet) {
appid = 842126029;
@ -93,12 +102,20 @@ async function firstTransaction() {
let algoInfo = await algodClient.accountInformation(nativeAlgoAddr).do();
console.log("ALGO locked: " + (algoInfo["amount"] - 1002001));
console.log("wormhole assets");
console.log("wormhole assets (bridged in)");
for (let i = 0; i < wormholeAssets.length; i++) {
console.log(wormholeAssets[i]);
console.log(
await algodClient.getAssetByID(wormholeAssets[i]["asset-id"]).do()
let orig = await getOriginalAssetAlgorand(
algodClient,
BigInt(appid),
wormholeAssets[i]["asset-id"]
);
let v = [
coalesceChainName(orig["chainId"]),
uint8ArrayToHex(orig["assetAddress"]),
wormholeAssets[i],
await algodClient.getAssetByID(wormholeAssets[i]["asset-id"]).do(),
];
console.log(v);
}
console.log("native assets");

View File

@ -75,17 +75,17 @@ clean:
.PHONY: reset-force
reset-force: build
-echo cleaning and restarting state
-kubectl delete pod guardian-0 --force &
-kubectl delete pod algorand-0 --force &
-kubectl delete pod near-0 --force &
-minikube kubectl -- delete pod guardian-0 --force &
-minikube kubectl -- delete pod algorand-0 --force &
-minikube kubectl -- delete pod near-0 --force &
-echo done
.PHONY: reset
reset: build
-echo cleaning and restarting state
-kubectl delete pod guardian-0 &
-kubectl delete pod algorand-0 &
-kubectl delete pod near-0 &
-minikube kubectl -- delete pod guardian-0 &
-minikube kubectl -- delete pod algorand-0 &
-minikube kubectl -- delete pod near-0 &
-echo done
.PHONY: cycle

View File

@ -105,6 +105,76 @@ impl FTContract {
self.meta.replace(&meta);
}
#[payable]
pub fn wh_burn(&mut self, from: AccountId, amount: u128) {
assert_one_yocto();
if env::predecessor_account_id() != self.controller {
env::panic_str("CrossContractInvalidCaller");
}
self.token.internal_withdraw(&from, amount);
near_contract_standards::fungible_token::events::FtBurn {
owner_id: &from,
amount: &U128::from(amount),
memo: Some("Wormhole burn"),
}
.emit();
}
#[payable]
pub fn wh_mint(
&mut self,
account_id: AccountId,
refund_to: AccountId,
amount: u128,
) -> Promise {
if env::predecessor_account_id() != self.controller {
env::panic_str("CrossContractInvalidCaller");
}
let mut deposit: Balance = env::attached_deposit();
if deposit == 0 {
env::panic_str("ZeroDepositNotAllowed");
}
if !self.token.accounts.contains_key(&account_id) {
let min_balance = self.storage_balance_bounds().min.0;
if deposit < min_balance {
env::panic_str("The attached deposit is less than the minimum storage balance");
}
self.token.internal_register_account(&account_id);
deposit -= min_balance;
}
self.token.internal_deposit(&account_id, amount);
near_contract_standards::fungible_token::events::FtMint {
owner_id: &account_id,
amount: &U128::from(amount),
memo: Some("wormhole minted tokens"),
}
.emit();
Promise::new(refund_to).transfer(deposit)
}
#[payable]
pub fn wh_update(&mut self, v: Vec<u8>) -> Promise {
assert_one_yocto();
if env::predecessor_account_id() != self.controller {
env::panic_str("CrossContractInvalidCaller");
}
Promise::new(env::current_account_id())
.deploy_contract(v.to_vec())
}
#[payable]
pub fn vaa_withdraw(
&mut self,
@ -161,7 +231,9 @@ impl FTContract {
env::panic_str("Payload3 does not support fees");
}
p = [p, hex::decode(&payload).unwrap()].concat();
let account_hash = env::sha256(from.as_bytes());
p = [p, account_hash, hex::decode(&payload).unwrap()].concat();
if p.len() != (133 + (payload.len() / 2)) {
env::panic_str(&format!("payload3 formatting error len = {}", p.len()));
}

View File

@ -404,10 +404,15 @@ impl TokenBridge {
}
env::log_str(&format!(
"token-bridge/{}#{}: vaa_transfer {} {} {} {} {}",
file!(),
line!(),
amount.1, fee.1, namount, nfee, near_mult));
"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 {}",
@ -618,7 +623,6 @@ impl TokenBridge {
Promise::new(asset_token_account.clone())
.create_account()
.transfer(cost)
.add_full_access_key(self.owner_pk.clone())
.deploy_contract(BRIDGE_TOKEN_BINARY.to_vec())
.function_call(
"new".to_string(),
@ -1021,7 +1025,18 @@ impl TokenBridge {
env::panic_str("Payload1 formatting error");
}
} else {
p = [p, hex::decode(&payload).unwrap()].concat();
let account_hash = env::sha256(env::predecessor_account_id().as_bytes());
if !self.hash_map.contains_key(&account_hash) {
env::log_str(&format!(
"token-bridge/{}#{}: default(): {}",
file!(),
line!(),
env::predecessor_account_id()
));
env::panic_str("UnregisteredSender1");
}
p = [p, account_hash, hex::decode(&payload).unwrap()].concat();
if p.len() != (133 + (payload.len() / 2)) {
env::panic_str("Payload3 formatting error");
}
@ -1066,6 +1081,17 @@ impl TokenBridge {
);
if self.is_wormhole(&token) {
let account_hash = env::sha256(env::predecessor_account_id().as_bytes());
if !self.hash_map.contains_key(&account_hash) {
env::log_str(&format!(
"token-bridge/{}#{}: default(): {}",
file!(),
line!(),
env::predecessor_account_id()
));
env::panic_str("UnregisteredSender2");
}
ext_ft_contract::ext(AccountId::try_from(token).unwrap())
.with_attached_deposit(1)
.with_static_gas(Gas(30_000_000_000_000))
@ -1546,7 +1572,18 @@ impl TokenBridge {
env::panic_str(&format!("payload1 formatting error len = {}", p.len()));
}
} else {
p = [p, hex::decode(&tp.payload).unwrap()].concat();
let account_hash = env::sha256(sender_id.as_bytes());
if !self.hash_map.contains_key(&account_hash) {
env::log_str(&format!(
"token-bridge/{}#{}: default(): {}",
file!(),
line!(),
sender_id
));
env::panic_str("UnregisteredSender3");
}
p = [p, account_hash, hex::decode(&tp.payload).unwrap()].concat();
if p.len() != (133 + (tp.payload.len() / 2)) {
env::panic_str(&format!("payload3 formatting error len = {}", p.len()));
}
@ -1692,23 +1729,23 @@ impl TokenBridge {
let state: TokenBridge = env::state_read().expect("failed");
state
// let old_state: OldPortal = env::state_read().expect("failed");
// Self {
// booted: old_state.booted,
// core: old_state.core,
// gov_idx: old_state.gov_idx,
// dups: old_state.dups,
// owner_pk: old_state.owner_pk,
// emitter_registration: old_state.emitter_registration,
// last_asset: old_state.last_asset,
// upgrade_hash: old_state.upgrade_hash,
//
// tokens: old_state.tokens,
// key_map: old_state.key_map,
// hash_map: old_state.hash_map,
//
// bank: old_state.bank,
// }
// let old_state: OldPortal = env::state_read().expect("failed");
// Self {
// booted: old_state.booted,
// core: old_state.core,
// gov_idx: old_state.gov_idx,
// dups: old_state.dups,
// owner_pk: old_state.owner_pk,
// emitter_registration: old_state.emitter_registration,
// last_asset: old_state.last_asset,
// upgrade_hash: old_state.upgrade_hash,
//
// tokens: old_state.tokens,
// key_map: old_state.key_map,
// hash_map: old_state.hash_map,
//
// bank: old_state.bank,
// }
}
}

View File

@ -327,13 +327,13 @@ async function testNearSDK() {
console.log("for norium, createWrappedOnNear returned");
let account_hash = await userAccount.viewFunction(
token_bridge,
"hash_account",
{
let account_hash = await userAccount.viewFunction({
contractId: token_bridge,
methodName: "hash_account",
args: {
account: userAccount.accountId,
}
);
},
});
console.log(account_hash);
@ -705,14 +705,22 @@ async function testNearSDK() {
}
console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: userAccount.accountId,
await userAccount.viewFunction({
contractId: randoToken,
methodName: "ft_balance_of",
args: {
account_id: userAccount.accountId,
},
})
);
console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: token_bridge,
await userAccount.viewFunction({
contractId: randoToken,
methodName: "ft_balance_of",
args: {
account_id: token_bridge,
},
})
);
@ -757,14 +765,22 @@ async function testNearSDK() {
}
console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: userAccount.accountId,
await userAccount.viewFunction({
contractId: randoToken,
methodName: "ft_balance_of",
args: {
account_id: userAccount.accountId,
},
})
);
console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: token_bridge,
await userAccount.viewFunction({
contractId: randoToken,
methodName: "ft_balance_of",
args: {
account_id: token_bridge,
},
})
);
@ -986,14 +1002,22 @@ async function testNearSDK() {
);
console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: userAccount.accountId,
await userAccount.viewFunction({
contractId: randoToken,
methodName: "ft_balance_of",
args: {
account_id: userAccount.accountId,
},
})
);
console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: token_bridge,
await userAccount.viewFunction({
contractId: randoToken,
methodName: "ft_balance_of",
args: {
account_id: token_bridge,
},
})
);
@ -1008,14 +1032,22 @@ async function testNearSDK() {
}
console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: userAccount.accountId,
await userAccount.viewFunction({
contractId: randoToken,
methodName: "ft_balance_of",
args: {
account_id: userAccount.accountId,
},
})
);
console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: token_bridge,
await userAccount.viewFunction({
contractId: randoToken,
methodName: "ft_balance_of",
args: {
account_id: token_bridge,
},
})
);
@ -1134,14 +1166,22 @@ async function testNearSDK() {
}
console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: userAccount.accountId,
await userAccount.viewFunction({
contractId: randoToken,
methodName: "ft_balance_of",
args: {
account_id: userAccount.accountId,
},
})
);
console.log(
await userAccount.viewFunction(randoToken, "ft_balance_of", {
account_id: token_bridge,
await userAccount.viewFunction({
contractId: randoToken,
methodName: "ft_balance_of",
args: {
account_id: token_bridge,
},
})
);