Skip to content

Commit

Permalink
sdk/js: Near refactor, added integration tests (wormhole-foundation#1648
Browse files Browse the repository at this point in the history
)

* near-sdk-refactor: Fix attest
                 : Fix missing function calls
                 : make near on near
                 : renamed tryHexToNativeStringNear

* near-sdk-refactor: bumped near-sdk-js version

Co-authored-by: Josh Siegel <jsiegel@jumptrading.com>
  • Loading branch information
kev1n-peters and jumpsiegel authored Sep 28, 2022
1 parent 02cf085 commit c5d2f9d
Show file tree
Hide file tree
Showing 38 changed files with 1,016 additions and 681 deletions.
6 changes: 4 additions & 2 deletions Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ docker_build(
context = ".",
dockerfile = "Dockerfile.node",
target = "build",
ignore=["./sdk/js"]
)

def command_with_dlv(argv):
Expand Down Expand Up @@ -697,9 +698,10 @@ if near:
)

docker_build(
ref = "near-contracts",
ref = "near-deploy",
context = "near",
dockerfile = "near/Dockerfile.contracts",
dockerfile = "near/Dockerfile.deploy",
ignore = ["./test"]
)

k8s_resource(
Expand Down
12 changes: 5 additions & 7 deletions clients/js/near.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function execute_near(
let contracts = CONTRACTS[network]["near"];

let target_contract = "";
let numSubmits = 1
let numSubmits = 1;

switch (payload.module) {
case "Core":
Expand All @@ -37,7 +37,7 @@ export async function execute_near(
if (contracts.nft_bridge === undefined) {
throw new Error("NFT bridge not supported yet for near");
}
numSubmits = 2
numSubmits = 2;
target_contract = contracts.nft_bridge;
switch (payload.type) {
case "ContractUpgrade":
Expand All @@ -57,7 +57,7 @@ export async function execute_near(
if (contracts.token_bridge === undefined) {
throw new Error("Token bridge not supported yet for near");
}
numSubmits = 2
numSubmits = 2;
target_contract = contracts.token_bridge;
switch (payload.type) {
case "ContractUpgrade":
Expand Down Expand Up @@ -89,9 +89,7 @@ export async function execute_near(
keyStore.setKey(n.networkId, n.deployerAccount, key);

let near = await nearAPI.connect({
deps: {
keyStore,
},
keyStore,
networkId: n.networkId,
nodeUrl: n.rpc,
});
Expand All @@ -111,7 +109,7 @@ export async function execute_near(

if (numSubmits <= 1) {
console.log("Hash: " + result1.transaction.hash);
return
return;
}

// You have to feed a vaa twice into the contract (two submits),
Expand Down
2 changes: 2 additions & 0 deletions cosmwasm/tools/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ const contract_registrations = {
process.env.REGISTER_ALGO_TOKEN_BRIDGE_VAA,
// TERRA
process.env.REGISTER_TERRA_TOKEN_BRIDGE_VAA,
// NEAR
process.env.REGISTER_NEAR_TOKEN_BRIDGE_VAA,
],
};

Expand Down
2 changes: 1 addition & 1 deletion devnet/eth-devnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ spec:
command:
- /bin/sh
- -c
- "npm run migrate && npx truffle exec scripts/deploy_test_token.js && npm run deploy-batched-vaa-sender && npx truffle exec scripts/register_solana_chain.js && npx truffle exec scripts/register_terra_chain.js && npx truffle exec scripts/register_terra2_chain.js && npx truffle exec scripts/register_bsc_chain.js && npx truffle exec scripts/register_algo_chain.js && nc -lkp 2000 0.0.0.0"
- "npm run migrate && npx truffle exec scripts/deploy_test_token.js && npm run deploy-batched-vaa-sender && npx truffle exec scripts/register_solana_chain.js && npx truffle exec scripts/register_terra_chain.js && npx truffle exec scripts/register_terra2_chain.js && npx truffle exec scripts/register_bsc_chain.js && npx truffle exec scripts/register_algo_chain.js && npx truffle exec scripts/register_near_chain.js && nc -lkp 2000 0.0.0.0"
readinessProbe:
periodSeconds: 1
failureThreshold: 300
Expand Down
2 changes: 1 addition & 1 deletion devnet/eth-devnet2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ spec:
command:
- /bin/sh
- -c
- "sed -i 's/CHAIN_ID=0x2/CHAIN_ID=0x4/g;s/EVM_CHAIN_ID=1/EVM_CHAIN_ID=1397/g' .env && npm run migrate && npx truffle exec scripts/deploy_test_token.js && npx truffle exec scripts/register_solana_chain.js && npx truffle exec scripts/register_terra_chain.js && npx truffle exec scripts/register_terra2_chain.js && npx truffle exec scripts/register_eth_chain.js && npx truffle exec scripts/register_algo_chain.js && nc -lkp 2000 0.0.0.0"
- "sed -i 's/CHAIN_ID=0x2/CHAIN_ID=0x4/g;s/EVM_CHAIN_ID=1/EVM_CHAIN_ID=1397/g' .env && npm run migrate && npx truffle exec scripts/deploy_test_token.js && npx truffle exec scripts/register_solana_chain.js && npx truffle exec scripts/register_terra_chain.js && npx truffle exec scripts/register_terra2_chain.js && npx truffle exec scripts/register_eth_chain.js && npx truffle exec scripts/register_algo_chain.js && npx truffle exec scripts/register_near_chain.js && nc -lkp 2000 0.0.0.0"
readinessProbe:
periodSeconds: 1
failureThreshold: 300
Expand Down
6 changes: 3 additions & 3 deletions devnet/near-devnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ spec:
readinessProbe:
tcpSocket:
port: 3030
- name: near-contracts
image: near-contracts
- name: near-deploy
image: near-deploy
command:
- /bin/sh
- -c
- "sh devnet_deploy.sh && touch success && sleep infinity"
- "sh /app/devnet_deploy.sh && touch success && sleep infinity"
readinessProbe:
tcpSocket:
port: 3030
Expand Down
29 changes: 29 additions & 0 deletions ethereum/scripts/register_near_chain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// run this script with truffle exec

const jsonfile = require("jsonfile");
const TokenBridge = artifacts.require("TokenBridge");
const BridgeImplementationFullABI = jsonfile.readFileSync(
"../build/contracts/BridgeImplementation.json"
).abi;
const nearTokenBridgeVAA = process.env.REGISTER_NEAR_TOKEN_BRIDGE_VAA;

module.exports = async function (callback) {
try {
const accounts = await web3.eth.getAccounts();
const tokenBridge = new web3.eth.Contract(
BridgeImplementationFullABI,
TokenBridge.address
);

// Register the near token bridge endpoint
await tokenBridge.methods.registerChain("0x" + nearTokenBridgeVAA).send({
value: 0,
from: accounts[0],
gasLimit: 2000000,
});

callback();
} catch (e) {
callback(e);
}
};
28 changes: 28 additions & 0 deletions near/Dockerfile.deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM debian@sha256:2ce44bbc00a79113c296d9d25524e15d423b23303fdbbe20190d2f96e0aeb251 as build

RUN apt-get update && apt-get install -y curl \
make \
build-essential

WORKDIR /app
COPY ./setup-rust.sh .
RUN ./setup-rust.sh

COPY . .

RUN ./build-contracts.sh

FROM node:16-alpine@sha256:004dbac84fed48e20f9888a23e32fa7cf83c2995e174a78d41d9a9dd1e051a20 AS deploy

WORKDIR /app
COPY package.json .
COPY package-lock.json .
COPY .env .
COPY devnet_deploy.sh .
COPY devnet_deploy.ts .
COPY --from=build /app/contracts/*/target/wasm32-unknown-unknown/release/*.wasm .

# mount the buildkit cache on npm's cache dir, install dependencies
RUN --mount=type=cache,target=/root/.npm npm ci --production


175 changes: 89 additions & 86 deletions near/devnet_deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ async function initNear() {
keyStore.setKey(config.networkId, config.masterAccount, masterKey);

let near = await nearAPI.connect({
deps: {
keyStore,
},
keyStore,
networkId: config.networkId,
nodeUrl: config.nodeUrl,
});
Expand All @@ -76,21 +74,25 @@ async function initNear() {
);

if (e === "sandbox") {
let da = parseSeedPhrase("weather opinion slam purpose access artefact word orbit matter rice poem badge");
console.log(da);
let resp = await masterAccount.createAccount(
"devnet.test.near",
da.publicKey,
new BN(10).pow(new BN(27))
);

console.log("devnet.test.near funded");
let da = parseSeedPhrase(
"weather opinion slam purpose access artefact word orbit matter rice poem badge"
);
console.log(da);
let resp = await masterAccount.createAccount(
"devnet.test.near",
da.publicKey,
new BN(10).pow(new BN(27))
);

console.log("devnet.test.near funded");
}

const wormholeContract = await fs.readFileSync("./near_wormhole.wasm");
const tokenContract = await fs.readFileSync("./near_token_bridge.wasm");
const nftContract = await fs.readFileSync("./near_nft_bridge.wasm");
const testContract = await fs.readFileSync("./near_mock_bridge_integration.wasm");
const testContract = await fs.readFileSync(
"./near_mock_bridge_integration.wasm"
);

let wormholeAccount: any;

Expand All @@ -108,27 +110,27 @@ async function initNear() {
new BN("20000000000000000000000000")
);

await wormholeAccount.functionCall({
contractId: config.wormholeAccount,
methodName: "register_emitter",
args: {emitter: config.tokenAccount},
attachedDeposit: new BN("30000000000000000000000"),
gas: new BN("100000000000000"),
})
await wormholeAccount.functionCall({
contractId: config.wormholeAccount,
methodName: "register_emitter",
args: {emitter: config.testAccount},
attachedDeposit: new BN("30000000000000000000000"),
gas: new BN("100000000000000"),
})
await wormholeAccount.functionCall({
contractId: config.wormholeAccount,
methodName: "register_emitter",
args: {emitter: config.nftAccount},
attachedDeposit: new BN("30000000000000000000000"),
gas: new BN("100000000000000"),
})
await wormholeAccount.functionCall({
contractId: config.wormholeAccount,
methodName: "register_emitter",
args: { emitter: config.tokenAccount },
attachedDeposit: new BN("30000000000000000000000"),
gas: new BN("100000000000000"),
});
await wormholeAccount.functionCall({
contractId: config.wormholeAccount,
methodName: "register_emitter",
args: { emitter: config.testAccount },
attachedDeposit: new BN("30000000000000000000000"),
gas: new BN("100000000000000"),
});
await wormholeAccount.functionCall({
contractId: config.wormholeAccount,
methodName: "register_emitter",
args: { emitter: config.nftAccount },
attachedDeposit: new BN("30000000000000000000000"),
gas: new BN("100000000000000"),
});
} else {
// This uses the standard API to redeploy ... we can migrate over to the vaa's later
console.log(
Expand All @@ -140,18 +142,17 @@ async function initNear() {
);
await wormholeAccount.deployContract(wormholeContract);

// console.log("migrating " + config.wormholeAccount);
// console.log(
// await wormholeAccount.functionCall({
// contractId: config.wormholeAccount,
// methodName: "migrate",
// args: {},
// attachedDeposit: new BN(1),
// gas: new BN("100000000000000"),
// })
// );
// console.log("done migrating " + config.tokenAccount);

// console.log("migrating " + config.wormholeAccount);
// console.log(
// await wormholeAccount.functionCall({
// contractId: config.wormholeAccount,
// methodName: "migrate",
// args: {},
// attachedDeposit: new BN(1),
// gas: new BN("100000000000000"),
// })
// );
// console.log("done migrating " + config.tokenAccount);
}

let tokenAccount: any;
Expand All @@ -170,19 +171,21 @@ async function initNear() {
tokenAccount = new nearAPI.Account(near.connection, config.tokenAccount);
await tokenAccount.deployContract(tokenContract);

console.log( await tokenAccount.viewFunction(config.tokenAccount, "emitter", {}));

// console.log("migrating " + config.tokenAccount);
// console.log(
// await tokenAccount.functionCall({
// contractId: config.tokenAccount,
// methodName: "migrate",
// args: {},
// attachedDeposit: new BN(1),
// gas: new BN("100000000000000"),
// })
// );
// console.log("done migrating " + config.tokenAccount);
console.log(
await tokenAccount.viewFunction(config.tokenAccount, "emitter", {})
);

// console.log("migrating " + config.tokenAccount);
// console.log(
// await tokenAccount.functionCall({
// contractId: config.tokenAccount,
// methodName: "migrate",
// args: {},
// attachedDeposit: new BN(1),
// gas: new BN("100000000000000"),
// })
// );
// console.log("done migrating " + config.tokenAccount);
}

let nftAccount: any;
Expand Down Expand Up @@ -286,33 +289,33 @@ async function initNear() {
});
}

// for (const line of vaasNFT) {
// console.log("Submitting to " + config.nftAccount + ": " + line);
//
// try {
// await masterAccount.functionCall({
// contractId: config.nftAccount,
// methodName: "submit_vaa",
// args: {
// vaa: line,
// },
// attachedDeposit: new BN("30000000000000000000000"),
// gas: new BN("300000000000000"),
// });
//
// await masterAccount.functionCall({
// contractId: config.nftAccount,
// methodName: "submit_vaa",
// args: {
// vaa: line,
// },
// attachedDeposit: new BN("30000000000000000000000"),
// gas: new BN("300000000000000"),
// });
// } catch {
// console.log("Exception thrown.. ");
// }
// }
// for (const line of vaasNFT) {
// console.log("Submitting to " + config.nftAccount + ": " + line);
//
// try {
// await masterAccount.functionCall({
// contractId: config.nftAccount,
// methodName: "submit_vaa",
// args: {
// vaa: line,
// },
// attachedDeposit: new BN("30000000000000000000000"),
// gas: new BN("300000000000000"),
// });
//
// await masterAccount.functionCall({
// contractId: config.nftAccount,
// methodName: "submit_vaa",
// args: {
// vaa: line,
// },
// attachedDeposit: new BN("30000000000000000000000"),
// gas: new BN("300000000000000"),
// });
// } catch {
// console.log("Exception thrown.. ");
// }
// }

console.log("nft bridge booted");

Expand Down
Loading

0 comments on commit c5d2f9d

Please sign in to comment.