2022-08-19 08:29:57 -07:00
|
|
|
import { impossible, Payload } from "./vaa";
|
|
|
|
import { NETWORKS } from "./networks";
|
|
|
|
import { CONTRACTS } from "@certusone/wormhole-sdk";
|
|
|
|
|
|
|
|
const BN = require("bn.js");
|
|
|
|
const nearAPI = require("near-api-js");
|
|
|
|
|
|
|
|
export async function execute_near(
|
|
|
|
payload: Payload,
|
|
|
|
vaa: string,
|
|
|
|
network: "MAINNET" | "TESTNET" | "DEVNET"
|
|
|
|
) {
|
|
|
|
let n = NETWORKS[network]["near"];
|
|
|
|
let contracts = CONTRACTS[network]["near"];
|
|
|
|
|
2022-08-26 09:46:37 -07:00
|
|
|
let target_contract = "";
|
2022-09-28 06:53:15 -07:00
|
|
|
let numSubmits = 1;
|
2022-08-19 08:29:57 -07:00
|
|
|
|
|
|
|
switch (payload.module) {
|
|
|
|
case "Core":
|
|
|
|
if (contracts.core === undefined) {
|
|
|
|
throw new Error("Core bridge not supported yet for near");
|
|
|
|
}
|
2022-08-26 09:46:37 -07:00
|
|
|
target_contract = contracts.core;
|
2022-08-19 08:29:57 -07:00
|
|
|
switch (payload.type) {
|
|
|
|
case "GuardianSetUpgrade":
|
|
|
|
console.log("Submitting new guardian set");
|
|
|
|
break;
|
|
|
|
case "ContractUpgrade":
|
|
|
|
console.log("Upgrading core contract");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
impossible(payload);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "NFTBridge":
|
|
|
|
if (contracts.nft_bridge === undefined) {
|
|
|
|
throw new Error("NFT bridge not supported yet for near");
|
|
|
|
}
|
2022-09-28 06:53:15 -07:00
|
|
|
numSubmits = 2;
|
2022-08-26 09:46:37 -07:00
|
|
|
target_contract = contracts.nft_bridge;
|
2022-08-19 08:29:57 -07:00
|
|
|
switch (payload.type) {
|
|
|
|
case "ContractUpgrade":
|
|
|
|
console.log("Upgrading contract");
|
|
|
|
break;
|
|
|
|
case "RegisterChain":
|
|
|
|
console.log("Registering chain");
|
|
|
|
break;
|
|
|
|
case "Transfer":
|
|
|
|
console.log("Completing transfer");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
impossible(payload);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "TokenBridge":
|
2022-08-26 09:46:37 -07:00
|
|
|
if (contracts.token_bridge === undefined) {
|
|
|
|
throw new Error("Token bridge not supported yet for near");
|
|
|
|
}
|
2022-09-28 06:53:15 -07:00
|
|
|
numSubmits = 2;
|
2022-08-26 09:46:37 -07:00
|
|
|
target_contract = contracts.token_bridge;
|
2022-08-19 08:29:57 -07:00
|
|
|
switch (payload.type) {
|
|
|
|
case "ContractUpgrade":
|
|
|
|
console.log("Upgrading contract");
|
|
|
|
break;
|
|
|
|
case "RegisterChain":
|
|
|
|
console.log("Registering chain");
|
|
|
|
break;
|
|
|
|
case "Transfer":
|
|
|
|
console.log("Completing transfer");
|
|
|
|
break;
|
|
|
|
case "AttestMeta":
|
|
|
|
console.log("Creating wrapped token");
|
|
|
|
break;
|
|
|
|
case "TransferWithPayload":
|
|
|
|
throw Error("Can't complete payload 3 transfer from CLI");
|
|
|
|
default:
|
|
|
|
impossible(payload);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
impossible(payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
let key = nearAPI.utils.KeyPair.fromString(n.key);
|
|
|
|
|
|
|
|
let keyStore = new nearAPI.keyStores.InMemoryKeyStore();
|
2022-08-26 09:46:37 -07:00
|
|
|
keyStore.setKey(n.networkId, n.deployerAccount, key);
|
2022-08-19 08:29:57 -07:00
|
|
|
|
|
|
|
let near = await nearAPI.connect({
|
2022-09-28 06:53:15 -07:00
|
|
|
keyStore,
|
2022-08-19 08:29:57 -07:00
|
|
|
networkId: n.networkId,
|
|
|
|
nodeUrl: n.rpc,
|
|
|
|
});
|
|
|
|
|
2022-08-26 09:46:37 -07:00
|
|
|
let nearAccount = new nearAPI.Account(near.connection, n.deployerAccount);
|
2022-08-19 08:29:57 -07:00
|
|
|
|
|
|
|
console.log("submitting vaa the first time");
|
|
|
|
let result1 = await nearAccount.functionCall({
|
|
|
|
contractId: target_contract,
|
|
|
|
methodName: "submit_vaa",
|
|
|
|
args: {
|
|
|
|
vaa: vaa,
|
|
|
|
},
|
|
|
|
attachedDeposit: new BN("100000000000000000000000"),
|
|
|
|
gas: new BN("300000000000000"),
|
|
|
|
});
|
|
|
|
|
2022-08-26 09:46:37 -07:00
|
|
|
if (numSubmits <= 1) {
|
|
|
|
console.log("Hash: " + result1.transaction.hash);
|
2022-09-28 06:53:15 -07:00
|
|
|
return;
|
2022-08-26 09:46:37 -07:00
|
|
|
}
|
|
|
|
|
2022-08-19 08:29:57 -07:00
|
|
|
// You have to feed a vaa twice into the contract (two submits),
|
|
|
|
// The first time, it checks if it has been seen at all.
|
|
|
|
// The second time, it executes.
|
|
|
|
console.log("submitting vaa the second time");
|
|
|
|
let result2 = await nearAccount.functionCall({
|
|
|
|
contractId: target_contract,
|
|
|
|
methodName: "submit_vaa",
|
|
|
|
args: {
|
|
|
|
vaa: vaa,
|
|
|
|
},
|
|
|
|
attachedDeposit: new BN("100000000000000000000000"),
|
|
|
|
gas: new BN("300000000000000"),
|
|
|
|
});
|
|
|
|
|
|
|
|
let txHash = result1.transaction.hash + ":" + result2.transaction.hash;
|
|
|
|
console.log("Hash: " + txHash);
|
|
|
|
}
|