Skip to content

Commit 7965322

Browse files
committed
include WFLOW and all bridge tests
1 parent b20834f commit 7965322

File tree

12 files changed

+730
-75
lines changed

12 files changed

+730
-75
lines changed

engine/execution/state/bootstrap/bootstrap_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestBootstrapLedger(t *testing.T) {
5353
}
5454

5555
func TestBootstrapLedger_ZeroTokenSupply(t *testing.T) {
56-
expectedStateCommitmentBytes, _ := hex.DecodeString("ea660c27b27ecfa7f93ee0462918b60145db8a78bc6a7e428574d4150b83b1b5")
56+
expectedStateCommitmentBytes, _ := hex.DecodeString("27e23bc91b53d9b3b20af6e23ef506009715798ea32cae78157d84840e8747f5")
5757
expectedStateCommitment, err := flow.ToStateCommitment(expectedStateCommitmentBytes)
5858
require.NoError(t, err)
5959

fvm/blueprints/bridge.go

+156
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,48 @@ func UpsertContractCodeChunksTransaction(
289289
AddAuthorizer(service)
290290
}
291291

292+
// CreateWFLOWTokenHandlerTransaction returns the transaction body for the transaction
293+
// that creates a token handler for the WFLOW Solidity contract
294+
func CreateWFLOWTokenHandlerTransaction(
295+
service flow.Address,
296+
bridgeEnv bridge.Environment,
297+
env templates.Environment,
298+
wflowEVMAddress string,
299+
) *flow.TransactionBody {
300+
txScript, _ := bridge.GetCadenceTransactionCode("cadence/transactions/bridge/admin/token-handler/create_wflow_token_handler.cdc", bridgeEnv, env)
301+
302+
return flow.NewTransactionBody().
303+
SetScript([]byte(
304+
txScript,
305+
),
306+
).
307+
AddArgument(jsoncdc.MustEncode(cadence.String(wflowEVMAddress))).
308+
SetProposalKey(service, 0, 0).
309+
SetPayer(service).
310+
AddAuthorizer(service)
311+
}
312+
313+
// EnableWFLOWTokenHandlerTransaction returns the transaction body for the transaction
314+
// that enables the token handler for the WFLOW Solidity contract
315+
func EnableWFLOWTokenHandlerTransaction(
316+
service flow.Address,
317+
bridgeEnv bridge.Environment,
318+
env templates.Environment,
319+
flowTokenType string,
320+
) *flow.TransactionBody {
321+
txScript, _ := bridge.GetCadenceTransactionCode("cadence/transactions/bridge/admin/token-handler/enable_token_handler.cdc", bridgeEnv, env)
322+
323+
return flow.NewTransactionBody().
324+
SetScript([]byte(
325+
txScript,
326+
),
327+
).
328+
AddArgument(jsoncdc.MustEncode(cadence.String(flowTokenType))).
329+
SetProposalKey(service, 0, 0).
330+
SetPayer(service).
331+
AddAuthorizer(service)
332+
}
333+
292334
// OnboardToBridgeByTypeIDTransaction returns the transaction body for the transaction
293335
// that onboards a FT or NFT type to the bridge
294336
func OnboardToBridgeByTypeIDTransaction(
@@ -309,3 +351,117 @@ func OnboardToBridgeByTypeIDTransaction(
309351
SetPayer(service).
310352
AddAuthorizer(service)
311353
}
354+
355+
// BridgeFTToEVMTransaction returns the transaction body for the transaction
356+
// that bridges a fungible token from Cadence to EVM
357+
func BridgeFTToEVMTransaction(
358+
service flow.Address,
359+
bridgeEnv bridge.Environment,
360+
env templates.Environment,
361+
forType string,
362+
amount string,
363+
) *flow.TransactionBody {
364+
txScript, _ := bridge.GetCadenceTransactionCode("cadence/transactions/bridge/tokens/bridge_tokens_to_evm.cdc", bridgeEnv, env)
365+
bridgeAmount, _ := cadence.NewUFix64(amount)
366+
return flow.NewTransactionBody().
367+
SetScript([]byte(
368+
txScript,
369+
),
370+
).
371+
AddArgument(jsoncdc.MustEncode(cadence.String(forType))).
372+
AddArgument(jsoncdc.MustEncode(bridgeAmount)).
373+
SetProposalKey(service, 0, 0).
374+
SetPayer(service).
375+
AddAuthorizer(service)
376+
}
377+
378+
// BridgeFTFromEVMTransaction returns the transaction body for the transaction
379+
// that bridges a fungible token from EVM to Cadence
380+
func BridgeFTFromEVMTransaction(
381+
service flow.Address,
382+
bridgeEnv bridge.Environment,
383+
env templates.Environment,
384+
forType string,
385+
amount uint,
386+
) *flow.TransactionBody {
387+
txScript, _ := bridge.GetCadenceTransactionCode("cadence/transactions/bridge/tokens/bridge_tokens_from_evm.cdc", bridgeEnv, env)
388+
389+
return flow.NewTransactionBody().
390+
SetScript([]byte(
391+
txScript,
392+
),
393+
).
394+
AddArgument(jsoncdc.MustEncode(cadence.String(forType))).
395+
AddArgument(jsoncdc.MustEncode(cadence.NewUInt256(amount))).
396+
SetProposalKey(service, 0, 0).
397+
SetPayer(service).
398+
AddAuthorizer(service)
399+
}
400+
401+
// GetEscrowedTokenBalanceScript returns the script body for the script
402+
// that gets the balance of an escrowed fungible token in the Cadence side of the VM bridge
403+
func GetEscrowedTokenBalanceScript(
404+
bridgeEnv bridge.Environment,
405+
env templates.Environment,
406+
) []byte {
407+
script, _ := bridge.GetCadenceTransactionCode("cadence/scripts/escrow/get_locked_token_balance.cdc", bridgeEnv, env)
408+
409+
return script
410+
}
411+
412+
// BridgeNFTToEVMTransaction returns the transaction body for the transaction
413+
// that bridges a non-fungible token from Cadence to EVM
414+
func BridgeNFTToEVMTransaction(
415+
service flow.Address,
416+
bridgeEnv bridge.Environment,
417+
env templates.Environment,
418+
forType string,
419+
id cadence.UInt64,
420+
) *flow.TransactionBody {
421+
txScript, _ := bridge.GetCadenceTransactionCode("cadence/transactions/bridge/nft/bridge_nft_to_evm.cdc", bridgeEnv, env)
422+
423+
return flow.NewTransactionBody().
424+
SetScript([]byte(
425+
txScript,
426+
),
427+
).
428+
AddArgument(jsoncdc.MustEncode(cadence.String(forType))).
429+
AddArgument(jsoncdc.MustEncode(id)).
430+
SetProposalKey(service, 0, 0).
431+
SetPayer(service).
432+
AddAuthorizer(service)
433+
}
434+
435+
// BridgeNFTFromEVMTransaction returns the transaction body for the transaction
436+
// that bridges a non-fungible token from EVM to Cadence
437+
func BridgeNFTFromEVMTransaction(
438+
service flow.Address,
439+
bridgeEnv bridge.Environment,
440+
env templates.Environment,
441+
forType string,
442+
id cadence.UInt256,
443+
) *flow.TransactionBody {
444+
txScript, _ := bridge.GetCadenceTransactionCode("cadence/transactions/bridge/nft/bridge_nft_from_evm.cdc", bridgeEnv, env)
445+
446+
return flow.NewTransactionBody().
447+
SetScript([]byte(
448+
txScript,
449+
),
450+
).
451+
AddArgument(jsoncdc.MustEncode(cadence.String(forType))).
452+
AddArgument(jsoncdc.MustEncode(id)).
453+
SetProposalKey(service, 0, 0).
454+
SetPayer(service).
455+
AddAuthorizer(service)
456+
}
457+
458+
// GetIsNFTInEscrowScript returns the script body for the script
459+
// that gets if an NFT is escrowed in the Cadence side of the VM bridge
460+
func GetIsNFTInEscrowScript(
461+
bridgeEnv bridge.Environment,
462+
env templates.Environment,
463+
) []byte {
464+
script, _ := bridge.GetCadenceTransactionCode("cadence/scripts/escrow/is_nft_locked.cdc", bridgeEnv, env)
465+
466+
return script
467+
}

fvm/blueprints/token.go

+9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ func DeployMetadataViewsContractTransaction(nonFungibleToken flow.Address, contr
4141
)
4242
}
4343

44+
func DeployCrossVMMetadataViewsContractTransaction(nonFungibleToken flow.Address, contract []byte) *flow.TransactionBody {
45+
contractName := "CrossVMMetadataViews"
46+
return DeployContractTransaction(
47+
nonFungibleToken,
48+
contract,
49+
contractName,
50+
)
51+
}
52+
4453
func DeployViewResolverContractTransaction(nonFungibleToken flow.Address) *flow.TransactionBody {
4554
contract := contracts.ViewResolver()
4655
contractName := "ViewResolver"

fvm/bootstrap.go

+54-25
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/onflow/flow-core-contracts/lib/go/contracts"
1212
"github.com/onflow/flow-core-contracts/lib/go/templates"
1313

14-
usdc "github.com/onflow/bridged-usdc/lib/go/contracts"
1514
bridge "github.com/onflow/flow-evm-bridge"
1615
storefront "github.com/onflow/nft-storefront/lib/go/contracts"
1716

@@ -379,9 +378,6 @@ func (b *bootstrapExecutor) Execute() error {
379378
b.deployMetadataViews(fungibleToken, nonFungibleToken, &env)
380379
b.deployFungibleTokenSwitchboard(fungibleToken, &env)
381380

382-
// deploys the USDCFlow smart contract
383-
b.deployUSDCFlow(fungibleToken, &env)
384-
385381
// deploys the NFTStorefrontV2 contract
386382
b.deployNFTStorefrontV2(nonFungibleToken, &env)
387383

@@ -453,6 +449,8 @@ func (b *bootstrapExecutor) Execute() error {
453449
b.setupEVM(service, nonFungibleToken, fungibleToken, flowToken, &env)
454450
b.setupVMBridge(service, &env)
455451

452+
b.deployCrossVMMetadataViews(nonFungibleToken, &env)
453+
456454
err = expectAccounts(systemcontracts.EVMStorageAccountIndex)
457455
if err != nil {
458456
return err
@@ -584,6 +582,21 @@ func (b *bootstrapExecutor) deployMetadataViews(fungibleToken, nonFungibleToken
584582
panicOnMetaInvokeErrf("failed to deploy fungible token metadata views contract: %s", txError, err)
585583
}
586584

585+
func (b *bootstrapExecutor) deployCrossVMMetadataViews(nonFungibleToken flow.Address, env *templates.Environment) {
586+
if bool(b.setupEVMEnabled) && b.ctx.Chain.ChainID().Transient() {
587+
crossVMMVContract := contracts.CrossVMMetadataViews(*env)
588+
589+
txError, err := b.invokeMetaTransaction(
590+
b.ctx,
591+
Transaction(
592+
blueprints.DeployCrossVMMetadataViewsContractTransaction(nonFungibleToken, crossVMMVContract),
593+
0),
594+
)
595+
env.CrossVMMetadataViewsAddress = nonFungibleToken.String()
596+
panicOnMetaInvokeErrf("failed to deploy cross VM metadata views contract: %s", txError, err)
597+
}
598+
}
599+
587600
func (b *bootstrapExecutor) deployFungibleTokenSwitchboard(deployTo flow.Address, env *templates.Environment) {
588601

589602
contract := contracts.FungibleTokenSwitchboard(*env)
@@ -781,27 +794,6 @@ func (b *bootstrapExecutor) deployNFTStorefrontV2(deployTo flow.Address, env *te
781794
panicOnMetaInvokeErrf("failed to deploy NFTStorefrontV2 contract: %s", txError, err)
782795
}
783796

784-
func (b *bootstrapExecutor) deployUSDCFlow(deployTo flow.Address, env *templates.Environment) {
785-
786-
contract := usdc.USDCFlowBasic(
787-
env.FungibleTokenAddress,
788-
env.MetadataViewsAddress,
789-
env.FungibleTokenMetadataViewsAddress,
790-
env.ViewResolverAddress,
791-
env.BurnerAddress)
792-
793-
txError, err := b.invokeMetaTransaction(
794-
b.ctx,
795-
Transaction(
796-
blueprints.DeployContractTransaction(
797-
deployTo,
798-
contract,
799-
"USDCFlow"),
800-
0),
801-
)
802-
panicOnMetaInvokeErrf("failed to deploy USDCFlow contract: %s", txError, err)
803-
}
804-
805797
func (b *bootstrapExecutor) mintInitialTokens(
806798
service, fungibleToken, flowToken flow.Address,
807799
initialSupply cadence.UFix64,
@@ -1267,6 +1259,43 @@ func (b *bootstrapExecutor) setupVMBridge(serviceAddress flow.Address, env *temp
12671259
)
12681260
panicOnMetaInvokeErrf("failed to add the NFT template code chunks: %s", txError, err)
12691261

1262+
// Retrieve the WFLOW bytecode from the JSON args
1263+
wflowBytecode, err := bridge.GetSolidityContractCode("WFLOW")
1264+
if err != nil {
1265+
panic(fmt.Sprintf("failed to get WFLOW bytecode: %s", err))
1266+
}
1267+
1268+
// deploy the WFLOW contract to the service account's COA
1269+
tx = blueprints.DeployEVMContractTransaction(serviceAddress, wflowBytecode, gasLimit, deploymentValue, bridgeEnv, *env)
1270+
1271+
txOutput, err = b.runMetaTransaction(
1272+
ctx,
1273+
Transaction(tx, 0),
1274+
)
1275+
panicOnMetaInvokeErrf("failed to deploy the WFLOW contract in the Service Account COA: %s", txOutput.Err, err)
1276+
1277+
wflowAddress := getContractAddressFromEVMEvent(txOutput)
1278+
1279+
// Create WFLOW Token Handler, supplying the WFLOW EVM address
1280+
txError, err = b.invokeMetaTransaction(
1281+
ctx,
1282+
Transaction(
1283+
blueprints.CreateWFLOWTokenHandlerTransaction(serviceAddress, bridgeEnv, *env, wflowAddress),
1284+
0),
1285+
)
1286+
panicOnMetaInvokeErrf("failed to create the WFLOW token handler: %s", txError, err)
1287+
1288+
// Enable WFLOW Token Handler, supplying the Cadence FlowToken.Vault type
1289+
flowVaultType := "A." + env.FlowTokenAddress + ".FlowToken.Vault"
1290+
1291+
txError, err = b.invokeMetaTransaction(
1292+
ctx,
1293+
Transaction(
1294+
blueprints.EnableWFLOWTokenHandlerTransaction(serviceAddress, bridgeEnv, *env, flowVaultType),
1295+
0),
1296+
)
1297+
panicOnMetaInvokeErrf("failed to enable the WFLOW token handler: %s", txError, err)
1298+
12701299
// Unpause the bridge
12711300
txError, err = b.invokeMetaTransaction(
12721301
ctx,

0 commit comments

Comments
 (0)